我正在显示一个结果列表,使用一个把手帮助器,它循环在控制器中的一个数组上,如下所示:
{{#each results as |job|}}
{{job-table-row model=job}}
{{/each}}
results: function() {
var searchTerm = this.get('searchTerm'),
results = this.get('content'),
order = this.get('date_ordering'),
status = this.get('Status'),
if (searchTerm) {
results = results.filter(function(item) {
return item.get('name').toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
});
}
if (status) {
results = results.filter(function(item) {
return (item.get('status') == status);
}
});
}
return results;
}.property('content', 'searchTerm', 'startDate', 'endDate', 'date_ordering', 'Status', 'Platform'),
我想在属性'date-ordering'收到值时添加对'created_at'列进行排序的功能:
results: function() {
var searchTerm = this.get('searchTerm'),
results = this.get('content'),
order = this.get('date_ordering'),
status = this.get('Status'),
...
if ((order) && (order ===0)) {
results = results.sort('created_at' sortAscending);
}
else if ((order) && (order ===1)) {
results = results.sort('created_at' sortDescending);
}
});
}
return results;
}.property('content', 'searchTerm', 'startDate', 'endDate', 'date_ordering', 'Status', 'Platform'),
是否可以采用与上述类似的方式进行此操作?
答案 0 :(得分:2)
我相信你的建议应该有用。
或者,您可以通过不同的属性进行排序:
sortedResults: Ember.computed.sort('results', 'sortProperties'),
sortProperties: function() {
var order = this.get('date_ordering') ? "asc" : "desc";
return ['created_at:' + order];
}.property('date_ordering')
将each
助手更改为循环sortedResults