分页改变了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();