how to reformat data in initialization string

how to reformat data in initialization string

dinotomdinotom Posts: 27Questions: 7Answers: 0
edited May 2014 in DataTables 1.10

I am currently populating a table via this init string

<code>
ideaTable.DataTable({
data: newArray,<br />
columns: [<br />
{ data: 'Date', title: 'Date', searchable: false, orderable: false },<br />
{ data: 'CompanyInfo', title: 'Company Info' },<br />
{ data: 'TradeDirection', title: 'Buy/Sell', searchable: false },<br />
{ data: 'Notes', title: 'Trading Notes', orderable: false }]
}).order([1, 'asc'], [2, 'asc'])<br />
.draw();<br />
</code>

The date i coming in as a long datetime and needs reformatiing. Can I do that in the init string and if so what is best method?

This question has an accepted answers - jump to answer

Answers

  • dinotomdinotom Posts: 27Questions: 7Answers: 0
    edited May 2014

    That example appears to be from 1.9 and I'm using 1.10. I had already tried using render like so

    <code>
    "render": function ( data, type, full ) {
    return data.Date.split('T')[0]}
    </code>

    To the date column definition but I get a 'data not in scope' error.
    How do I use render in version 1.10 with the columns attribute of the init string versus the 1.9 columndefs

  • dinotomdinotom Posts: 27Questions: 7Answers: 0

    Or more simply, how do I access the data object for each column defined in columns to use in render method

  • dinotomdinotom Posts: 27Questions: 7Answers: 0
    edited May 2014

    with much trial and error, the solution is

    [code]<br />
    ideaTable.DataTable({<br />
        data: data,<br />
        columns: [<br />
    {<br />
      data: 'Date', title: 'Date', searchable: false, orderable: false,<br />
    render: function () {<br />
        return data[0].Date.split('T')[0]; //original format 2014-05-09T00:00:00-04:00<br />
    }<br />
    },<br />
      { data: 'CompanyInfo', title: 'Company Info' },<br />
      { data: 'TradeDirection', title: 'Buy/Sell', searchable: false },<br />
      { data: 'Notes', title: 'Trading Notes', orderable: false }<br />
    ]
    }).order([1, 'asc'], [2, 'asc'])<br />
    .draw();<br />
    [/code]

  • dinotomdinotom Posts: 27Questions: 7Answers: 0

    That's actually not the solution as data[0] is obviously the first row only as I can see now that im changing something other than the date and they all come out the same. What is the syntax for this to work on each row?

  • dinotomdinotom Posts: 27Questions: 7Answers: 0
    edited May 2014

    Love trial and error, leads you to learn a whole bunch of stuff.

    The solution, which was embarrassingly easy, I was just making it too complicated.

    <code>
    render: function(mData) {
    //using moment.js library for date formatting
    return moment(mData).format('MMMM Do YYYY'); }
    </code>

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Heh - good to here you found a solution in the end. I do love moment.js!

    Allan

This discussion has been closed.