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>

4

2 回答 2

2

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

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

于 2011-09-14T14:54:04.697 回答
1

您是否尝试过使用 Dictionary 对象,而不是做一堆 if(..),将 id 设置为键,将名称设置为值?如果我没记错的话,对字典的访问是使用哈希函数完成的,并且比一系列 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>
于 2011-09-14T14:46:40.190 回答