ngTable如何以编程方式为表数据单元设置过滤器数据

时间:2014-11-05 09:36:42

标签: javascript angularjs angular-promise ngtable

我正在使用ngTable和async加载表,但其中一列使用了select过滤器,当调用它时,表数据尚未解析。最初我使用本地JSON对象测试ngTable,这很有效,但现在我不知道如何填充选择。是否可以以编程方式设置列的filter-data以填充选择字段?

ngTable Cell

<td data-title="'Status'" 
    sortable="'status'" 
    filter="{ 'status': 'select' }"
    filter-data="getStatus()"  // hasn't resolved yet and select is empty
    ng-bind="doc.status"></td>

控制器功能

$scope.getStatus = function() {

    var def = $q.defer();
    var arr = [];
    var status = [];

    // NOTE: RestService.getPackages() below isn't resolved and select is empty

    angular.forEach( RestService.getPackages(), function( item ) {

        // Check if status already exists
        if( arr.indexOf( item.status ) === -1 ) {

            // Store status for comparison to avoid duplicates
            arr.push( item.status );

            // Store status information for filtering
            status.push({
                'id': item.status,
                'title': item.status
            });
        }
    });

    def.resolve( status );

    return def;
};

1 个答案:

答案 0 :(得分:1)

我认为由于promise的调用导致RestService.getPackages()链接中的问题,你应该通过回调更明确地做到这一点,这里是我的小例子:

$scope.getData = function() {
    var def = $q.defer();
    var Names = [];

    //simulate  network delay
    $timeout(function(){

      Names.push({
         title: "Enos",
        id: "Enos"});
      Names.push({
         title: "Nephi",
        id: "Nephi"});

    }, 1000);

    def.resolve(Names);
    return def;
  };

在这里工作example