Flex:DataGrid默认排序中的问题 [英] Flex:Problems in DataGrid default sorting

查看:167
本文介绍了Flex:DataGrid默认排序中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid,数据如下:



Column1    Column2

     1                10款
     2                11款
     3                10款
     4                10个



当点击Column2并使用Default Sort时,datagrid变成这样:



Column1     Column2

     3                10款
     1                10款
     4                10款
     2                11款



为什么会这样?我想这应该是:



Column1    ; Column2

     1                10款
     3                10款
     4                10款
     2                11款



默认排序有什么问题?有没有人可以告诉我如何解决?



谢谢!

解决方案

这是一个有趣的问题。我查了一下,发现最终调用了$ code> Array.sortOn()函数。在该功能的描述中,有一行说:




  • 数组经过修改以反映排序顺序;具有相同排序字段的多个元素将连续放置在 中的排序数组中。



没有办法研究代码,我会做一个有根据的猜测,选择不保留以前的项目顺序是由它的效率成本决定的,没有真正的理由来保持订单在多数情况下。这就是默认的1列排序反映的行为。



在你的情况下,一个快速而肮脏的(因为它是一个星期六:))如何获得结果的例子想。
您的数据网格:

 < mx:DataGrid id =DG> 
< mx:columns>
< mx:DataGridColumn dataField =0/>
< mx:DataGridColumn dataField =1sortCompareFunction =testsortCompareFunction/>
< / mx:columns>
< / mx:DataGrid>

排序功能:

  private function testsortCompareFunction(object1:Object,object2:Object):int 
{
if(object1 [1]> object2 [1])return 1;
if(object1 [1]< object2 [1])return -1;
if(object1 [1] == object2 [1])$ ​​b $ b {
if(object1 [0]> object2 [0])return 1;
if(object1 [0]< object2 [0])return -1;
}
return 0;
}


I have a datagrid with data like this:

Column1     Column2
    1                 10
    2                 11
    3                 10
    4                 10

When clicking Column2 and using Default Sort,the datagrid turns into this:

Column1     Column2
    3                 10
    1                 10
    4                 10
    2                 11

Why's that?I suppose it should be:

Column1     Column2
    1                 10
    3                 10
    4                 10
    2                 11

What's wrong with default sorting?Could anyone tell me how to fix it?

Thanks!

解决方案

This is an interesting question. I looked into it and found that in the end Array.sortOn() function is getting called. In the description of that function there's a line saying:

  • The array is modified to reflect the sort order; multiple elements that have identical sort fields are placed consecutively in the sorted array in no particular order.

Having no way to look into the code, I would make an educated guess that the choice of not keeping the previous order of the items is dictated by the efficiency cost of it, and no real reason to keep the order in most cases. And that's the behavior the default 1 column sorting reflects.

In your case, a quick and dirty (as it's a saturday :) ) example of how to get the result you want. Your datagrid:

<mx:DataGrid id="DG" >
    <mx:columns>
        <mx:DataGridColumn  dataField="0"  />
        <mx:DataGridColumn  dataField="1" sortCompareFunction="testsortCompareFunction" />
    </mx:columns>
</mx:DataGrid>

The sort function:

private function testsortCompareFunction(object1:Object,object2:Object):int
        {
            if (object1[1]>object2[1]) return 1;
            if (object1[1]<object2[1]) return -1;
            if (object1[1]==object2[1]) 
            {
                if (object1[0]>object2[0]) return 1;
                if (object1[0]<object2[0]) return -1;
            }
            return 0;
        }

这篇关于Flex:DataGrid默认排序中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆