Problem when redefining columnDefs
Problem when redefining columnDefs
So, I seem to be running into a weird issue...
I have a report that I'm creating, and depending on whether a checkbox is checked or not, I supply the $('#myTable').DataTable(...)
call with a different array for the columnDefs parameter.
The problem is, whichever columnDefs array is supplied first seems to stick around even though $('#myTable').DataTable(...)
is called again with a different columnDefs array. Now, before the $('#myTable').DataTable(...)
is called again, I am making sure to call myDataTable.clear()
and myDataTable.destroy()
(When I initialize the datatable, I'm setting it to the myDataTable
variable so I can reference it later, like so: var myDataTable = $('#myTable').DataTable(...)
)
Is there a way to make sure the new columnDefs array takes hold when the datatable is created again?
Answers
Based on your description I built a simplistic example. Seems to work:
http://live.datatables.net/pekoquvu/1/edit
Maybe you can provide a test case (update the above if you want) or link to your page showing the issue.
Kevin
I can't recreate this in the JSBin, so I'll try to find some workaround.
Maybe your checkbox check isn't working and the wrong array is chosen. Maybe output a console message to verify the code paths when you have the checkbox checked and not checked.
If you swap the arrays does the other columnDefs work in general?
Kevin
I can verify in the console that the columnDefs is being changed before I call
myDataTable = $('#reportResults table').DataTable({...});
again.It's driving me nuts. lol
I'll try again to get an example that closely matches what I'm doing.
Maybe post your JS code here and we can take a cursory look.
Kevin
I can't really post the exact JS, as it's all tied into our system, which includes authentication, etc. This is as close as I can get, though it works as expected here: http://live.datatables.net/pekoquvu/3/edit
In the real case, the data is coming from an ajax call to a server, of course. I'm not, howver, using the serverSide option... I'm getting the response, and then supplying that to the .DataTable({...}) call as a 'data' option, like in the JSBin above.
Some screenshots and posting the columnDefs object might be the best I can do...
So,
As you can see in the following screenshot, there are 14 columns visible, and I've included the columns object below:
var columnDefs =
Now, if I re-run the report with the columnDefs below, you can see that there are still only 14 columns, but the columnDefs show that the 15th column should be visible:
var columnDefs =
I can't help but think that something is sticking around in memory. Maybe I'm not calling the .clear().draw() and .destroy() methods at the right time or something.
And here's the crazy part...
Let's say I run the report and the columnDefs array that has a render method inside of the object with
"targets": 14
. If I run the report again with the different columnDefs array (one that doesn't have that render method on that column), I can put a breakpoint inside of the render method on the original columnDefs array and that render method is still called.This is after verifying that the right columnDefs array is being used in the
.DataTable({...})
call.Oh god... finally figured it out.
I was creating the object passed into
.DataTable({...})
using the extend method from Lodash.For some reason, I thought that the _.extend method returned a new object, much like Object.assign(), but of course it wasn't.
So, config settings were hanging around like I thought, but until now I didn't know why. smh
Glad you found the issue.
Kevin
Thanks for the help!