在嵌套领域ngTable过滤 [英] ngTable filtering on nested fields
问题描述
我有在NG表的发票清单,并希望能够在嵌套的属性进行过滤。 JSON的是这样的;
I've got a list of invoices in ng-table and would like to be able to filter on a nested attribute. The json looks like this;
[
{
id: 1,
date: "20/03/2014",
no: "1",
client: {
fullname: "ABC Catering"
}
}
]
我认为这个样子的。
My view look like this
<table ng-table="tableParams" show-filter="true" class="table">
<tr class='listing' ng-repeat="invoice in $data">
<td data-title="'Invoice No.'" sortable="'no'" filter="{'no':'text'}">
{{invoice.no}}
</td>
<td data-title="'Date'" sortable="'date'" filter="{'date':'text'}">
{{invoice.date}}
</td>
<td data-title="'Client'" sortable="'client.fullname'" filter="{'client.fullname':'text'}">
{{invoice.client.fullname}}
</td>
<td>
<a href="/api#/invoices/{{invoice.id}}">Show</a>
<a href="/api#/invoices/{{invoice.id}}/edit">Edit</a>
<a href="" ng-confirm-click="destroy(invoice.id)">Delete</a>
</td>
</tr>
</table>
我想获得的滤波client.fullname工作。如何在嵌套属性过滤器?
I would like to get the filtering working for client.fullname. How do I filter on nested attributes?
更新
我发现身边,我只是把嵌套领域到非嵌套的JSON元素,在上面的例子中,我创建一个JSON ['CLIENT_NAME']元素,并为其分配一个工作轨道模型中client.fullname 。然后过滤器的工作原理,因为它不是嵌套。
I've found a work around where I just put the nested field into a non-nested JSON element, in the above example I create a JSON['client_name'] element and assign it to client.fullname within the rails model. Then the filter works as it's not nested.
还在寻找一个方式,我可以不用做这项工作各地。
Still looking for a way in which I could do without this work around.
推荐答案
您可以使用 $过滤器
上任何你想从JSON响应滤波器上
You can use $filter
on anything you want to filter on from the JSON response.
<大骨节病> 这里 骨节病> 是一个人为的例如如何过滤可以嵌套JSON元素上进行。样品code为从纳克表与过滤器用法的例子之一作出。
HERE is a contrived example of how filtering can be done on nested JSON element. Sample code is taken from one of the example of ng-table's usage with filters.
主要部分应用需要注意的是
Main part to note in the app is
$scope.tableParams = new ngTableParams({
page: 1,
count: 10,
filter: {
'client': 'Bo' //Sample filter
}
}, {
total: data.length,
getData: function($defer, params) {
//Note the usage of built in angular filter
//which is injected in the app
var orderedData =
params.filter() ?
$filter('filter')(data, params.filter()) :
data;
$scope.users =
orderedData.slice((params.page() - 1) * params.count(),
params.page() * params.count());
params.total(orderedData.length);
$defer.resolve($scope.users);
}
});
Plunker按预期工作(如果我得到你的要求是正确的)。给留言如果这东西是不是你的目标所在。 :)
Plunker works as expected (if I got your requirement correct). Give a shout if that something that is not what you are aiming at. :)
这篇关于在嵌套领域ngTable过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!