带有组合框的过滤柱,角度为ngtable

时间:2014-03-10 09:39:26

标签: javascript angularjs ngtable

我正在尝试使用类似于this example的ngtable在angularjs表中实现'自定义过滤',但在我的情况下,'data'是异步获取的并且需要一些时间。我面临的问题是,当访问页面时,控制器中的这个功能(当其他功能正常工作时)从不运行,在调试它时只是跨过它。

$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>

1 个答案:

答案 0 :(得分:2)

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

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;
};