2009-08-03 76 views
3

對我的dataprovider(Array Collection)應用數字排序後,我無法通過tilelist重新排序項目。我需要從arrayCollection中移除排序嗎?如果是這樣,是否只是設置collection.sort = null?ArrayCollection刪除排序

var sortField:SortField=new SortField(); 
sortField.name="order"; 
sortField.numeric=true; 
var sort:Sort=new Sort(); 
sort.fields=[sortField]; 

回答

4

將排序設置爲空應該確實會刪除集合的排序。您可能需要執行可選的刷新()。

+0

哼哼......實際上,當我設置`sort = null`並嘗試編輯`ArrayCollection`時,我得到一個異常。除非我調用`ArrayCollection.refresh()`,但是當我這樣做時,即使'sort`爲'null',數據也會被排序。我卡住了:p – nununo 2011-05-28 16:18:12

1

Source

Adob​​e Flex的 - 按日期排序的ArrayCollection

/** 
* @params data:Array 
* @return dataCollection:Array 
**/ 
private function orderByPeriod(data:Array):Array 
{ 
var dataCollection:ArrayCollection = new ArrayCollection(data);//Convert Array to ArrayCollection to perform sort function 

var dataSortField:SortField = new SortField(); 
dataSortField.name = "period"; //Assign the sort field to the field that holds the date string 

var numericDataSort:Sort = new Sort(); 
numericDataSort.fields = [dataSortField]; 
dataCollection.sort = numericDataSort; 
dataCollection.refresh(); 
return dataCollection.toArray(); 
} 
1

我得到了這個問題抓過,我發現你的問題,我仍然沒有得到它解決了像克里斯托夫建議。

經過一段時間的苦難,我發現了一種避免你提到的問題的方法。

只需使用輔助ArrayCollection進行排序。無論如何,你的排序實例似乎是臨時的(你想通過它),所以爲什麼不使用臨時ArrayCollection?

這裏是我的代碼是如何模樣:

// myArrayCollection is the one to sort 

// Create the sorter 
var alphabeticSort:ISort = new Sort(); 
var sortfieldFirstName:ISortField = new SortField("firstName",true); 
var sortfieldLastName:ISortField = new SortField("lastName",true); 
alphabeticSort.fields = [sortfieldFirstName, sortfieldLastName]; 

// Copy myArrayCollection to aux 
var aux:ArrayCollection = new ArrayCollection(); 
while (myArrayCollection.length > 0) { 
    aux.addItem(myArrayCollection.removeItemAt(0)); 
} 

// Sort the aux 
var previousSort:ISort = aux.sort; 
aux.sort = alphabeticSort; 
aux.refresh(); 
aux.sort = previousSort; 

// Copy aux to myArrayCollection 
var auxLength:int = aux.length; 
while (auxLength > 0) { 
    myArrayCollection.addItemAt(aux.removeItemAt(auxLength - 1), 0); 
    auxLength--; 
} 

這不是最巧妙的代碼,它有一個像auxLength代替aux.length一些奇怪的黑客(此人給我-1陣列範圍除外),但在至少它解決了我的問題。