Wiki

Clone wiki

grails-datatables / dataFunction

dataFunction

Values

Name Type Default
dataFunction Closure None

Description

dataFunction is a column option that allows you to change the way a column is displayed. It consists of a closure that accepts either one or two parameters. The first parameter is the item for the row, and the second (optional) parameter is the row index.

You can use the dataFunction option to perform a minor change such as displaying the value in upper case, or to completely change the value to whatever you like.

Note that if you are using server-side processing (by setting serverSide=true) and are allowing searching and ordering on this column, then the fact that the GrailsDataTables plugin is performing the search function for you means that whatever transformation you use for this column will not be reflected in the search algorithm. If you simply use the dataFunction option to change the case of the value, then searching will not be affected, but if you change the text otherwise, the matching will still be done using the original value of the column, before the dataFunction closure has been applied. This is because the search matching is done in the database, and the dataFunction closure is only applied after the search has been completed and the data retrieved from the database.

When you use the dataFunction option, the column name is not used in the obtaining of the data, therefore it is not necessary to have the column name match a property in your domain class or list item. (See the notARealProperty column in the example below). However, when you use server-side processing (by setting serverSide=true), if your column name does not match a domain class property, then searchable and orderable will be automatically set to false for that column, since it is not possible for the server to search and sort based on the result of the dataFunction closure. (Technically it could be done, but since it would require applying the closure to the entire dataset, it would be expensive and probably impractical).

The exception to the preceding paragraph is that if you are using a custom controller to provide your table data, then you will have complete control over what happens, so in that case, searchable and orderable will not be automatically set to false.

If you are not using server-side processing, then searching and ordering are performed in the browser, so there is no such consideration when using the dataFunction option to manipulate a column value.

Example

<dt:datatable name="MyTable" domainClass="this.is.my.DomainClass" serverSide="true">
    <dt:column name="myFirstProperty" dataFunction="${{domainClass -> domainClass.myFirstProperty.toUpperCase()}}"/>
    <dt:column name="notARealProperty" dataFunction="${{domainClass -> domainClass.mySecondProperty + 'extra characters'}}"/>
</dt:datatable>

If you have a particularly complex closure, you may prefer to define it in your controller and simply refer to it in the dt:column tag.

See Also

serverSide, searchable, orderable


Back to Options.

Updated