【问题标题】:ngTable.reload() unable to refresh ng-table does not show new data second timengTable.reload() 无法刷新 ng-table 第二次不显示新数据
【发布时间】:2015-06-23 10:29:00
【问题描述】:

ngTable.reload()无法刷新ng-table does not show new data second time 我正在绑定 ng-table 数据的 index.js 的代码。

第一次数据绑定正常工作。第二次数据没有正确绑定。它显示了以前的数据。 所以基本上是无法绑定数据,无法刷新

 $http.get("GetValidationsDataForTable", { params: { "entity": entity } })
                 .success(function (response) {
                     $scope.tableValue = response;
                     $scope.tableParams = [];
                     $scope.tableParams = new ngTableParams(
                         {
                             page: 1,            // show first page
                             count: 5          // count per page
                         },
                         {
                             groupBy: 'Entity',
                             total: $scope.tableValue.length,
                             getData: function ($defer, params) {

                                 var orderedData = params.filter() ?
                            $filter('filter')($scope.tableValue,    params.filter()) :
                            $scope.tableValue;

                                 var orderedData = params.sorting() ?
                                         $filter('orderBy')($scope.tableValue, $scope.tableParams.orderBy()) : scope.tableValue;

                                 orderedData = params.filter ?
                            $filter('filter')(orderedData, params.filter()) :
                            orderedData;
                                 params.total(orderedData.length);
                                 $defer.resolve($scope.tableValue.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                             }
                         });
                     $scope.tableParams.reload();
                 })

index.cshtml 的代码

  <table id="tblValue" ng-table="tableParams" class="customTable" style="table-layout:fixed;max-width:100%">
                    <tbody ng-repeat="group in $groups">


                        <tr ng-hide="group.$hideRows" ng-repeat="validation in group.data" style="table-layout:fixed;">

                            <td data-title="'Property'" style="width:10%;text-align:left !important;overflow:hidden;">
                                <label id="lblProperty" ng-model="validation.Property" for="Property" ng-hide="editmode" style="width:97%;word-wrap:break-word; overflow-wrap:break-word;">{{validation.Property}}</label>

                                <select class="form-control" data-ng-model="validation.PropertyChoice" data-ng-options="choice.Name for choice in CurrentEntityProperties" ng-change="ChangeValidationRules(validation)" ng-show="editmode" style="width:100%; ">
                                    <option value="" style="margin-left:25px">-Select Property-</option>
                                </select>

                            </td>
                        </tr>
                    </tbody>
                </table>

为什么 ngtable.reload() 不起作用并且第二次也不显示新数据?

【问题讨论】:

  • 什么情况下要刷新数据?
  • 第一次正确绑定数据。例如,特定树选择的记录,例如验证的记录。下次我可以更改过滤器参数,例如说配置,问题从这里开始,配置数据正确但未绑定到表。此处的表再次显示以前的值,即验证记录值。这就是问题
  • 也许this可以推动你前进。

标签: angularjs angularjs-directive angularjs-ng-repeat ngtable


【解决方案1】:

解决办法是:

$scope.tableParams = {};
    $http.get("GetValidationsDataForTable", { params: { "entity": entity } })
                 .success(function (response) {
                     $scope.tableValue = response;
                     $scope.tableParams = [];
                     $scope.tableParams = new ngTableParams(
                         {
                             page: 1,            // show first page
                             count: 5          // count per page
                         },
                         {
                             groupBy: 'Entity',
                             total: $scope.tableValue.length,
                             getData: function ($defer, params) {

                                 var orderedData = params.filter() ?
                            $filter('filter')($scope.tableValue,    params.filter()) :
                            $scope.tableValue;

                                 var orderedData = params.sorting() ?
                                         $filter('orderBy')($scope.tableValue, $scope.tableParams.orderBy()) : scope.tableValue;

                                 orderedData = params.filter ?
                            $filter('filter')(orderedData, params.filter()) :
                            orderedData;
                                 params.total(orderedData.length);
                                 $defer.resolve($scope.tableValue.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                             }
                         });
                     $scope.tableParams.reload();
                 })

即在每次请求之前将表参数设为空白。

【讨论】:

    【解决方案2】:

    此问题的解决方案是在对数据进行任何请求之前将表参数设置为空白。

    $scope.GetValidationsDataForTable = function(entity) {
    
        // $("div").removeClass("container body-content");
    
        $scope.tab1eParams = {};
        SpinStart();
        $http.get("GetValidationsDataForTable", {
            params: {
                "entity": entity
            }
        }).success(function(response) {
            var resultArray = response;
            SpinStop();
            if (!$.isArray(resu1tArray) || !resultArray.1ength) {
                $scope.HideFormVa1ue();
                alert("Ho record found for this selection.");
            }
            else {
    
                $scope.ShowFormVa1ue();
                $scope.rows = response;
                $scope.groupby = 'Entity',
                $scope.tab1eParams = new ngTableParams({
                    page: 1,
                    count: 50,
                    filter: {},
                    sorting: {}
                },
                {
                    groupBy: $scope.groupby,
                    counts: [],
                    total: function() {
                        return $scope.rows.1ength;
                    },
                    getData: function($defer, params) {
                        $defer.resolve($scope.rows.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                    }
                });
    

    【讨论】:

    • 截图。呲牙咧嘴。那是。创意。
    【解决方案3】:

    您必须在使用您的服务之前克​​隆params.filter(),并在HTML 数据绑定中使用params.filter().yourFilter

    就像你不需要使用reload()一样,ngTable会自动重新加载。

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2020-05-11
      相关资源
      最近更新 更多