首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flex DataGrid列排序不起作用

Flex DataGrid列排序不起作用
EN

Stack Overflow用户
提问于 2017-03-05 03:10:38
回答 2查看 168关注 0票数 1

我想按字母顺序对这些关联进行排序。Label函数如下所示,列是从其中获取值的。

代码语言:javascript
复制
private function setColumnLabel(item:Object, col:*):String
    {
        if(AssociationModel(item).service != null)
            return 
                    AssociationModel(item).service.serviceName
                    +"["+AssociationMoel(item).service.siServiceId+"]";           
        else
            return "";
 }



 <mx:DataGrid id="subLinearAssocGridInView" 
    width="600"
    top="30"
    left="12"
    editable="false"
    maxHeight="500"
    rowHeight="20"
    headerHeight="20">
<mx:columns>
<mx:DataGridColumn [Si_Service_Id]"
            headerText="Service                 
            editable="false"
            dataField="service"
            labelFunction="setColumnLabel"
            sortable="true"/>
</mx:columns>
</mx:DataGrid>

在这里,我尝试了sortabledecsending=true和sortabledecsending=true,但没有效果

代码语言:javascript
复制
<mx:DataGridColumn
headerText="Service [Si_Service_Id]"
editable="false"
dataField="service"
labelFunction="setServiceColumnLabel"
sortabledecsending=true
sortable="true"/>

我也尝试过使用sortCompareFunction。

代码语言:javascript
复制
public function doSortForField(field:String):Function
 {
 return function(obj1:Object, obj2:Object):int 
  {
return mx.utils.ObjectUtil.stringCompare(obj1[field],obj2[field],true);
                    }


   <mx:DataGrid>
     <mx:columns>
     <mx:DataGridColumn
     sortCompareFunction=”doSortForField(‘service’)”                    
     headerText=”service” dataField=”service” />
     </mx:columns>
     </mx:DataGrid>

但这也没有效果。请给出我缺少的地方。提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-05 17:25:46

你几乎是对的,只是你遗漏了一件事:

代码语言:javascript
复制
public function doSortForField(field:String, property:String):Function
    {
        return function (obj1:Object, obj2:Object):int
        {
            return mx.utils.ObjectUtil.stringCompare(obj1[field][property], obj2[field][property], true);
        }
    }

但是默认情况下,它仍然不会排序--您必须单击列标题来对其进行dec/acc排序。请看下面的结果代码:

代码语言:javascript
复制
<?xml version="1.0"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:mx="library://ns.adobe.com/flex/mx"
            creationComplete="creationCompleteHandler(event)">
<mx:DataGrid id="subLinearAssocGridInView"
             width="600"
             top="30"
             left="12"
             editable="false"
             maxHeight="500"
             rowHeight="20"
             headerHeight="20"
             sortableColumns="true"
             >
    <mx:columns>
        <mx:DataGridColumn
        headerText="Service"
        editable="false"
        dataField="service"
        labelFunction="setColumnLabel"
        sortCompareFunction="doSortForField('service', 'serviceName')"
        />
    </mx:columns>
</mx:DataGrid>

<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import mx.events.FlexEvent;
    import mx.utils.ObjectUtil;

    private function creationCompleteHandler(event:FlexEvent):void
    {
        const mockData:ArrayCollection = new ArrayCollection([
            {service: {serviceName: "service1", siServiceId: "serviceId1"}},
            {service: {serviceName: "service2", siServiceId: "serviceId2"}},
            {service: {serviceName: "service3", siServiceId: "serviceId3"}},
            {service: {serviceName: "service4", siServiceId: "serviceId4"}}
        ]);
        subLinearAssocGridInView.dataProvider = mockData;
    }

    private function setColumnLabel(item:Object, col:*):String
    {
        if (item.service != null)
        {
            return item.service.serviceName + "[" + item.service.siServiceId + "]";
        }
        else
        {
            return "";
        }
    }

    public function doSortForField(field:String, property:String):Function
    {
        return function (obj1:Object, obj2:Object):int
        {
            return mx.utils.ObjectUtil.stringCompare(obj1[field][property], obj2[field][property], true);
        }
    }

    ]]></fx:Script>

如果您想要应用默认排序:

代码语言:javascript
复制
 private function subLinearAssocGridInView_creationCompleteHandler(event:FlexEvent):void
    {
        mockData.sort = new Sort();
        mockData.sort.fields = [
            new SortField('serviceName', true, true, false, null, doSortForField('service', 'serviceName'))
        ];
        mockData.refresh();
    }

因此,得到的代码将是:

代码语言:javascript
复制
<?xml version="1.0"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:mx="library://ns.adobe.com/flex/mx">
<mx:DataGrid id="subLinearAssocGridInView"
             dataProvider="{mockData}"
             width="600"
             top="30"
             left="12"
             editable="false"
             maxHeight="500"
             rowHeight="20"
             headerHeight="20"
             creationComplete="subLinearAssocGridInView_creationCompleteHandler(event)"
             >
    <mx:columns>
        <mx:DataGridColumn
        headerText="Service"
        editable="false"
        labelFunction="setColumnLabel"
        sortCompareFunction="doSortForField()"
        />
    </mx:columns>
</mx:DataGrid>

<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import mx.collections.Sort;
    import mx.collections.SortField;
    import mx.events.FlexEvent;
    import mx.utils.ObjectUtil;

    private static const SERVICE_FIELD:String = 'service';
    private static const SERVICE_NAME_PROPERTY:String = 'serviceName';

    [Bindable]
    public var mockData:ArrayCollection = new ArrayCollection([
        {service: {serviceName: "service1", siServiceId: "serviceId1"}},
        {service: {serviceName: "service2", siServiceId: "serviceId2"}},
        {service: {serviceName: "service3", siServiceId: "serviceId3"}},
        {service: {serviceName: "service4", siServiceId: "serviceId4"}}
    ]);

    private function setColumnLabel(item:Object, col:*):String
    {
        if (item.service != null)
        {
            return item.service.serviceName + "[" + item.service.siServiceId + "]";
        }
        else
        {
            return "";
        }
    }

    public function doSortForField(field:String = SERVICE_FIELD, property:String = SERVICE_NAME_PROPERTY):Function
    {
        return function (obj1:Object, obj2:Object):int
        {
            return mx.utils.ObjectUtil.stringCompare(obj1[field][property], obj2[field][property], true);
        }
    }

    private function subLinearAssocGridInView_creationCompleteHandler(event:FlexEvent):void
    {
        mockData.sort = new Sort();
        mockData.sort.fields = [
            new SortField(SERVICE_NAME_PROPERTY, true, true, false, null, doSortForField())
        ];
        mockData.refresh();
    }

    ]]></fx:Script>

票数 0
EN

Stack Overflow用户

发布于 2017-03-05 14:25:31

您是否尝试过对dataProvider内容进行排序?如果您使用的是数组,则使用Array.sort()。如果您使用的是ArrayCollection,则使用其sort字段

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42600161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档