对 adg 中的日期进行排序

发布于 2024-10-30 04:11:24 字数 3041 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋凉 2024-11-06 04:11:24

您可以使用 Object util 执行以下操作:

private function dateSortCompare(itemA:YourObject, itemB:YourObject):int 
{
  var dateA:Date = new Date(Date.parse(itemA.date));
  var dateB:Date = new Date(Date.parse(itemB.date));
  return ObjectUtil.dateCompare(dateA, dateB);
}

然后在您的列中:

 <mx:AdvancedDataGridColumn headerText="Fecha del estudio" dataField="studyDate"  paddingRight="0" textAlign="right" resizable="false" sortCompareFunction="dateSortCompare"/>

或执行 Date.time() 并按照您想要的方式对数字进行排序。

这是伪代码,我不知道它是否可以编译,但是它应该让您知道如何去做。祝你好运 :)

You can use the Object util to do something like:

private function dateSortCompare(itemA:YourObject, itemB:YourObject):int 
{
  var dateA:Date = new Date(Date.parse(itemA.date));
  var dateB:Date = new Date(Date.parse(itemB.date));
  return ObjectUtil.dateCompare(dateA, dateB);
}

and then in your column:

 <mx:AdvancedDataGridColumn headerText="Fecha del estudio" dataField="studyDate"  paddingRight="0" textAlign="right" resizable="false" sortCompareFunction="dateSortCompare"/>

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文