Posted by: edsyrett | 11 December 2009

Sorting XML in AdvancedDataGrid


If you use native types in the dataprovider of an AdvancedDataGrid, the grid will look at the first row of data to figure out how to sort the columns.  For example if you have set the datafield of the first column to reference numeric data, the column will be sorted numerically.  In other words, 100 will appear after 2 etc.  Similarly for dates.

However, if you pass XML data, none of this will work – all the text in the elements and attribute are by definition text, and no code in the world can look at the data and reliably decide whether to sort as text, numbers or dates.

After having a look at the framework code, it became quite easy to see where extra functionality was needed.  Click on the screenshot of the grid above and you’ll see my app that has XML data but is correctly sorting a numeric and date field.

I have monkey-patched the initCompare() function of SortField to look for additional attributes in the XML which define what the sort should be.  The code for each column looks at the datafield for the column, and then looks for an additional attribute with the extension “_format”.  In otherwords, if your grid column has a datafield “total”, the new code in SortField looks for an attribute with the name “total_format”.  The value of this extra field can be “string”, “numeric” or “date” and the new code will put in place the correct sort function.

Advertisements

Responses

  1. I really like this blog. Please continue the great work. Regards!!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: