UI Plugin

DataTable: incorporate type formatters

Details

  • Type: Task Task
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0.2
  • Fix Version/s: 1.1
  • Component/s: None
  • Labels:
    None

Description

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

Issue Links

Activity

Hide
Matthew Taylor added a comment -

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.

Show
Matthew Taylor added a comment - 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.

People

Vote (4)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: