【问题标题】:ng-Table not rendering new data when reloading request重新加载请求时ng-Table不呈现新数据
【发布时间】:2014-10-18 03:54:41
【问题描述】:

我有一个带有 ng-table 的页面,它可以很好地处理初始数据。我有一个选择框,它根据选择的选项发送服务器请求,然后我将新数据获取到 Angular 但未使用 ng-table 更新。

这是我的看法:

<section data-ng-controller="ReportsController">
<div class="plain-box">
    <table ng-table="tableParams" show-filter="true" class="table table-striped">
        <thead>
        <tr>
            <th ng-repeat="column in columns" ng-show="column.visible"
                class="text-center sortable" ng-class="{
                    'sort-asc': tableParams.isSortBy(column.field, 'asc'),
                    'sort-desc': tableParams.isSortBy(column.field, 'desc')
                  }"
                ng-click="tableParams.sorting(column.field, tableParams.isSortBy(column.field, 'asc') ? 'desc' : 'asc')">
                {{column.title}}
            </th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="user in $data">
            <td ng-repeat="column in columns" sortable="column.field">
                {{user[column.field]}}
            </td>
        </tr>
        </tbody>
    </table>
</div>

我的控制器:

angular.module('reports').controller('ReportsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Reports', '$filter', 'ngTableParams',
function($scope, $stateParams, $location, Authentication, Reports, $filter, ngTableParams ) {
    $scope.reportBillingPeriod = 0;
    $scope.$watch('reportBillingPeriod', function(newValue, oldValue) {
        $scope.findReportData( newValue );
    });
    //$scope.reportBillingData = 0;


    $scope.findReportData = function( selectedMonth ){
        var selectedPeriod = selectedMonth;
        if( selectedPeriod === null )
        selectedPeriod = '0';
        $scope.customers_report = Reports.customer_report({
        monthReport: selectedPeriod
        }, function( result ){
        var data = result.customers;
        $scope.columns = [
            { title: 'Account Name', field: 'accountName', visible: true, filter: { 'name': 'text' } },
            { title: 'Gross Revenue', field: 'planTotalAmount', visible: true }
        ];

   $scope.$watch('result', function () {
        $scope.tableParams.settings().$scope = $scope;
        $scope.tableParams.reload(); 
    });

        $scope.tableParams = new ngTableParams({
            page: 1,            // show first page
            count: 10,          // count per page
            filter: {
            name: 'O'       // initial filter
            }
        }, {
            total: data.length, // length of data
            getData: function($defer, params) {
            var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
            //$scope.reportBillingData = new Date();
            }
        });
        });
    };
}]);

【问题讨论】:

    标签: angularjs ngtable


    【解决方案1】:
    $scope.$watch('result', function () {
            $scope.tableParams.settings().$scope = $scope; <<--nested scope is replacing!!
            $scope.tableParams.reload(); 
        });
    

    ngTable 指令创建嵌套范围,其中包含指令的所有设置,因此您不应将其替换为 ReportsController 的范围
    你需要删除这一行 $scope.tableParams.settings().$scope = $scope;
    并且你需要为 ngTable 指定所有参数,请看一下 这里In ngTables, running $scope.tableParams.reload() for the third time results in TypeError: Cannot set property '$data' of null

    更新 并且最好将 ngTableParams 初始化和 $watch 用于 findReportData 函数之外的结果,以防止错误和不必要的重新初始化

    更新 2 我设置了plunk,试着让它看起来更像原始问题,并模拟httpservice, 但这没关系,所有数据访问都必须在ngTableParams 中的getData func 中进行,并且在设置所有需要调用的过滤器时(或者可能只使用$watch 就像在plunk 中一样)你必须调用$scope.tableParams.reload();,您可以尝试在上面的输入中键入一些数字,数据将毫无问题地更新。为清楚起见,删除了与排序、分页和过滤相关的所有代码, 所以getData 如下所示:

     getData: function($defer, params) {
            var selectedPeriod = $scope.reportBillingPeriod ; //selectedMonth;
             if( selectedPeriod === null )
        selectedPeriod = '0';
        $scope.customers_report = Reports.get({   //Reports.customer_report({
        monthReport: selectedPeriod
        }, function(result ){
        var data = result.customers;
         $scope.tableParams.total(data.length);
    
    
        var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    
        });      
    
            }
    

    观察过滤器的变化如下所示:

     $scope.reportBillingPeriod = 0;
        $scope.$watch('reportBillingPeriod', function(newValue, oldValue) {
            $scope.tableParams.reload(); 
         });
    

    更新 3
    删除重复调用getData func,编辑here 问题出在$watchreportBillingPeriod 变量中,所以为了防止第二次数据上传,我们必须检查值是如何变化的,就像这里

    $scope.reportBillingPeriod = defaultValue;
        $scope.$watch('reportBillingPeriod', function(newValue, oldValue) {
            //this how we prevent second call
            if (newValue!=oldValue)
            $scope.tableParams.reload(); 
         });
    

    【讨论】:

    • 我将 $watch 移到了 findReportData 函数之外,然后注释掉了 $scope.tableParams.settings().$scope = $scope;。然后手动分配pagecounttotalcounts 的值。但仍然无法正常工作。
    • 哦,还有一些需要让它工作,让我稍后设置 plunk,找出问题所在
    • 抱歉回复晚了,请检查答案
    • 第一次加载时,getData 会被调用两次。有什么办法可以控制吗?
    • 添加了 if($scope.reportBillingPeriod !=0) $scope.tableParams.reload();。现在工作正常。
    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2014-11-27
    • 2021-07-08
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多