ngtable:对嵌套对象进行排序和过滤 [英] ngtable : sort and filter on nested object

查看:46
本文介绍了ngtable:对嵌套对象进行排序和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象列表要在ngTable的表上显示.我的对象看起来像:

I have a list of objects to display on a table with ngTable. My object looks like :

obj {label:string,
     nestObj{nestLabel:string
            }
    }

在我的控制器中,我想允许对字段"label"和"nestObject.label"进行排序和过滤.我已经尝试过:

In my controller I want to allow sorting and filtering on fields 'label' and 'nestObject.label'. I have tried this:

$scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 10,
        filter: {
            label='',
            nestObj.label=''
        },
        sorting: {
            label: 'asc',
            nestObj.label: 'asc'
        }
    }, {
        total: data.length, // length of data
        getData: function($defer, params) {
            // use build-in angular filter
            var filteredData = params.filter() ?
            $filter('filter')(data, params.filter()) :
            data;
            var orderedData = params.sorting() ?
                    $filter('orderBy')(filteredData, params.orderBy()) :
                    data;

            params.total(orderedData.length); // set total for recalc pagination
            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    }          
});

但是我遇到了一个错误,javascript编译器不喜欢nestObj.label上的过滤器:

But I m getting an error, the javascript compiler doesn't like the filter on nestObj.label :

严重的syntexError:意外令牌.

Uncaugth syntexError : unexpected token .

如果我不对nestObj.label进行过滤和排序,它的效果很好.

IT works well if I don't filter and sort on nestObj.label.

是否可以使用ngTable对嵌套对象进行过滤和排序?

Is it possible to filter and sort on nested object with ngTable?

这是说明问题的 plunker .

谢谢.

推荐答案

不幸的是,使用嵌套对象进行过滤和排序目前尚不适用于ng-table.阅读此帖子和@ Kostia Mololkin ,我终于知道了如何避免该错误,并且最终的解决方案非常简单.非常感谢他!

Unfortunately, the filtering and sorting with nested objects is not suitable in ng-table for now. Reading this post and solution from @Kostia Mololkin, I finally got it how to avoid this bug and the solution is in the end very simple. Big thanks to him!

我只是重写了数组,其中是您的数据:初始化新属性并将数据从嵌套对象设置为新属性,如:

I just rewrote the array where is your data: initialized the new property and set the data from nested object into the new property like:

for (var i = 0; i < data.length; i++) {
  data[i].town = ""; //initialization of new property 
  data[i].town = data[i].adresse.town;  //set the data from nested obj into new property
}

您可以在 plunker 上看到此解决方案,现在它就像一种魅力...

You can see this solution here on plunker, now it's working like a charm...

这篇关于ngtable:对嵌套对象进行排序和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆