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

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

问题描述

我有一个包含如下数据的数据网格:

I have a datagrid with data like this:

Column1    第2列
    1           10
              11
    3           10
    4           10

Column1     Column2
    1                 10
    2                 11
    3                 10
    4                 10

当点击Column2并使用默认排序时,数据网格变成这样:

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

Column1    第2列
    3           10
    1           10
    4           10
              11

Column1     Column2
    3                 10
    1                 10
    4                 10
    2                 11

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

Why's that?I suppose it should be:

Column1    第2列
    1           10
    3           10
    4           10
    2           11

Column1     Column2
    1                 10
    3                 10
    4                 10
    2                 11

默认排序有什么问题?谁能告诉我如何解决?

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

谢谢!

推荐答案

这是一个有趣的问题.我查看了它,发现最后 Array.sortOn() 函数被调用了.在该函数的描述中有一行说:

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:

  • 修改数组以反映排序顺序;多个具有相同排序字段的元素以无特定顺序连续放置在已排序数组中.

无法查看代码,我会做出有根据的猜测,选择不保留项目的先前顺序是由它的效率成本决定的,并且在大多数情况下没有真正的理由来保持顺序.这就是默认的 1 列排序所反映的行为.

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>

排序函数:

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天全站免登陆