0 Replies Latest reply on Aug 24, 2010 2:02 PM by JoshBeall

    "Proper" way to handle date-sorting when there are possible null values for a datagrid's date column

    JoshBeall Level 1

      Hi All,

       

      I've got a DataGrid that is bound to an ArrayCollection of objects.  Each object has an "expiration" property that is displayed in the DataGrid.

       

      The ArrayCollection is the populated by a JSON HTTP service.  In some circumstance, the "expiration" property will be unset.  This results in the "expiration" property having all it's properties (it's of type Date) to be NaN.

       

      I found that as soon as there's one unset "expiration" value in the JSON result, the "expiration" column in the datagrid becomes unsortable.  You click on it to sort by that column, but the sorting doesn't happen.

       

      After some digging I came up with this workaround.  Is this the "correct" way to do this or should I go about this a different way?  What I've got below appears to work, but I don't know if there's a better/simpler way.

       

                     public function dateFormatter(item:valueObjects.RosterEntry, column:DataGridColumn):String {
                          var result:String = "";
                          if(!isNaN(item.expiration.fullYear))
                               result = DateField.dateToString(item.expiration,"MM/DD/YYYY");
                          return result;
                     }
                     
                     public function sortExpiration(item1:RosterEntry, item2:RosterEntry):int {
                          // -1 if ob1 is less, 0 if they are equal, 1 is ob1 is greater
                          var result:int = 0;
                          
                          // If either one of them is a number (i.e., isNaN returns false), we can do
                          // the comparisons.  Otherwise they are considered equal.
                          if(!isNaN(item1.expiration.time) || !isNaN(item2.expiration.time)) {
                               // If item1.expiration is less than item2.expiration, OR if item.expiration is not defined,
                               // item1.expiration is "smaller" than item2.expiration
                               if(item1.expiration < item2.expiration || isNaN(item1.expiration.time))
                                    result = -1;
                               else if(item1.expiration > item2.expiration || isNaN(item2.expiration.time))
                                    result = 1;
                               // In any other case, they must be equal
                          }
                               
                          return result;
                     }
                     
                     // labelFunction='{function(item:valueObjects.RosterEntry, column:DataGridColumn):String { return DateField.dateToString(item.expiration,"MM/DD/YYYY"); }}'
      
                ]]>
      
              // Skipping a bit...
           
           <mx:DataGrid x="10" y="42" dataProvider="{currentRoster}">
                <mx:columns>
                     <mx:DataGridColumn headerText="Member ID" dataField="constituentId"/>
                     <mx:DataGridColumn headerText="Firstname" dataField="firstname"/>
                     <mx:DataGridColumn headerText="Lastname" dataField="lastname"/>
                     <mx:DataGridColumn headerText="Standing" dataField="standing"/>
                     <mx:DataGridColumn headerText="Expiration" dataField="expiration"
                                            labelFunction="dateFormatter"
                                            sortCompareFunction="sortExpiration"
                                            />
                     <mx:DataGridColumn headerText="Type" dataField="entryType"/>
                </mx:columns>
           </mx:DataGrid>