Flex howto: DataGrid sorting and labeling
First, the data going into my DataGrid. I have three objects
Discipline ... Employee ... Step
The Discipline object has an EmployeeID and StepID. Discipline also has EmployeeObject and StepObject, which are the full Employee and Step objects that match the IDs in the Discipline object. It's a lot simpler than it might sound.
An array of Discipline objects is the dataprovider of my DataGrid.
Now when I sort my DataGrid, I don't want to sort on the EmployeeID or StepID fields. I want to sort on EmployeeName and in the case of the step, the step's Rank (the order the steps happen). In other words, I want to sort by something other than the dataField of that column.
id="dgProposals">
<mx:columns>
<mx:DataGridColumn headerText="Month" dataField="DisciplineMonth" labelFunction="formatDate"/>
<mx:DataGridColumn headerText="Employee" dataField="EmployeeBadgeID" labelFunction="formatEmployee" sortCompareFunction="sortName"/>
<mx:DataGridColumn headerText="Step" dataField="StepID" labelFunction="formatStep" sortCompareFunction="sortStep"/>
</mx:columns>
</mx:DataGrid>
if( item1.StepObject.Rank < item2.StepObject.Rank )
return -1;
else if( item1.StepObject.Rank > item2.StepObject.Rank )
return 1;
return 0;
}
The sortName function for employees is the same sort of thing. A sortCompareFunction will take objects of the same type. In this case my sortStep function is taking two Discipline objects, starting with items 0 and 1 in the array of my dataprovider. Returning -1 means the first object passed in is "less" and will remain where it is. Returning 1 will swap the items, 0 means they're the same. If the two items are the same, you might want to compare some other data elements.
return object.StepObject.Step;
}
formatName is, again, the same thing. A DataGridColumn's labelFunction will also take two values. One is the dataprovider's current row of data, the other is the whole column. I haven't found a lot of use for the column being passed in.


There are no comments for this entry.
[Add Comment]