2016-02-06 46 views
0

我嘗試通過下拉使用angularjs進行排序。但排序不起作用。請幫幫我。請檢查此鏈接:jsfiddle如何在angularjs中對下拉列表進行排序?

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數組:的json 「鍵=>值」 陣列使用下拉列表

$scope.charityList = { 
    "1": "softweb charity", 
    "2": "charity 1", 
    "3": "charity update", 
    "4": "Engagement1 Name", 
    "6": "United Way Enagagement", 
    "7": "Indian Noble Charity" 
} 

請檢查這個鏈接:jsfiddle

+0

要通過鑰匙或值對列表進行排序? – Harshad

回答

0

你擁有的是一個不是數組的對象。數組由V形圖定義。

charityList = [ 
    {"name": "softweb charity"}, 
    {"name": "charity 1"}, 
    {"name": "charity update"}, 
    {"name": "Engagement1 Name"}, 
    {"name": "United Way Enagagement"}, 
    {"name": "Indian Noble Charity"} 
], 

如果你可以使它看起來這種方式,您可以用排序依據

排序呢
ng-options="value.name for value in charityList | orderBy:'name'" 
+0

關鍵是我的要求..我沒有刪除的關鍵(1,2,3,4,6,7) –

+0

你可以創建你試圖達到什麼plunker? –

+0

你好朋友請檢查:http://jsfiddle.net/bschauhan/tu568yj0/3/ –

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> 
+0

請檢查這個鏈接:http://jsfiddle.net/bschauhan/tu568yj0/3/ –

+0

我檢查了你的小提琴,但在你的小提琴orderBy將無法正常工作,因爲按orderBy的語法參數應該是「對象數組中的屬性」,而在您的情況下,charityList是單個「對象」。這就是爲什麼在我的回答中,我調用函數sort(),它返回對中的排序值數組。 – Nijeesh

0

你必須改變你的對象爲對象,否則排序依據是行不通的數組。

angular.forEach($scope.charityList, function(value, key) { 
     arr.push({key: key, value: value}) 

     }) 

fiddler