Flex: process the Column value display in the DataGrid (original)

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.