我尝试使用angularjs下拉命令。但是,排序不起作用。请帮我。请检查此链接:jsfiddle
HTML: 我的Html代码在这里
<select ng-options="key as value for (key, value) in charityList | orderBy:'value'" ng-model="reportSearch.charityId">
<option value="">All Charity</option>
</select>
JSON ARRAY:我的Json“Key =&gt; Value”数组使用下拉列表
$scope.charityList = {
"1": "softweb charity",
"2": "charity 1",
"3": "charity update",
"4": "Engagement1 Name",
"6": "United Way Enagagement",
"7": "Indian Noble Charity"
}
请检查此链接:jsfiddle
答案 0 :(得分:0)
你所拥有的是一个对象而不是一个数组。数组由V形符号定义。
charityList = [
{"name": "softweb charity"},
{"name": "charity 1"},
{"name": "charity update"},
{"name": "Engagement1 Name"},
{"name": "United Way Enagagement"},
{"name": "Indian Noble Charity"}
],
如果您可以通过这种方式查看,可以使用orderBy
对其进行排序ng-options="value.name for value in charityList | orderBy:'name'"
答案 1 :(得分:0)
以这种方式试试。
控制器:
$scope.sort = function(list) { //returns an array : ["charity 1","charity update","Engagement1 Name","Indian Noble Charity","softweb charity", "United Way Enagagement"]
return $.map(list, function(el) { return el }).sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
}
html:
<select ng-options="value as value for value in sort(charityList)" ng-model="reportSearch.charityId">
<option value="">All Charity</option>
</select>
答案 2 :(得分:0)
您必须将对象转换为对象数组,否则orderBy将无效。
angular.forEach($scope.charityList, function(value, key) {
arr.push({key: key, value: value})
})