How to add rows / append to already initialized DT table
How to add rows / append to already initialized DT table
Hello All,
So i have two buttons on my page(both having separate onClick functions to go to) , when I click the first, it runs the first ajax and appends the data. When I click the second , the expected result would be to have the data appended to the same table , but as you can guess that's not what's happening.
First click ,all good . Data is being displayed with zero problems.
My issue is with the second button , if I initialize the table and hit the button everything works great.
the whole thing does two things, button 1 brings first rows, button 2 brings the rest.
let dataTable;
function button1() {
dataTable = $('#resTbl').DataTable({
destroy: true,
"scrollY": "650px",
"scrollCollapse": true,
"paging": false,
dom: 'Bfrtip',
buttons: ['csv'],
aaSorting: [],
ajax : {
url: '/someURL',
type: 'post',
error: function (error) {
alert(error.responseText)
},
data : function (data) {
data.vars to send
},
dataSrc: '',
},
aaSorting: [],
columns: [
{"data": 'id'},
{"data": 'name'},
{"data": 'title'},
{"data": 'b.day'},
],
"columnDefs": [{
"defaultContent": "",
"targets": "_all"
}],
})
}
function button2() {
dataTable = $('#resTbl').DataTable() // not sure if this is the right place
$.ajax ({
url: 'someURL',
type: 'post',
error: function (error) {
alert(error.responseText)
},
data : {
data.vars to send
},
dataSrc: '',
success: function (data) {
$.each(data, function(i, item) {
console.log(data[i].id)
dataTable.row.add(
[
data[0].id,
data[0].name,
data[0].title
data[0].b.day
]).draw()
})
}
})
}
May have misplaced some of the brackets during copy/pasting. As I mentioned , both button clicks works great on their own nothing too fancy about it . The issue is ,when I populate the table from button1 and click button2 after, it only adds a row without values . But the values are visible on the browser console. Any help is appreciated
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Answers
You have
console.log(data[i].id)
. but are usingdata[0].id,
inrow.add()
. Probably need to usedata[i].id,
. I would remove.draw()
from line 79 and usedraw()
after the loop so the table is drawn once instead of the number of times through the loop.If you still need help then post the
data
response in thesuccess
function so we can see what you are receiving.Kevin
Hi Kevin, yes I made a mistake while refactoring the code to be more presentable here. The correct iteration was data[i].xxxx . That being said, wherever I placed the .draw() API has not made any difference. Anytime I try to row.add() ,it just adds a blank row with no values. I can append data the html way as
but then when I download the table as .csv those rows do not appear
below is the exact response I receive ,copied from the browser console ;
maybe it's how I initiate the DataTable or something else...
Thanks in advance!
This how it is
small update , I just changed the code like this (still uses regular <tr>...</tr> method)
although I don't fully understand the logic ,it works
Please provide a test case with an example of your data and the
success
code you are using. This will allow us to help debug. We don't need the ajax request.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I built a simple example based on your code snippets. It works:
http://live.datatables.net/wolilenu/1/edit
Kevin