flex datagrid分頁排序失效

分頁改變了datagrid的數據源,數據源變了視圖就跟着變了。因此原來的排序方式丟失。解決的辦法是在更新數據源之前保存Sort。改變數據後對在對arrayCollection加上Sort屬性。
下面是代碼的關鍵

if(obj.data!=null){
if(userDb!=null){
var sortFields:Sort=userDb.sort;
userDb=obj.data as ArrayCollection;
userDb.sort=sortFields;
userDb.refresh();
}else{
userDb=obj.data as ArrayCollection;
}

}

備註:userDb.refresh();加上這句話排序才起作用。
下面的代碼是關於ArrayCollection的排序的

假設ArrayCollection(m_myArrayCollection)屬性有userID,userName,regTime。

1、按regTime排序
程序代碼
var m_myArrayCollection:ArrayCollection = new ArrayCollection();
//先加入N個測試object
m_myArrayCollection.addItem({userID:0,userName:AAA,regTime:2008-02-28},{userID:1,userName:BBB,regTime:2008-02-29},{userID:2,userName:CCC,regTime:2008-03-01});
//設定Sort對象
var m_sort:Sort = new Sort();
//默認按升序排序
m_Sort.fields = [new SortField("regTime")];
//按降序排序,把上一句註釋,比對一下效果
//m_Sort.fields = [new SortField("regTime",true,true)];
//把排序方法指定給m_myArrayCollection
m_myArrayCollection.sort = m_sort;
//如果不執行refresh,啥事都不發生
m_myArrayCollection.refresh();
2、先按userID降序排序,再按userName升序排序
程序代碼
var m_myArrayCollection:ArrayCollection = new ArrayCollection();
//先加入N個測試object
m_myArrayCollection.addItem({userID:0,userName:AAA,regTime:2008-02-28},{userID:1,userName:BBB,regTime:2008-02-29},{userID:2,userName:CCC,regTime:2008-03-01});
//設定Sort對象
var m_sort:Sort = new Sort();
//默認按升序排序
m_Sort.fields = [new SortField("userID",true,true),new SortField("userName")];
//把排序方法指定給m_myArrayCollection
m_myArrayCollection.sort = m_sort;
//如果不執行refresh,啥事都不發生
m_myArrayCollection.refresh();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章