Re-rendering table from pre-existing column info

Re-rendering table from pre-existing column info

zanedevzanedev Posts: 8Questions: 0Answers: 0
edited December 2012 in General
Hello, I am working on adding columns to a table (reloading the table with updated data) and I have it working with the original aoColumns array but instead I would like to pull the table's existing aoColumns settings using something like fnSettings().aoColumns. That way I could pull the current columns in case they have been updated and keep any settings like the current sorting or whatever. If I pass fnSettings().aoColumns to the new datatable as it is it works but if I try to add a new column with just stitle and mdata to that array it doesn't. I've even tried copying one of the existing columns data and changing the title/mdata but that doesn't work either. Is there a better way to do this or is there a required set of options I need to include with a new column since the others have more data from initialization? Thanks!

Replies

  • zanedevzanedev Posts: 8Questions: 0Answers: 0
    edited December 2012
    Here is a jsfiddle that shows the issue: http://jsfiddle.net/qrk5W/6/ (updated fiddle) looks like fnDestroy (true or not) is throwing an error:

    [code]
    Uncaught TypeError: Cannot read property 'className' of undefined jquery.dataTables.js:4274
    _fnSortingClasses jquery.dataTables.js:4274
    fnDestroy jquery.dataTables.js:5211
    [/code]

    At least there is a workaround to manually delete and reinit the table and dom node, maybe I'm not understanding the best approach here to pull and refresh the data from an existing table.
  • zanedevzanedev Posts: 8Questions: 0Answers: 0
    Should probably change the title of this post to "fnDestroy is throwing an error", I'm also getting "TypeError: oSettings is null" when using fnDestroy
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    [code]
    colData = example3.fnSettings().aoColumns

    colData.push({
    sTitle: "newcol",
    mData: "newcol"
    })
    [/code]

    I'm not surprised that's throwing an error - you are manipulating a private variable by adding new (incomplete) data to the private `aoColumns` parameter in the settings object. DataTables will see that there are then three entries for that column, attempt to work on three columns, find there are only two and go boom.

    I'd very strongly suggest you don't manipulate the settings object properties at all - that's what the API is there for. Treat the options from fnSettings as read only.

    Allan
  • zanedevzanedev Posts: 8Questions: 0Answers: 0
    Okay makes sense on not manipulating the fnsettings directly. I'll revisit later since I have a workaround for now to just manually destroy and recreate the table with the new data. Seems a bit odd that the table renders fine though and only fndestroy is the issue.
This discussion has been closed.