Sorting 在flex中使用数组集合上的点对项目编号进行排序
我需要在Flex 3.5中的datagrid上按项目编号订购一个项目数组 一旦它进入datagrid,我实际上不需要重新排序,我只需要在将它发送到dataprovider之前在arraycollection上对它进行排序 我的问题是我需要排序的适当的“item_number”是一个字符串,它由批号和一个点组成,如下所示: 1.1, 1.2, 1.3, 2.1, 2.2, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 我需要像这样订购 如果我试着按数字订购,3.2比3.11大,所以它不会起作用。我需要先按点前的整数排序,然后再按点后的整数排序,然后再转到点前的下一个整数 我还有另一个问题。item_number属性位于my arraycollection上另一个对象内的对象内 要实现这一目标,我必须: 数组\集合.item.item\编号 总而言之,我需要列出一个数组,它是由arrayitem的另一个对象中的属性排序的,它是一个以点分隔的字符串上的数字 这是我的代码的简化版本:Sorting 在flex中使用数组集合上的点对项目编号进行排序,sorting,apache-flex,datagrid,flex3,arraycollection,Sorting,Apache Flex,Datagrid,Flex3,Arraycollection,我需要在Flex 3.5中的datagrid上按项目编号订购一个项目数组 一旦它进入datagrid,我实际上不需要重新排序,我只需要在将它发送到dataprovider之前在arraycollection上对它进行排序 我的问题是我需要排序的适当的“item_number”是一个字符串,它由批号和一个点组成,如下所示: 1.1, 1.2, 1.3, 2.1, 2.2, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 我需要像这样订
<mx:Script>
<![CDATA[
public function print_data_grid(array_collection):void
{
my_data_grid.dataProvider = array_collection
}
]]>
</mx:Script>
<mx:DataGrid id="my_data_grid">
<mx:columns>
<mx:DataGridColumn headerText="# Item">
<mx:itemRenderer>
<mx:Component>
<mx:Label toolTip="{this.text}" text="{data.product.item_number}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Item Name">
<mx:itemRenderer>
<mx:Component>
<mx:Label toolTip="{this.text}" text="{data.product.name}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
您可以编写自定义排序函数。这里有一个例子。请检查它是否满足您的要求
<?xml version="1.0"?>
<s:Application 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.collections.Sort;
import mx.utils.ObjectUtil;
private var myArrayCollection:ArrayCollection = new ArrayCollection([
{product: {item_number: "1.1", name: "A"}},
{product: {item_number: "10.2", name: "Lottery"}},
{product: {item_number: "10.11", name: "Book"}},
{product: {item_number: "1.13", name: "DVD"}},
{product: {item_number: "1.221", name: "Car"}},
{product: {item_number: "1.211", name: "Mobile"}},
{product: {item_number: "10.1111", name: "Laptop"}},
{product: {item_number: "11.1", name: "Camera"}},
{product: {item_number: "12.1", name: "Desktop"}},
{product: {item_number: "144.41", name: "iPad"}},
{product: {item_number: "14.21", name: "Tablet"}},
{product: {item_number: "14.111", name: "Android phone"}},
{product: {item_number: "10.1", name: "TV"}},
{product: {item_number: "10.100", name: "Bulb"}}]);
private function createCopyOfArrayCollection():ArrayCollection
{
var copyOfArrayCollection:ArrayCollection = new ArrayCollection();
for(var i = 0; i < myArrayCollection.length; i++)
{
copyOfArrayCollection.addItem(mx.utils.ObjectUtil.copy(myArrayCollection[i]));
}
return copyOfArrayCollection;
}
private function onButtonClick():void {
var copyOfArrayCollection:ArrayCollection = createCopyOfArrayCollection();
var sort:Sort = new Sort();
sort.compareFunction = sortFunction;
copyOfArrayCollection.sort = sort;
copyOfArrayCollection.refresh();
print_data_grid(copyOfArrayCollection);
}
private function sortFunction(a:Object, b:Object, array:Array = null):int {
//assuming all item_number contains one decimal
var itemNumberA:String = a.product.item_number;
var itemNumberASplitArray:Array = itemNumberA.split(".", 2);
var itemNumberB:String = b.product.item_number;
var itemNumberBSplitArray:Array = itemNumberB.split(".", 2);
if (Number(itemNumberASplitArray[0]) == Number(itemNumberBSplitArray[0])) {
if (Number(itemNumberASplitArray[1]) == Number(itemNumberBSplitArray[1])) {
return 0;
}
else if (Number(itemNumberASplitArray[1]) > Number(itemNumberBSplitArray[1])) {
return 1;
}
else {
return -1;
}
}
else if (Number(itemNumberASplitArray[0]) > Number(itemNumberBSplitArray[0])) {
return 1;
}
else {
return -1;
}
}
public function print_data_grid(array_collection):void {
my_data_grid2.dataProvider = array_collection
}
]]>
</fx:Script>
<mx:HBox verticalCenter="0" horizontalCenter="0">
<mx:Panel title="Unsorted Data">
<mx:DataGrid id="my_data_grid" dataProvider="{myArrayCollection}" height="400">
<mx:columns>
<mx:DataGridColumn headerText="# Item">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="{this.text}" text="{data.product.item_number}"/>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Item Name">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="{this.text}" text="{data.product.name}"/>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
<mx:VBox height="100%">
<mx:Spacer percentHeight="50"/>
<mx:Button label=">>- Show sorted data ->>" click="{onButtonClick()}"/>
<mx:Spacer percentHeight="50"/>
</mx:VBox>
<mx:Panel title="Sorted Data">
<mx:DataGrid id="my_data_grid2" height="400">
<mx:columns>
<mx:DataGridColumn headerText="# Item">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="{this.text}" text="{data.product.item_number}"/>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Item Name">
<mx:itemRenderer>
<fx:Component>
<mx:Label toolTip="{this.text}" text="{data.product.name}"/>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:HBox>
</s:Application>
编号(itemNumberBSplitArray[1])){
返回1;
}
否则{
返回-1;
}
}
else if(Number(itemnumbersplitarray[0])>Number(itemnumbersplitarray[0])){
返回1;
}
否则{
返回-1;
}
}
公共函数打印数据网格(数组集合):无效{
my_data_grid2.dataProvider=数组_集合
}
]]>
参见