Update of table when using JS Array as source
Update of table when using JS Array as source
someotherchris
Posts: 5Questions: 0Answers: 0
Hi all,
I'm having huge problems when I want to update an existing table by using new sources. Since I am using an input field to enter a search term for the table data, I fetch the data object via Ajax and apply it with $(selector).dataTable(fetched_data) afterwards. My problem is, that the table is built correctly when I make my first call but it's never updated, when I start another search. I tried lots of combinations using fnDraw, fnUpdate and fnDestroy but now I am totally lost.
Here's my code:
HTML:
[code]
ID
Code
Company
Zip
CIty
Phone
[/code]
JS for data assignment:
[code]
var rTable = parent
.children(".resultstable")
.children("table:first")
.dataTable(data);
[/code]
I hope, I provided all data that is necessary to understand my problem.
Thanks for any kind of advise.
Regards,
Chris
I'm having huge problems when I want to update an existing table by using new sources. Since I am using an input field to enter a search term for the table data, I fetch the data object via Ajax and apply it with $(selector).dataTable(fetched_data) afterwards. My problem is, that the table is built correctly when I make my first call but it's never updated, when I start another search. I tried lots of combinations using fnDraw, fnUpdate and fnDestroy but now I am totally lost.
Here's my code:
HTML:
[code]
ID
Code
Company
Zip
CIty
Phone
[/code]
JS for data assignment:
[code]
var rTable = parent
.children(".resultstable")
.children("table:first")
.dataTable(data);
[/code]
I hope, I provided all data that is necessary to understand my problem.
Thanks for any kind of advise.
Regards,
Chris
This discussion has been closed.
Replies
[code]
oTable.fnClearTable();
oTable.fnAddData( fetched_data.aaData );
[/code]
would clear the current data in the table, and then add the new information.
Allan
thanks for answering so fast!
I changed the code to the following:
[code]
var rTable = parent
.children(".resultstable")
.children("table:first")
.dataTable();
rTable.fnClearTable();
rTable.fnAddData(data.aaData);
[/code]
The first table is displayed correctly but when I try to apply another search, I get "TypeError: Result of expression "oSettings' [null] is not an object".
Is there a problem with ".dataTable()" when I try to update? The problem is, that I have multiple ".resultstable"s and every time I select some "rTable" it seems like I can't use it as a dataTable object, so I try to call ".dataTable()" again in order to get the instance but maybe my approach is wrong here.
Regards,
Chris
Allan
in order not to confuse you with further information that might be wrong, I installed a test version. You will receive further information via contact form.
BTW: The install isn't for you to do my job. It's just for you if you are interested in the problem. :)
Regards,
Christian
Hi Allan,
I solved to problem. Since dataTable rewrites the source (of course), I have to use .find() instead of .children() when searching for the table. Totally my fault. :)
In order not to reinit the already initialized table, I stored the object using .data() to be able to retrieve it on data update.
The code (for anybody who might stumble upon a similar problem):
[code]
// apply data tables
var rTable = parent
.children(".resultstable")
.find("table:first");
// check whether data table has been initialized already
var obj = rTable.data("dataTable");
if (obj === undefined) {
// init and store object for further use
rTable.dataTable(tableConfig);
rTable.data("dataTable", rTable);
} else {
// fetch already initialized object
rTable = obj;
}
// update table content
rTable.fnClearTable();
rTable.fnAddData(data.aaData);
[/code]
Thank again for helping and pushing me into the right direction.
Regards,
Christian
Regards,
Allan
Hi Allan,
no problem. I'm pretty sure it would have taken a decade to solve it without your hints.
Regards,
Christian