我有价格列表,我想根据下拉列表的选择对其进行排序。我想以升序和降序两种方式对数据进行排序。
在选择“从低到高”时,我想按降序对价格列表进行排序,在选择“从高到低”时,我想按升序对价格列表进行排序。
在这里,我将Application
夹具传递给index
路线模型,并且Astcart.IndexController
我正在尝试对数据进行排序,但它抛出了一个错误Uncaught TypeError: Object [object Object] has no method 'sort'
Astcart.IndexRoute = Ember.Route.extend({
model: function() {
return Astcart.Application.find();
}
});
Astcart.IndexController = Ember.ArrayController.extend({
sortingMethod: null,
sortingOptions: [
{option: "Best Match", id: 0},
{option: "Low to High", id: 1},
{option: "High to Low", id: 2}
],
sortProperties: ['product_price'],
sortAscending: true,
sortingMethodDidChange: function() {
if(this.get('sortingMethod') == 1){
alert("sort data in descending order");
}else if (this.get('sortingMethod') == 2){
alert("sort data in ascending order");
}
var content = this.get("content");
this.set('content', Ember.copy(content.sort(), true));
}.observes('sortingMethod')
});
夹具:
Astcart.Application.adapter = Ember.FixtureAdapter.create();
Astcart.Application.FIXTURES=[
{
"home_products": [
{
"id": "1",
"name": "Mobiles & Accessories",
"contents": [
{
"id": "1",
"price": "1"
},
{
"id": "2",
"price": "2"
},
{
"id": "3",
"price": "3"
},
{
"id": "4",
"price": "4"
}
]
},
{
"id": "2",
"name": "Mobiles & Accessories",
"contents": [
{
"id": "41",
"price": "5"
},
{
"id": "5",
"price": "6"
},
{
"id": "6",
"price": "7"
},
{
"id": "7",
"price": "8"
}
]
},
{
"id": "3",
"name": "Mobiles & Accessories",
"contents": [
{
"id": "8",
"price": "9"
},
{
"id": "9",
"price": "10"
},
{
"id": "10",
"price": "11"
},
{
"id": "11",
"price": "12"
}
]
}
]
}
];
我在这里(jsfiddle)发布了完整的代码。任何人都可以帮助我使这个jsfiddle工作吗?