Wiki

Clone wiki

grails-datatables / controller

controller

Values

Name Type Default
controller String None

Description

controller is a table option that allows you to specify a custom controller that will be used to supply the table data via AJAX. This option only applies when using serverSide or serverDataLoad.

The default behavior for a table that uses AJAX to obtain the table data from the server is for the GrailsDataTables plugin to supply the data using its own controller built into the plugin. If you provide your own custom controller, you can return whatever data you like.

Note that if the controller option is set, then the dataAction option must also be set. If the dataAction option is set, then the controller option may be omitted, in which case the custom action must be placed in the same controller as the page in which the table resides.

You may also include custom parameters in the AJAX requests by using the ajaxParams option.

The format of the request and response is described here. Note that the number of columns returned by your controller must equal the number of columns in the table.

Example

Your controller might look like this:

#!groovy

import grails.converters.JSON

class MyTableDataController {

    def myDataAction() {
        def resultMap = [:]
        if(params.draw) {
            resultMap.draw = params.draw as int
        }
        resultMap.recordsTotal = 5
        resultMap.recordsFiltered = 5
        resultMap.data = [
                ["Thom", "Buehler", "thom.buehler@nowhere.com"],
                ["Lola", "Cowper", "lola.cowper@nowhere.com"],
                ["Kara", "Dugger", "kara.dugger@nowhere.com"],
                ["Kyla", "Hallauer", "kyla.hallauer@nowhere.com"],
                ["Herb", "Hawker", "herb.hawker@nowhere.com"]
        ]
        render resultMap as JSON
    }
}

Then in your GSP, you can define your table so that it uses your controller:

<dt:datatable name="MyTable" serverSide="true" controller="myTableData" dataAction="myDataAction">
        <dt:column name="firstName"/>
        <dt:column name="lastName"/>
        <dt:column name="email"/>
</dt:datatable>
Note that in this case, the controller is called MyTableDataController.

See Also

serverSide, serverDataLoad, dataAction, ajaxParams


Back to Options.

Updated