【问题标题】:Flex DataGrid column sorting has no effectFlex DataGrid 列排序没有效果
【发布时间】:2017-03-04 19:10:38
【问题描述】:

我想按字母顺序对这些关联进行排序。 标签函数位于列获取值的位置下方。

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 但没有效果

<mx:DataGridColumn
headerText="Service [Si_Service_Id]"
editable="false"
dataField="service"
labelFunction="setServiceColumnLabel"
sortabledecsending=true
sortable="true"/>

我也尝试过使用 sortCompareFunction。

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>

但这也没有效果。 请建议我在哪里失踪。提前致谢。

【问题讨论】:

    标签: apache-flex adobe flex3


    【解决方案1】:

    你几乎是对的,只是你错过了补充一件事:

    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 排序。请参阅下面的结果代码:

    <?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>
    

    如果你想应用默认排序:

     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();
        }
    

    所以结果代码将是:

    <?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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这不是直截了当的,因为我们是从我一开始编写的标签函数中获取值所以我不知道如何对其进行排序,如何将其设置为数据提供者。
      • 在这种情况下,您可以在模型中创建一个标签属性,在解析数据时使用要在列中显示的字符串填充它,并使用它来排序而不是使用标签函数。这里我假设AssociationModel(item).service的值不会动态变化
      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 2012-03-20
      • 2011-08-19
      • 2010-10-17
      • 2013-05-21
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多