4

我正在尝试使用类似于此示例的 ngtable 在 angularjs 表中实现“自定义过滤” ,但在我的情况下,“数据”是异步获取的,需要一些时间。我面临的问题是,当访问页面时,控制器中的这个函数(而其他函数工作正常)永远不会运行,在调试时它只是跳过它。

$scope.docNames = function(column) {
    var def = $q.defer(),
        arr = [],
        names = [];
    angular.forEach(data, function(item){
        if (inArray(item.name, arr) === -1) {
            arr.push(item.name);
            docNames.push({
                'id': item.Name,
                'title': item.Name
            });
        }
    });
    def.resolve(docNames);
    return def;
};

我的 ngTable 对象:

$scope.tableParams = new ngTableParams(
    {
        page: 1,
        count: 10,
        sorting: {
            Date: 'desc'
        }
     },
     {
         total: 0,
         getData: function ($defer, params) {

             factory.getData().then(function (result) {
                 documents = result.data;

                 //filtering
                 var orderedData = params.filter() ?
                     $filter('filter')(documents, params.filter()) :
                     documents;

                 //sorting
                 orderedData = params.sorting() ?
                     $filter('orderBy')(orderedData, params.orderBy()) :
                     orderedData;

                 //pagination
                 $scope.documents = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
                 params.total(orderedData.length);
                 $defer.resolve($scope.documents);
            });
       }
  });

我的html:

<table data-ng-table="tableParams" data-show-filter="true" class="table table-striped">
    <tr data-ng-repeat="document in $data">
        <td data-title="'Name'" filter="{ 'Name': 'select' }" filter-data="docNames($column)" data-sortable="'Name'">{{ document.Name }} </td>
        <td data-title="'Size'" filter="{ 'Size': 'text' }" data-sortable="'Size'">{{ document.Size }}</td>
        <td data-title="'Date'" data-sortable="'Date'">{{ document.Date }}</td>
    </tr>
</table>
4

1 回答 1

2

正如预测的那样,在评估“docNames”列表时,“文档”尚未加载。我们通过在第一次使用它之前解决 defer 来解决这个问题:

var qDocse = $q.defer();
qDocs.resolve(factory.getData());
$scope.tableParams = new ngTableParams(
    {
        page: 1,
        count: 10,
        sorting: {
             Date: 'desc'
        }
    },
    {
        total: 0,
        getData: function ($defer, params) {
            qDocs.promise.then(function (result) {
                documents = result.data;

                //filtering
                var orderedData = params.filter() ?
                    $filter('filter')(documents, params.filter()) :
                    documents;

                //sorting
                orderedData = params.sorting() ?
                    $filter('orderBy')(orderedData, params.orderBy()) :
                    orderedData;

                //pagination
                $scope.documents = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
                params.total(orderedData.length);
                $defer.resolve($scope.documents);
            });
       }
  });

$scope.docNames = function (column) {
    var def = $q.defer(),
        arr = [],
        docNames = [];
    qDocs.promise.then(function (result) {
        angular.forEach(result.data, function (item) {
            if (inArray(item.Name, arr) === -1) {
                arr.push(item.Name);
                docNames.push({
                    'id': item.Name,
                    'title': item.Name
                    });
                }
            });
        });
     def.resolve(docNames);
     return def;
};
于 2014-03-10T14:06:18.897 回答