对XMLListCollection进行排序

时间:2012-03-28 23:21:38

标签: flash flex

我一直试图按照像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>

感谢任何和所有帮助以使其排序。

编辑

这是一个照相馆,我希望能够根据位置显示图像 - 在这种情况下,加拿大,美国,英国等等。感谢输入!

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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 item.@location == 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="{imageList.selectedItem.@image}" 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="{data.@thumb}"/>
                </mx:Component>
            </mx:itemRenderer>
        </mx:HorizontalList>    
    </mx:VBox>
    <mx:ComboBox id="countryCollection" dataProvider="{_countryCollection}" change="filterByLocation(event)"/>
</mx:Application>