views:

7054

answers:

3

I have an AdvancedDataGrid that uses customer grouping of data. Not all of the groups will be at the same level in the hierarchy, and groups can contain both groups and members. We have a sort callback, but it's not being called except for groups at the leaf-most levels. See code below for an example -- expand all of the groups, then click the sort column on "date of birth" to get a reverse sort by date of birth. (Oddly, for some unfathomable reason, the first ascending sort works.)

We're not getting called for any of the data that's grouped at the same level as a group member.

How do I fix this?

Thanks.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
     layout="vertical" 
     verticalAlign="middle" 
     backgroundColor="white" >
  <mx:Script>
    <![CDATA[
      import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
      import mx.collections.HierarchicalData;
      import mx.utils.ObjectUtil;

      private var arrData : Array = [
        { name: "User A", dob: "04/14/1980" },
        { name: "User B", dob: "01/02/1975" },
        { name: "Group A", children: [
          { name: "User E", dob: "09/13/1972" },
          { name: "User F", dob: "11/22/1993" }
          ]
        },
        { name: "Group B", children: [
          { name: "Group B1", children: [
            { name: "User I", dob: "01/23/1984" },
            { name: "User J", dob: "11/10/1948" }
            ]
          },
          { name: "User G", dob: "04/09/1989" },
          { name: "User H", dob: "06/20/1963" }
          ]
        },
        { name: "User C", dob: "12/30/1977" },
        { name: "User D", dob: "10/27/1968" }
      ];

      private function date_sortCompareFunc(itemA:Object, itemB:Object):int
      {
        if ( itemA.hasOwnProperty("dob") && itemB.hasOwnProperty("dob"))
        {
          var dateA:Date = new Date(Date.parse(itemA.dob));
          var dateB:Date = new Date(Date.parse(itemB.dob));
          return ObjectUtil.dateCompare(dateA, dateB);
        }
        else if ( itemA.hasOwnProperty("dob"))
        {
          return 1;
        }
        else if (itemB.hasOwnProperty("dob"))
        {
          return -1;
        }
        return ObjectUtil.stringCompare(itemA.name, itemB.name);
      }

      private function date_dataTipFunc(item:Object):String
      {
        if (item.hasOwnProperty("dob"))
        {
          return dateFormatter.format(item.dob);
        }
        return "";
      }

      private function label_dob(item:Object, col:AdvancedDataGridColumn):String
      {
        var dob:String="";
        if(item.hasOwnProperty("dob"))
        {
          dob=item.dob;
        }
        return dob;
      }
    ]]>
  </mx:Script>

  <mx:DateFormatter id="dateFormatter" formatString="MMMM D, YYYY" />

  <mx:AdvancedDataGrid id="adgTest" dataProvider="{new HierarchicalData(this.arrData)}" designViewDataType="tree" width="746" height="400">
    <mx:columns>
      <mx:AdvancedDataGridColumn headerText="Name"  dataField="name"/>
      <mx:AdvancedDataGridColumn dataField="dob" headerText="Date of birth" 
           labelFunction="label_dob" 
           sortCompareFunction="date_sortCompareFunc"
           showDataTips="true" 
           dataTipFunction="date_dataTipFunc" />

    </mx:columns>
  </mx:AdvancedDataGrid>
</mx:Application>
A: 

This has something to do with the logic of the SortCompareFunction.

Put dob:"01/01/1970" for all the group nodes and the sort works as expected, is that correct?

Swaroop C H
A: 

Yes, it works. But in the actual solution, it's not always feasible to add the field to each object.

If I put a breakpoint or tracing at the sort callback, the sort callback is never called for the other rows. By "logic of the SortCompareFunction", are you referring to the SortCompareFunction inside Flex, or the callback function above? I can't see anything odd in the SortCompareFunction above that would cause a problem, unless I'm not supposed to use "hasOwnProperty"?

Shawn Hurley
+1  A: 

It seems as if the first row contains null data or an empty string, and the advanceddatagrid is set to use grouped data, then the sort function doesn't get called.

it's a bit of a hack, yes, but if you can put in an unrealistic (say 1/1/1770), constant piece of data that you could insert at the database/file read/data input level then use the column labelFunction to render as null if the data matches that column, it should work, or at least the sort function will get called.

 public function dateCellLabel(item:Object, column:AdvancedDataGridColumn):String
    {
        var date:String = item[column.dataField];

        if (date=="1/1/1770") 
         return null; 
        else
         return  date;
    }

Sorry about answering this so late, but at least if somebody else tries to find the answer, they might see this.

RaySir