Flex XML的属性排序

效果预览:

经过一番研究搞定了,这方法的效率不怎么样,望高人指点!,如:其中id与type是employee的属性值,实现原理是将XML的属性值取出来存为数组,然后按你想要的排序去重排那个数组,再循环条件将XML的属性值与数组排序后的值对比,重新赋给新的XML或者XMLList.

xmlSort.mxml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" creationComplete="init()">
  3.     <mx:Script>
  4.         <![CDATA[
  5.             import mx.events.DropdownEvent;
  6.            
  7.             [Bindable]
  8.             public var dataXML:XMLList;
  9.             private function init():void
  10.             {
  11.                 dataXML = new XMLList(xmlsource);
  12.             }
  13.            
  14.             private function closeHandler(event:DropdownEvent):void
  15.             {
  16.                 var arr:Array = new Array();
  17.                 var temp:XMLList = new XMLList();
  18.                 //trace(dataXML.length());
  19.                 var str:String = event.target.text.substr(6, 4);
  20.                 //将属性值存为数组排序,然后根据条件重赋XMLList
  21.                 switch(str)
  22.                 {
  23.                     case "id":   
  24.                     for (var i:int = 0; i < dataXML.length(); i++)
  25.                     {
  26.                         arr.push(dataXML[i].@id);
  27.                     }
  28.                     arr.sort();
  29.                     break;
  30.                     case "type":
  31.                     for (i = 0; i < dataXML.length(); i++)
  32.                     {
  33.                         arr.push(dataXML[i].@type);
  34.                     }
  35.                     arr.sort();
  36.                     break;
  37.                     default:
  38.                     break;
  39.                 }
  40.                 for (i = 0; i < arr.length; i++)
  41.                 {
  42.                     for (var j:int = 0; j < dataXML.length(); j++)
  43.                     {
  44.                         if (dataXML[j].@id == arr[i] || dataXML[j].@type == arr[i])
  45.                         {
  46.                             temp += dataXML[j];
  47.                         }
  48.                     }
  49.                 }
  50.                 dg.dataProvider = temp;
  51.             }
  52.            
  53.             private function reset():void
  54.             {
  55.                 dg.dataProvider = dataXML;
  56.             }
  57.            
  58.             private function labelNameFun(item:String):String
  59.             {
  60.                 if (item == "id")
  61.                 {
  62.                     return "排序属性: " + item;
  63.                 }
  64.                 else
  65.                 {
  66.                     return "排序属性: " + item;
  67.                 }
  68.             }
  69.         ]]>
  70.     </mx:Script>
  71.    
  72.     <mx:XMLList id="xmlsource">
  73.         <employee id="3" type="a">
  74.             <name sex="男">阿健</name>
  75.             <phone>13517093949</phone>
  76.             <chinese>90</chinese>
  77.             <math>92</math>
  78.         </employee>
  79.         <employee id="1" type="d">
  80.             <name sex="">阿B</name>
  81.             <phone>13990939499</phone>
  82.             <chinese>85</chinese>
  83.             <math>76</math>
  84.         </employee>
  85.         <employee id="2" type="e">
  86.             <name sex="女">阿雪</name>
  87.             <phone>13826826828</phone>
  88.             <chinese>87</chinese>
  89.             <math>90</math>
  90.         </employee>
  91.         <employee id="4" type="b">
  92.             <name sex="">晴晴</name>
  93.             <phone>13888888888</phone>
  94.             <chinese>90</chinese>
  95.             <math>92</math>
  96.         </employee>
  97.         <employee id="5" type="c">
  98.             <name sex="男">阿轩</name>
  99.             <phone>13936973689</phone>
  100.             <chinese>81</chinese>
  101.             <math>83</math>
  102.         </employee>
  103.     </mx:XMLList>
  104.    
  105.     <mx:Panel title="XML Attribute Sort" height="100%" width="100%"
  106.         paddingTop="5" paddingLeft="10" paddingRight="10" paddingBottom="5" layout="horizontal">
  107.        
  108.         <mx:DataGrid id="dg" width="450" height="155" rowCount="5" dataProvider="{dataXML}">
  109.             <mx:columns>
  110.                 <mx:DataGridColumn dataField="@id" headerText="ID" width="40" />
  111.                 <mx:DataGridColumn dataField="@type" headerText="Type" width="40" />
  112.                 <mx:DataGridColumn dataField="name" headerText="Name" width="65"/>
  113.                 <mx:DataGridColumn dataField="name.@sex" headerText="Math" width="40" />
  114.                 <mx:DataGridColumn dataField="phone" headerText="Phone" width="95" />
  115.                 <mx:DataGridColumn dataField="chinese" headerText="Chinese" width="55"/>
  116.                 <mx:DataGridColumn dataField="math" headerText="Math" width="45" />
  117.             </mx:columns>
  118.         </mx:DataGrid>
  119.        
  120.         <mx:Canvas width="200" height="100%">
  121.             <mx:ComboBox x="10" y="45" dataProvider="['id','type']" labelFunction="labelNameFun" close="closeHandler(event)" />
  122.             <mx:Button label="原排序" x="130" y="45" click="reset()" />
  123.         </mx:Canvas>
  124.     </mx:Panel>
  125. </mx:Application>
发表于: 02-02-10 · 2 评论 »  1,793 次阅读

2 评论 致 《Flex XML的属性排序》

  1. jolie 发表:

    效果不错

    四月 28th, 2010 at 10:08
  2. pingge 发表:

    很好正在学习。

    七月 15th, 2010 at 08:21

评论

兄弟,没有中文内容的评论一律过滤!