Server-side processing using custom controller examples

Issue #3 new
David Brown created an issue
  • The server-side processing using custom controller I'm sure works very well like the Default GORM controller.
  • However, attempts to UnGORM my project and continue with the custom controller are less than ideal.
  • I have cloned your plugin in an effort to understand why the Default controller works and the custom controller does not.
  • More explicit custom controller examples would be great using a src/groovy domain or a domain that defines:
mappedWith = 'none'

Please advise.

Comments (4)

  1. Ben Wilson repo owner

    Hi David,

    Here's an example of a custom controller that does not use any domain objects:

    import grails.converters.JSON
    
    class MyCustomController {
    
        def myCustomAction() {
            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 the table as follows:

        <dt:datatable name="myCustomDataTable" serverDataLoad="true" controller="myCustom" dataAction="myCustomAction" >
            <dt:column name="firstName"/>
            <dt:column name="lastName"/>
            <dt:column name="email"/>
        </dt:datatable>
    

    You can build up the data any way you like. If you wish to use server-side processing, add serverSide="true" to your dt:datatable tag instead of serverDataLoad="true". Then, your controller will need to handle any pagination, ordering and filtering you wish to provide. The information pertaining to that is provided to the controller action as parameters in the params map. See this page for more information on those: https://datatables.net/manual/server-side

    I hope that helps. Let me know if you have further questions. -Ben

  2. David Brown reporter
    • Please see attachments, I now understand dropping the package and domain reference.
    • Recreating your example dt controller both in my existing app and a new Grails app dedicated to your example I only get the table displayed but no data and the ubiquitous $ not defined so it can find JQuery but the other controller can find $.
    • My target application is displaying a table with no data and a 404 error.
    • If I change the controller from respond to render then I get JSON in the browser.
    • Maybe if I could include your plugin outside of BuildConfig.groovy maybe I could debug and understand the DT plugin better.
  3. Ben Wilson repo owner

    Hi David, My apologies, I had an error in the GSP part, which was based on an error in the wiki documentation that I have also corrected.

    Replace this:

    <dt:datatable name="myCustomDataTable" serverDataLoad="true" controller="myCustomController" dataAction="myCustomAction" >
    

    with

    <dt:datatable name="myCustomDataTable" serverDataLoad="true" controller="myCustom" dataAction="myCustomAction" >
    

    Hope that fixes it!

  4. Log in to comment