datatables converts javascript object to description string

datatables converts javascript object to description string

tim.ingenuitytim.ingenuity Posts: 2Questions: 0Answers: 0
edited October 2010 in General
Hi,
I updated my version of datatables to 1.7.3. I keep a hidden column (at index 0) in the table which is a javascript object and re-use this object when I want to show additional details about the row.

Below is the structure of the row:

["[object Object]", "", "60%", "19%", "2.3.103067", "3", "1", "0", "0", "84", "0", "75", "8", "0", "0", "0", "0"]

The first element is the hidden cell. I do not understand why the first element has changed to a string "[object Object]" when the original data inserted there was a javascript object.

This used to work fine in the earlier version.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi tim.ingenuity,

    The reason that this occurs is that DataTables will cast everything it gets into it's data display array as a string. This was introduced in the 1.7 series in order to make data handling much simpler inside DataTables. It works on the assumption that the data array which is given to DataTables is for display, and since an object can't be displayed, using an object here is not an expected interaction.

    I can of course see exactly why you would want to do that - it's useful to store extra information about the row that is being shown. I think the completely "correct" way to deal with this will be to make an alteration to the DataTables API allowing objects to be attached to a row (this is actually already possible using the private API - aoData[row] is the array in question, but it's not part of publicly documented API). I think this will be a nice enhancement and I've made a note that this should be introduced - thanks :-)

    However, that doesn't help you at the moment... There are a couple of ways to do it just now:

    1. Use the private API to attach the object (fnSettings().aoData[ row_in_question ].whatever)
    2. Split the array you currently have into two arrays, one for DataTables display and one for the objects - then use the row marker to jump between them.
    3. As 2, but put an id into the display array in place of the object, which can be used for faster lookups.

    Regards,
    Allan
  • tim.ingenuitytim.ingenuity Posts: 2Questions: 0Answers: 0
    Thanks for the great reply Allan. This is really helpful info. Option 3 is probably the best way to go.

    Cheers,
    Tim
This discussion has been closed.