Flex 中的 labelFunction 和 sortCompareFunction 以及大数据集

发布于 2024-12-04 13:34:54 字数 2300 浏览 4 评论 0原文

好的,下面是我必须做的事情的简化示例。

到目前为止一切顺利,A1 工作正常。 ID 被好友姓名替换,并且该列是可排序的。

现在,我必须将其应用于包含数千个 ID 和数千行的系统。

我尝试了一下,呜呜呜,太慢了,不可能向客户交付这样的东西……

在您看来,实现相同目标的最佳方法是什么?

我唯一的想法是不只将 ID 存储在数据库中,而是将名称也存储为字符串...我只是认为这是我不应该存储的信息...

有人有想法吗?另一种对呈现的字符串进行排序而不必调用每行上的 fId.labelFunction(obj1, fId) 的方法?

多谢!

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        private var _friendList:ArrayCollection = new ArrayCollection([
            {friend_id : 1},
            {friend_id : 3},
            {friend_id : 2},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 3}
        ]);

        private function friendNameFromID(item:Object, column:DataGridColumn):String
        {
            var id:int = item[column.dataField];

            if (id == 1)
                return "Thomas";

            if (id == 2)
                return "Anthony";

            if (id == 3)
                return "George"

            return "";
        }

        private function sortFromFriendName(obj1:Object, obj2:Object):int
        {
            var value1:String = fId.labelFunction(obj1, fId);
            var value2:String = fId.labelFunction(obj2, fId);

            if (value1 == value2)
                return 0;
            else if (value1 > value2)
                return 1;
            else
                return -1;
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<mx:DataGrid id="myDataGrid" dataProvider="{_friendList}" width="90%" height="90%" horizontalCenter="0" verticalCenter="0">
    <mx:columns>
        <mx:DataGridColumn dataField="friend_id"/>
        <mx:DataGridColumn id="fId" dataField="friend_id" labelFunction="friendNameFromID" sortCompareFunction="sortFromFriendName"/>
    </mx:columns>
</mx:DataGrid>

OK, below is a simplified example of what I have to do.

So far so good, works A1. The IDs are replaced by the friend name, and the column is sortable.

Now, I have to apply this to a system containing thousands of IDs and thousands of rows.

I tried it and wooooooooooo, it is so slow, impossible to deliver something like this to a client...

What would be, in your opinion, the best approach to achieve the same goal?

The only idea I had is instead of storing only the ID in the DB, to store the names as strings too... I just thing it is information I shouldn't have to store...

Anybody have an idea? Another way to sort the rendered string instead of having to recall the fId.labelFunction(obj1, fId) on each row?

THANKS A LOT!

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        [Bindable]
        private var _friendList:ArrayCollection = new ArrayCollection([
            {friend_id : 1},
            {friend_id : 3},
            {friend_id : 2},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 3}
        ]);

        private function friendNameFromID(item:Object, column:DataGridColumn):String
        {
            var id:int = item[column.dataField];

            if (id == 1)
                return "Thomas";

            if (id == 2)
                return "Anthony";

            if (id == 3)
                return "George"

            return "";
        }

        private function sortFromFriendName(obj1:Object, obj2:Object):int
        {
            var value1:String = fId.labelFunction(obj1, fId);
            var value2:String = fId.labelFunction(obj2, fId);

            if (value1 == value2)
                return 0;
            else if (value1 > value2)
                return 1;
            else
                return -1;
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<mx:DataGrid id="myDataGrid" dataProvider="{_friendList}" width="90%" height="90%" horizontalCenter="0" verticalCenter="0">
    <mx:columns>
        <mx:DataGridColumn dataField="friend_id"/>
        <mx:DataGridColumn id="fId" dataField="friend_id" labelFunction="friendNameFromID" sortCompareFunction="sortFromFriendName"/>
    </mx:columns>
</mx:DataGrid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

禾厶谷欠 2024-12-11 13:34:54

对数据进行分页怎么样?过滤所有这些行不仅让您头疼。用户也可以抱怨;)

这只是我发现的第一件事作为示例: Flex Datagrid 中的高级分页和过滤

What about paging your data? It's not only your headache to filter all those bunch of rows. The user also can complain ;)

Here's just a first thing that I've found as example: Advanced Paging and Filtering in Flex Datagrid

_畞蕅 2024-12-11 13:34:54

你是否尝试过使用 Dictionary 对象,将 id 设置为键,将名称设置为值,而不是执行一堆 if(..) ?如果我没记错的话,对字典的访问是使用哈希函数完成的,并且比一系列 if 语句快得多。

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="buildNameDictionary()"
>
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        private var nameDictionary:Dictionary;

        [Bindable]
        private var _friendList:ArrayCollection = new ArrayCollection([
            {friend_id : 1},
            {friend_id : 3},
            {friend_id : 2},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 3}
        ]);

        private function buildNameDictionary():void
        {
            nameDictionary= new Dictionary();
            nameDictionary[1] = "Thomas";
            nameDictionary[2] = "Anthony";
            nameDictionary[3] = "George";
        }

        private function friendNameFromID(item:Object, column:DataGridColumn):String
        {
            if(nameDictionary[item])
                return nameDictionary[item] as String

            return "";
        }

        private function sortFromFriendName(obj1:Object, obj2:Object):int
        {
            var value1:String = fId.labelFunction(obj1, fId);
            var value2:String = fId.labelFunction(obj2, fId);

            if (value1 == value2)
                return 0;
            else if (value1 > value2)
                return 1;
            else
                return -1;
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<mx:DataGrid id="myDataGrid" dataProvider="{_friendList}" width="90%" height="90%" horizontalCenter="0" verticalCenter="0">
    <mx:columns>
        <mx:DataGridColumn dataField="friend_id"/>
        <mx:DataGridColumn id="fId" dataField="friend_id" labelFunction="friendNameFromID" sortCompareFunction="sortFromFriendName"/>
    </mx:columns>
</mx:DataGrid>

Instead of doing a bunch of if(..) have u tried using a Dictionary object, setting the id as the key and the name as the value? If I'm not mistaken, access into a Dictionary is done using a hash function and is much faster than a series of if statements.

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="buildNameDictionary()"
>
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        private var nameDictionary:Dictionary;

        [Bindable]
        private var _friendList:ArrayCollection = new ArrayCollection([
            {friend_id : 1},
            {friend_id : 3},
            {friend_id : 2},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 2},
            {friend_id : 1},
            {friend_id : 3}
        ]);

        private function buildNameDictionary():void
        {
            nameDictionary= new Dictionary();
            nameDictionary[1] = "Thomas";
            nameDictionary[2] = "Anthony";
            nameDictionary[3] = "George";
        }

        private function friendNameFromID(item:Object, column:DataGridColumn):String
        {
            if(nameDictionary[item])
                return nameDictionary[item] as String

            return "";
        }

        private function sortFromFriendName(obj1:Object, obj2:Object):int
        {
            var value1:String = fId.labelFunction(obj1, fId);
            var value2:String = fId.labelFunction(obj2, fId);

            if (value1 == value2)
                return 0;
            else if (value1 > value2)
                return 1;
            else
                return -1;
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<mx:DataGrid id="myDataGrid" dataProvider="{_friendList}" width="90%" height="90%" horizontalCenter="0" verticalCenter="0">
    <mx:columns>
        <mx:DataGridColumn dataField="friend_id"/>
        <mx:DataGridColumn id="fId" dataField="friend_id" labelFunction="friendNameFromID" sortCompareFunction="sortFromFriendName"/>
    </mx:columns>
</mx:DataGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文