darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
450 stars 172 forks source link

Java 8 date objects not rendering #19

Closed Rejinderi closed 8 years ago

Rejinderi commented 8 years ago

image

Local datetime objects are rendered as [Object object] any ways to have them rendered properly?

darrachequesne commented 8 years ago

Hi! I guess you have found the solution by yourself, may I ask if you could share it, for future reference?

For my part, I would have suggested using Moment.js to format that field.

dawnofclarity commented 8 years ago

FWIW, I used moment.js as follows:

function renderDate(data, type, row) {
  if (data) {
    var dm = moment({y: data.year, M: data.monthValue - 1, d: data.dayOfMonth});
    switch (type) {
      case 'display':
        return dm.format("D MMM YY");
      default:
        return dm.format("X");
    }
  }
  else {
    return '';
  }
}

Then apply to your column using the render keyword

      {
        'data': "txnDate",
        'render': renderDate
      },

The same technique can be applied to LocalTime and LocalDateTime too.

darrachequesne commented 8 years ago

@dawnofclarity great, thanks! :+1: