好的,下面是我必须做的简化示例。
到目前为止一切顺利,作品A1。 ID由朋友名称替换,列可以排序。
现在,我必须将其应用于包含数千个ID和数千行的系统。
我试过了,哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇
在您看来,实现同一目标的最佳方法是什么?
我唯一的想法是不是只在DB中存储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>
答案 0 :(得分:2)
分页数据怎么样?过滤所有那些行不仅是你的头痛。用户也可以投诉;)
答案 1 :(得分:1)
而不是做一堆if(..)你试过使用Dictionary对象,将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>