2012-03-28 50 views
0

我一直試圖排序一個XMLListCollection類似this指令一段時間,沒有成功。下面是相關的代碼:排序XMLListCollection

<fx:Declarations> 
<s:HTTPService id="photoServ" url="pics.xml" resultFormat="e4x"/> 
<s:XMLListCollection id="photoList" source = "{photoServ.lastResult.photo}"/> 
</fx:Declarations> 

<s:List id="imageList" dataProvider="{photoList}" /> 

我的目標是按位置下面的XML文件進行排序,而不是使用整個XMLListCollection爲List的dataProvider,基於位置的輸入變量。

Pics.XML

<photos> 
<photo title="Picture 1" location="Canada" medium="Photograph" thumb="images/thumbs/Picture1.png" image="images/Picture1.png"/> 
<photo title="Picture 2" location="UK" medium="Photograph" thumb="images/thumbs/Picture2.png" image="images/Picture2.png"/> 
<photo title="Picture 3" location="USA" medium="Photograph" thumb="images/thumbs/Picture3.png" image="images/Picture3.png"/> 
<photo title="Picture 4" location="Canada" medium="Photograph" thumb="images/thumbs/Picture4.png" image="images/Picture4.png"/> 
<photo title="Picture 5" location="USA" medium="Photograph" thumb="images/thumbs/Picture5.png" image="images/Picture5.png"/> 
<photo title="Picture 6" location="UK" medium="Photograph" thumb="images/thumbs/Picture6.png" image="images/Picture6.png"/> 
</photos> 

任何和所有幫助表示讚賞得到這個排序。

編輯

這是一個照相館,我希望能夠基於位置顯示的圖像 - 在這種情況下,加拿大,美國,英國等,對於輸入的感謝!

+0

我不知道,如果你真的想對它進行排序,或者如果你想對其進行過濾。如果前者使用sortFunction,或者使用sortFunction,則使用filterFunction。 – 2012-03-29 01:50:41

+0

好點 - 我只是想根據位置對它進行過濾,就是這樣 - 所以我可以用一個變量輸入位置,並且所有加拿大,英國或美國都會顯示出來。 – SQLiteNoob 2012-03-29 02:24:59

回答

2

您需要做的僅僅

photoList.(@location=="Canada" || @location=="USA"); 

得到的<photo>標籤列表與位置加拿大或美國

關於第二個想法,你想設置的XMLListCollection爲

photoList=new XMLListCollection(photoServ.lastResult.photo.(@location=="Canada" || @location=="USA")); 

編輯

要爲不同位置添加代碼,我們假設您已經使用位置數組填充了DropDownList。假設你的位置排列會像

var locs:ArrayCollection=new ArrayCollection(["USA", "UK", "Canada", /*and others too*/]); 

和你DropDownList(可以稱之爲locationList)有dataProviderlocs

現在,當你想上的位置進行篩選,所有你需要做的是

var lns:Vector.<Object>=locationList.selectedItems; 
var filtered:XMLList=photoServ.lastResult.photo.(lns.indexOf(@location) != -1); 
var photoList=new XMLListCollection(filtered); 
+0

拋出1123錯誤,直到我去'photoList.source。(@ location ==「Canada」)'但它失敗了,因爲我的'imageList' dataProvider。我試着用你建議的行填充一個新的'XMLList',並將它用作另一個'XMLListCollection'的源,用作我的列表的dataProvider - 但它不包含任何數據。 – SQLiteNoob 2012-03-29 15:45:15

+0

啊,我看到了新的建議。它給我一個類型強制錯誤,從'XMLList'到'XMLListCollection' – SQLiteNoob 2012-03-29 16:25:28

+0

'PhotoList.source = photoServ ...'給了我一個'#1065:變量位置未定義'錯誤 – SQLiteNoob 2012-03-29 16:33:11

0

ArrayCollection和XMLListCollection的行爲方式非常類似。 XMLListCollection具有屬性filterfunction和sort,使用它可以輕鬆過濾和排序集合。

<mx:Script> 
     <![CDATA[ 
      import mx.collections.SortField; 
      import mx.collections.Sort; 
      import mx.collections.ArrayCollection; 

      [Bindable] 
      private var _photoXML:XML = 
         <photos> 
          <photo title="Picture 1" location="Canada" medium="Photograph" thumb="assets/images/thumbs/Picture1.jpg" image="assets/images/Picture1.jpg"/> 
          <photo title="Picture 2" location="UK" medium="Photograph" thumb="assets/images/thumbs/Picture2.jpg" image="assets/images/Picture2.jpg"/> 
          <photo title="Picture 3" location="USA" medium="Photograph" thumb="assets/images/thumbs/Picture3.jpg" image="assets/images/Picture3.jpg"/> 
          <photo title="Picture 4" location="Canada" medium="Photograph" thumb="assets/images/thumbs/Picture4.jpg" image="assets/images/Picture4.jpg"/> 
          <photo title="Picture 5" location="USA" medium="Photograph" thumb="assets/images/thumbs/Picture5.jpg" image="assets/images/Picture5.jpg"/> 
          <photo title="Picture 6" location="UK" medium="Photograph" thumb="assets/images/thumbs/Picture6.jpg" image="assets/images/Picture6.jpg"/> 
          <photo title="Picture 7" location="Canada" medium="Photograph" thumb="assets/images/thumbs/Picture2.jpg" image="assets/images/Picture1.jpg"/> 
          <photo title="Picture 8" location="UK" medium="Photograph" thumb="assets/images/thumbs/Picture3.jpg" image="assets/images/Picture2.jpg"/> 
          <photo title="Picture 9" location="Canada" medium="Photograph" thumb="assets/images/thumbs/Picture6.jpg" image="assets/images/Picture4.jpg"/> 
          <photo title="Picture 10" location="UK" medium="Photograph" thumb="assets/images/thumbs/Picture4.jpg" image="assets/images/Picture6.jpg"/> 
         </photos> ; 

      [Bindable] 
      private var _countryCollection:ArrayCollection = new ArrayCollection(["select country","USA", "UK", "Canada"]) 

      private function filterByLocation(event:Event):void { 
       if(countryCollection.selectedIndex > 0){ 
        photoList.filterFunction = filter_ByLocation; 
        photoList.refresh(); 
       }else{ 
        photoList.filterFunction = null; 
        photoList.refresh(); 
       } 
      } 

      private function filter_ByLocation(item:XML):Boolean { 
       return [email protected] == countryCollection.selectedItem; 
      } 

      private function sortByTitle():void { 
       var titleSort:Sort = new Sort(); 
        titleSort.fields = [new SortField('@location', true)]; 

       photoList.sort = titleSort; 
       photoList.refresh(); 
      } 

     ]]> 
    </mx:Script> 

    <mx:XMLListCollection id="photoList" source="{_photoXML.children()}"/> 

    <mx:VBox width="100%" horizontalAlign="center"> 
     <mx:Image source="{[email protected]}" width="800" height="400"/> 
     <mx:HorizontalList id="imageList" dataProvider="{photoList}" labelField="@thumb" width="100%" height="100" 
          columnWidth="130" rowHeight="100" creationComplete="sortByTitle();"> 
      <mx:itemRenderer> 
       <mx:Component> 
        <mx:Image source="{[email protected]}"/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:HorizontalList>  
    </mx:VBox> 
    <mx:ComboBox id="countryCollection" dataProvider="{_countryCollection}" change="filterByLocation(event)"/> 
</mx:Application>