use*_*728 18 javascript angularjs
这是使用PHP生成的json
[{"Sale":{"id":"1","customer_id":"1","amount":"15","created":"2014-05-17"}}]
Run Code Online (Sandbox Code Playgroud)
做orderBy:id的显然不起作用.我在其他帖子中读到需要为这种数据结构创建orderBy函数.我无法创建该功能.任何人都可以告诉我这是正确的方法吗?
Kin*_*noP 59
假设你有:
$scope.sales = [{"Sale":{"id":"1"}},{"Sale":{"id":"2"}},{"Sale":{"id":"3"}}];
Run Code Online (Sandbox Code Playgroud)
为什么不这样做:
<div ng-repeat="sale in sales | orderBy:'Sale.id':true">
{{ sale.Sale.id }}
</div>
Run Code Online (Sandbox Code Playgroud)
它适用于我的情况.可能是Angular在你问这个问题的时候不支持它.
这些类型的数据操作我喜欢将它们保存在适当的角度对象中,因此我会创建自定义过滤器,例如:
var sampleSource= [{"Sale":{"id":"8","customer_id":"1","amount":"15","created":"2014-05-17"}}, {"Sale":{"id":"5","customer_id":"6","amount":"15","created":"2015-05-17"}}];
var myApp = angular.module('myApp',[]);
myApp.filter('myFilter', function() {
return function(items) {
items.sort(function(a,b){
if (parseInt(a.Sale.id) > parseInt(b.Sale.id))
return 1;
if (parseInt(a.Sale.id) < parseInt(b.Sale.id))
return -1;
return 0; })
});
Run Code Online (Sandbox Code Playgroud)
重要提示:我推荐使用自定义过滤器,因为根据个人喜好,我不喜欢使用我可以在其他对象上分离的任务(代码)来重载我的控制器或其他对象,从而为我提供更多独立性和更清晰的代码(我喜欢的事情之一)角度)但除了这个个人偏好,我会说这不是唯一的方法,但如果你分享我背后的原因,我希望它有所帮助.
| 归档时间: |
|
| 查看次数: |
16385 次 |
| 最近记录: |