Problem with dynamic columns/reinitializing table

Problem with dynamic columns/reinitializing table

doug_adoug_a Posts: 2Questions: 1Answers: 0

Hi: I'm trying to dynamically create a set of columnDefs, attach it to a JSON object that contains all the tableSettings, and then recreate the table. It works the first time

Say that the table settings object is like this:

var tableSettings =
{
"paging_type" : "full_numbers",
"paging" : "true",
"columnDefs" : undefined
}

Then I create columnDefs like so (the names are shortened):

var columnDefs =
[{"targets":[0],"name":"BNA"},{"targets":[1],"name":"CN"},{"targets":[2],"name":"BN"},{"targets":[3],"name":"BAS"},{"targets":[4],"name":"SR"},{"targets":[5],"name":"SRY"},{"targets":[6],"name":"CNR"}]

Then

tableSettings.columnDefs = columnDefs;
var myTable = $("#myTable").DataTable(tableSettings);

After that I want to recreate the column defs something like this:

var columnDefs =
[{"targets":[0],"name":"BNA"},{"targets":[1],"name":"CN"},{"targets":[2],"name":"BN"},{"targets":[3],"name":"BAS"},{"targets":[4],"name":"SR"},{"targets":[5],"name":"SRY"},{"targets":[6],"name":"CNR"},{"targets":[7], "name": "ABCD"}]

myTable.clear();
$("#myTable").DataTable().destroy();
tableSettings.columnDefs = columnDefs (the new columnDefs)
myTable = $("#myTable").DataTable(columnSettings);

When I look at the columns after that, I see that targets 0 through 6 are correct, but target 7 has no name. The original columns that the table was initially created with are there and fine, but the added column isn't accessible by name because the name is "". This is true of any number of columns I add that weren't in the original table.

Any insights?

Thanks.

Answers

  • doug_adoug_a Posts: 2Questions: 1Answers: 0

    I can strip this down, I think, to one easy question. Given a set of dynamically created column definitions, what is the proper way to initialize a data table?

    And given a second set of dynamically created column definitions, what is the proper way to clear out the columns of the first table and recreate the table with the new column definitions?

    Thanks.

This discussion has been closed.