formatting dates with mData and mRender

formatting dates with mData and mRender

jkrobbinsjkrobbins Posts: 32Questions: 3Answers: 0

I tried to create a test case at live.datatables.net, but I can neither login nor register. I just get "Checking...". There is a debug posted at "agavut". My dates are returned from the database as simple character strings as yyyyMMdd. I want to display them as MM/dd/yyyy.
Based on other posts I've found here, I wrote this function:

 function formatJSONDate(dateInput, type) {
    console.log('running formatJSONDate function');
    if (dateInput === null) {
      return '';
    }
    if (type === 'display') {
      var currYear = dateInput.substring(0, 4);
      var currMonth = dateInput.substring(4, 6);
      var currDay = dateInput.substring(6);
      return currMonth + '/' + currDay + '/' + currYear;
    }
    return dateInput;
  }

My table is setup like so:

var oDataTable = $('#allrectable').dataTable({
    'aaSorting': [[0, 'asc']],
    'aLengthMenu': [10, 25, 50, 100],
    'iDisplayLength': 10,
    'bJQueryUI': true,
    'bRetrieve': true,
    'bAutoWidth': false,
    'sScrollX': '100%',
    'sPaginationType': 'full_numbers',
    'aoColumns': [
      {'mdata': 0}, {'mdata': 1},
      {'mData': 2, 'mRender': function(data, type, full) {
          return formatJSONDate(data, 'display');
        }},
      {'mData': 3}, {'mData': 5},
      {'mData': 6}, {'mData': 7}, {'mData': 8}, {'mData': 9}, {'mData': 10},
      {'mData': 11}, {'mData': 12}, {'mData': 13}, {'mData': 14}, {'mData': 15},
      {'mData': 16}, {'mData': 4}, {'mData': 17}, {'mData': 18}, {'mData': 19}, {'mData': 20}
    ]
  });

The dates are displayed as yyyyMMdd. The console.log messages is never displayed so the function is never getting called.
And why is the MarkDown preview so screwed up? My code is in one continous line that runs off the screen.

This question has an accepted answers - jump to answer

Answers

  • RpiechuraRpiechura Posts: 98Questions: 3Answers: 14
    Answer ✓

    The markdown preview is odd, if you post something and than refresh the page it shows up correctly. I'm not sure why the preview / when you first post it looks so odd but you did things correctly so I wouldn't worry too much about it.

    As far as the question goes with datatables 1.9.4 it works just fine, so I don't really know what to tell you. I used your data (as provided by the debugger link) and your initialization code (as provided in the post) without changing the functionality of any of it and it works. Perhaps try going from 1.9 to 1.9.4? Or even go all the way up to 1.10 since it shouldn't break anything you're doing (the code is all backwards compatible with the 1.9* syntax) and see if that fixes it for you.

  • jkrobbinsjkrobbins Posts: 32Questions: 3Answers: 0

    It appears that 1.9 was at least part of the problem. That and a misunderstanding of the "type" parameter. I upgraded to 1.9.4 and I'm making progress now. Thanks for responding.

This discussion has been closed.