Issue Details (XML | Word | Printable)

Key: GRAILSPLUGINS-491
Type: Task Task
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Matthew Taylor
Reporter: Matthew Taylor
Votes: 4
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Grails Plugins

DataTable: incorporate type formatters

Created: 18/Sep/08 09:30 PM   Updated: 23/Jan/09 04:37 PM   Resolved: 23/Jan/09 04:37 PM
Return to search
Component/s: Grails-UI
Affects Version/s: Grails-UI 1.0.2
Fix Version/s: Grails-UI 1.1

Time Tracking:
Not Specified

Issue Links:
dependent
 


 Description  « Hide

Data table column def should support types, like date, number, currency. Need to incorporate these into the DataTable and add formatters for them.



Matthew Taylor added a comment - 23/Jan/09 04:37 PM

All the formatters worked fine except for 'date', which was because JSON doesn't handle date types. So in order to hook up your controllers to pass real JavaScript date-formatted objects back to the view, you have to use a utility method I added to the GrailsUITagLibService. For example, this came right out of the guidemo project:

def dataTableDataAsJSON = {
        def list = []
        if (params.sort == 'crap') params.remove('sort')
        def demoList = Demo.list(params) 
        response.setHeader("Cache-Control", "no-store")
        demoList.each {
            list << [
                    id: it.id,
                    name: it.name,
                    birthDate: grailsUITagLibService.dateToJs(it.birthDate),
                    age: it.age,
                    netWorth: it.netWorth,
                    isDumb: it.isDumb,
                    dataUrl: g.createLink(action: 'dataDrillDown') + "/$it.id"
            ]
        }
        def data = [
                totalRecords: Demo.count(),
                results: list
        ]
        render data as JSON
    }

As you can see, you send the 'birthDate' Date object into "dateToJs", and it will transform it into a string that will be recognized by the DataTable as a date in the view, and pulled back out into a JavaScript Date object for formatting.