One problem I encountered today is that the attribute corresponding to a column of the DataGrid object is a complex object. For example, the DataGrid displays the list of books and returns results using the python background, the Author column corresponds to an Author object. Now I want to display the author name in the "author" column of the Book.
Solution: first, add the column in The mxml file.
<Mx: DataGridColumn id = "colAuthor" headerText = "Author" dataField = "Author" width = "100"/>. It is worth noting that the dataFiled must be its corresponding attribute. For example, the data source bound to the DataGrid is defined as: {"Bookname", "Author", "PublicDate"}, where Author is defined as {"Name", "Age ", "Gender", "Nationality"} and so on.
In this DataGridColumn, dataField must be specified as "Author ".
Next, we need to modify the display content of the column of this DataGridColumn (otherwise it will only display "object", obviously not what we want ).
Private function formatAuthor (item: Object, c: DataGridColumn): String // defines what content to display to this column.
{
Return item. Author. Name; // Note: Write Author directly here. Because item refers to the Book object.
}
Then use this function as the labelFunction. colAuthor. labelFunction = formatAuthor; // set LabelFunction. Here, colAuthor is the ID of the column. In this way, you can.
Another point worth noting is that, when we modify this way, a problem will occur when we click the DataGrid header to sort the headers. Because the DataGrid cannot find the function for sorting the "Author" type. This column is bound with "Authro". Therefore, we must provide a sorting function explicitly. The method is as follows:
Private function AuthorSortFunc (obj1: Object, obj2: Object): int
{
// LabelFunction is called to obtain the content of the column. In this way, we do not need to repeatedly write comparison functions in the future.
Return ObjectUtil. stringCompare (colAuthor. labelFunction (obj1, colAuthor), colAuthor. labelFunction (obj2, colAuthor ));
}
Use this function as the sortCompareFunction of the column. ColAuthor. sortCompareFunction = AuthorSortFunc; // sets the sorting function.
---- David Cai Wu Yu Jinshan Company