flex:如何对GridColumn进行排序

时间:2012-10-12 07:56:41

标签: actionscript-3 flex sorting datagridcolumn

我想对gridcolumn进行排序。这是我的sortCompareFunction代码:

protected function sortCompareFunction(obj1:Object, obj2:Object, gc:GridColumn):int
        {
            collator.ignoreCase=true;
            return collator.compare(obj1[gc.dataField], obj2[gc.dataField]);
        }

和datagrid:

<s:DataGrid id="dataGrid" width="100%" height="100%" borderColor="#CCCCCC" borderVisible="true"
            chromeColor="#CCCCCC" color="#000000" contentBackgroundColor="#8C90BB"
            selectionColor="#D0E4E9" symbolColor="#FFFFFF" sortableColumns="true">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn width="150" dataField="title" headerText="Progetto"
                          sortCompareFunction="sortCompareFunction"></s:GridColumn>
            <s:GridColumn dataField="author" headerText="Stato" width="50"></s:GridColumn>
            <s:GridColumn dataField="newsdate" headerText="Scadenza"></s:GridColumn>
        </s:ArrayList>
    </s:columns>
    <s:AsyncListView list="{FlexGlobals.topLevelApplication.PMProjs.lastResult}"/>
</s:DataGrid>

它不起作用。我没有看到列标题中的箭头,任何内容都已排序。在此先感谢您的帮助!

好的,通过使用Arraycollection:

替换AsyncListView来解决
<s:DataGrid id="dataGrid" width="100%" height="100%" borderColor="#CCCCCC" borderVisible="true"
            chromeColor="#CCCCCC" color="#000000" contentBackgroundColor="#8C90BB"
            selectionColor="#D0E4E9" symbolColor="#FFFFFF">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn width="150" dataField="title" headerText="Progetto" 
                          showDataTips="title"></s:GridColumn>
            <s:GridColumn dataField="author" headerText="Stato" width="50"></s:GridColumn>
            <s:GridColumn dataField="newsdate" headerText="Scadenza"></s:GridColumn>
        </s:ArrayList>
    </s:columns>
    <s:ArrayCollection list="{FlexGlobals.topLevelApplication.PMProjs.lastResult}"/>

没有sortCompareFunction。

2 个答案:

答案 0 :(得分:0)

是。正如RIAstar所建议的那样,将AsyncListView更改为ArrayCollection就可以了。 请注意,asynclist是由Flex生成的数据结构。

另请注意,DataGrid的columns标记内定义的Arraylist不必更改为Arraycollection。有趣。

答案 1 :(得分:0)

protected function sortTtl_changeHandler(event:IndexChangeEvent):void
        {
            switch(event.newIndex)
            {
                case 0:
                {
                    var sort:Sort = new Sort();
                    sort.fields = [new SortField("title", true)];
                    dataGrid.sort = sort;
                    dataGrid.refresh();
                    break;
                }
                case 1:
                {
                    var sort:Sort = new Sort();
                    sort.fields = [new SortField("author", true)];
                    dataGrid.sort = sort;
                    dataGrid.refresh();
                    break;
                }
                case 2:
                {
                    var sort:Sort = new Sort();
                    sort.fields = [new SortField("newsdate", true)];
                    dataGrid.sort = sort;
                    dataGrid.refresh();
                    break;
                }

                default:
                {
                    break;
                }
            }

        }
    <s:DropDownList id="sortTtl" visible="false" width="128"
                                change="sortTtl_changeHandler(event)" dataProvider="{sortData}"
                                labelField="label"/>