Flex XML的属性排序
效果预览:
经过一番研究搞定了,这方法的效率不怎么样,望高人指点!,如:
xmlSort.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" creationComplete="init()">
- <mx:Script>
- <![CDATA[
- import mx.events.DropdownEvent;
- [Bindable]
- public var dataXML:XMLList;
- private function init():void
- {
- dataXML = new XMLList(xmlsource);
- }
- private function closeHandler(event:DropdownEvent):void
- {
- var arr:Array = new Array();
- var temp:XMLList = new XMLList();
- //trace(dataXML.length());
- var str:String = event.target.text.substr(6, 4);
- //将属性值存为数组排序,然后根据条件重赋XMLList
- switch(str)
- {
- case "id":
- for (var i:int = 0; i < dataXML.length(); i++)
- {
- arr.push(dataXML[i].@id);
- }
- arr.sort();
- break;
- case "type":
- for (i = 0; i < dataXML.length(); i++)
- {
- arr.push(dataXML[i].@type);
- }
- arr.sort();
- break;
- default:
- break;
- }
- for (i = 0; i < arr.length; i++)
- {
- for (var j:int = 0; j < dataXML.length(); j++)
- {
- if (dataXML[j].@id == arr[i] || dataXML[j].@type == arr[i])
- {
- temp += dataXML[j];
- }
- }
- }
- dg.dataProvider = temp;
- }
- private function reset():void
- {
- dg.dataProvider = dataXML;
- }
- private function labelNameFun(item:String):String
- {
- if (item == "id")
- {
- return "排序属性: " + item;
- }
- else
- {
- return "排序属性: " + item;
- }
- }
- ]]>
- </mx:Script>
- <mx:XMLList id="xmlsource">
- <employee id="3" type="a">
- <name sex="男">阿健</name>
- <phone>13517093949</phone>
- <chinese>90</chinese>
- <math>92</math>
- </employee>
- <employee id="1" type="d">
- <name sex="男">阿B</name>
- <phone>13990939499</phone>
- <chinese>85</chinese>
- <math>76</math>
- </employee>
- <employee id="2" type="e">
- <name sex="女">阿雪</name>
- <phone>13826826828</phone>
- <chinese>87</chinese>
- <math>90</math>
- </employee>
- <employee id="4" type="b">
- <name sex="女">晴晴</name>
- <phone>13888888888</phone>
- <chinese>90</chinese>
- <math>92</math>
- </employee>
- <employee id="5" type="c">
- <name sex="男">阿轩</name>
- <phone>13936973689</phone>
- <chinese>81</chinese>
- <math>83</math>
- </employee>
- </mx:XMLList>
- <mx:Panel title="XML Attribute Sort" height="100%" width="100%"
- paddingTop="5" paddingLeft="10" paddingRight="10" paddingBottom="5" layout="horizontal">
- <mx:DataGrid id="dg" width="450" height="155" rowCount="5" dataProvider="{dataXML}">
- <mx:columns>
- <mx:DataGridColumn dataField="@id" headerText="ID" width="40" />
- <mx:DataGridColumn dataField="@type" headerText="Type" width="40" />
- <mx:DataGridColumn dataField="name" headerText="Name" width="65"/>
- <mx:DataGridColumn dataField="name.@sex" headerText="Math" width="40" />
- <mx:DataGridColumn dataField="phone" headerText="Phone" width="95" />
- <mx:DataGridColumn dataField="chinese" headerText="Chinese" width="55"/>
- <mx:DataGridColumn dataField="math" headerText="Math" width="45" />
- </mx:columns>
- </mx:DataGrid>
- <mx:Canvas width="200" height="100%">
- <mx:ComboBox x="10" y="45" dataProvider="['id','type']" labelFunction="labelNameFun" close="closeHandler(event)" />
- <mx:Button label="原排序" x="130" y="45" click="reset()" />
- </mx:Canvas>
- </mx:Panel>
- </mx:Application>
发表于: 02-02-10 · 2 评论 » 1,793 次阅读
效果不错
四月 28th, 2010 at 10:08很好正在学习。
七月 15th, 2010 at 08:21