对 adg 中的日期进行排序
我有一个 AdvancedDataGrid,我想根据一个 AdvancedDataGridColumn(studyDate) 对 adg 进行排序,该 AdvancedDataGridColumn 使用 DD/MM/YYYY 格式的字符串(我从服务器收到这样的信息):
<mx:AdvancedDataGrid id="myADG" width="100%" height="100%" color="0x323232" dataProvider="{_currentDatosBusqueda}" verticalScrollPolicy="auto"
fontSize="11" fontFamily="Arial" fontStyle="normal" fontWeight="normal" doubleClickEnabled="true"
itemDoubleClick="dobleClickFilaDataGridBusqueda(event);" useRollOver="true"
>
<mx:AdvancedDataGridColumn headerText="Fecha del estudio" dataField="studyDate" paddingRight="0" textAlign="right" resizable="false"/>
这是我用来排序的函数:
private function sortData():void {
var sort:Sort = new Sort();
var sortField:SortField = new SortField("studyDate", true, true);
var sortField2:SortField = new SortField("studyTime", true, false);
sort.fields = [sortField, sortField2];
_currentDatosBusqueda.sort = sort;
_currentDatosBusqueda.refresh();
}
但是它只排序参加当天,我的意思是:
12/02/2011
23/03/2011
25/02/2011
排序如下:
25/02/2011
23/03/2011
12/02/2011
我尝试使用 sortCompareFunction 但它不起作用(可能是我'我做错了)所以有人可以给我任何想法吗???
预先感谢
编辑:
最后我找到了我的问题,我只是尝试使用 DD/MM/YYYY 格式对日期进行排序,因此我需要在排序之前转换为 MM/dd/yyyy 格式。这是我制作的函数:
private function date_sortCompareFunc(itemA:Object, itemB:Object, fields:Array = null):int {
var year:int = int(itemA.studyDate.substr(6,4));
var month:int = int(itemA.studyDate.substr(3,2))-1;
var day:int = int(itemA.studyDate.substr(0,2));
var dateA : Date = new Date(year, month, day);
year = int(itemB.studyDate.substr(6,4));
month = int(itemB.studyDate.substr(3,2))-1;
day = int(itemB.studyDate.substr(0,2));
var dateB : Date = new Date(year, month, day);
// return ObjectUtil.dateCompare(dateA, dateB);
return ( dateA.valueOf() > dateB.valueOf() ) ? 1 : ( dateA.valueOf() < dateB.valueOf() ) ? -1 : 0;
}
但我发现另一个问题,我需要在第一次显示时对 adg 的列进行排序,所以我使用了以下函数,但它没有从最近一天到旧一天排序,我不知道我能做什么,因为我设置了 sort.descending= true,有什么想法吗?
private function sortData():void {
var sort:Sort = new Sort();
var sortField:SortField = new SortField("studyDate", true, true);
sortField.descending = true;
var sortField2:SortField = new SortField("studyTime", true, false);
sort.fields = [sortField, sortField2];
sort.compareFunction = date_sortCompareFunc;
_currentDatosBusqueda.sort = sort;
_currentDatosBusqueda.refresh();
}
I have an advancedDataGrid and I would like to sort the adg according one AdvancedDataGridColumn(studyDate) which use strings in format DD/MM/YYYY(I receive like this from the server):
<mx:AdvancedDataGrid id="myADG" width="100%" height="100%" color="0x323232" dataProvider="{_currentDatosBusqueda}" verticalScrollPolicy="auto"
fontSize="11" fontFamily="Arial" fontStyle="normal" fontWeight="normal" doubleClickEnabled="true"
itemDoubleClick="dobleClickFilaDataGridBusqueda(event);" useRollOver="true"
>
<mx:AdvancedDataGridColumn headerText="Fecha del estudio" dataField="studyDate" paddingRight="0" textAlign="right" resizable="false"/>
And this is the function I use to sort:
private function sortData():void {
var sort:Sort = new Sort();
var sortField:SortField = new SortField("studyDate", true, true);
var sortField2:SortField = new SortField("studyTime", true, false);
sort.fields = [sortField, sortField2];
_currentDatosBusqueda.sort = sort;
_currentDatosBusqueda.refresh();
}
But it only sort attendind the day, I mean:
12/02/2011
23/03/2011
25/02/2011
It sorts like this:
25/02/2011
23/03/2011
12/02/2011
I try using a sortCompareFunction but it does not work(probably I'm doing wrong) so can someone give me any idea???
Thanks in advance
EDIT:
Finally I find my problem, I just tried to sort dates with the format DD/MM/YYYY so I need to convert to MM/dd/yyyy format before sorting. This is the function I make:
private function date_sortCompareFunc(itemA:Object, itemB:Object, fields:Array = null):int {
var year:int = int(itemA.studyDate.substr(6,4));
var month:int = int(itemA.studyDate.substr(3,2))-1;
var day:int = int(itemA.studyDate.substr(0,2));
var dateA : Date = new Date(year, month, day);
year = int(itemB.studyDate.substr(6,4));
month = int(itemB.studyDate.substr(3,2))-1;
day = int(itemB.studyDate.substr(0,2));
var dateB : Date = new Date(year, month, day);
// return ObjectUtil.dateCompare(dateA, dateB);
return ( dateA.valueOf() > dateB.valueOf() ) ? 1 : ( dateA.valueOf() < dateB.valueOf() ) ? -1 : 0;
}
But i find another problem, I need to sort the columns of the adg the first time it displays so I used the following function but It doesn't sort from the recent day to old one, I do not know what I can I do cause I set sort.descending= true, any ideas?
private function sortData():void {
var sort:Sort = new Sort();
var sortField:SortField = new SortField("studyDate", true, true);
sortField.descending = true;
var sortField2:SortField = new SortField("studyTime", true, false);
sort.fields = [sortField, sortField2];
sort.compareFunction = date_sortCompareFunc;
_currentDatosBusqueda.sort = sort;
_currentDatosBusqueda.refresh();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Object util 执行以下操作:
然后在您的列中:
或执行 Date.time() 并按照您想要的方式对数字进行排序。
这是伪代码,我不知道它是否可以编译,但是它应该让您知道如何去做。祝你好运 :)
You can use the Object util to do something like:
and then in your column:
or do the Date.time() and sort the numbers wich ever way you want..
This is pseudo code, I have no idea if it compiles, but it should give you an idea of how to go about it. Good luck :)