I want to update 1 column periodically after loading datatable
I want to update 1 column periodically after loading datatable
arsalansiddiqui
Posts: 128Questions: 15Answers: 0
Hi all,
I want to update only single column via ajax. Datatable is initialized first from database then after loading all data i want to periodically update one column with another ajax source.
I've created a function and i called it after:
$(document).ready(function(){
function is simple i called a get link which gives me id and data, id is which row cell i want to update.
now the problem is how to display data where id is
Answers
If you are using
rowId
you can userow()
with arow-selector
with the id of the row as shown in the docs. Userow().data()
to get the data, update it then userow().data()
to update the table.Kevin
yes i'm using rowId, but i'm lacking to implement periodically updating/replacing cells.
first data is loaded from database then ajax is called that takes some time to load.
I store ajax data into a variable with key value pairs in json
like a=id of row and b = value which i want to put into 4th column.
In the ajax
success
function use the above to get and update the row data. Here is a pseudo code example:Se an example here:
http://live.datatables.net/gazadeto/1/edit
Kevin
I'm getting error:
TypeError: tbl1.row is not a function
and one thing more that, i want to loop through it, there are hundreds of cells i want to replace
tbl1 is my table id
Is the variable
tbl1
within the scope of the success function? If not then use something likevat tbl1 = $('my table').DataTable();
to get an instance of the API, see this doc for more details. Also look at this FAQ.You can use a standard Javascript loop of your choice for the loop. This is not Datatables specific.
You can also use the
cell()
API to do the same withcell().data()
.Kevin
Now i'm getting this error, one thing more that the row i want to update is null and looks like this
{"data":null,orderable:false,searchable:false,"render":function(data,type,row,meta){
return null;
}},
so how can i make it work on column 4 ?
And i also initialize tbl1 on top. then previous error gone.
Looks like the column will always display a null value.
Maybe use
cell()
instead ofrow()
.Kevin
yes because these values are not come with database so i have to make it null.
so i have to give id to every cell ?
No need. Per the
cell()
docs you can provide a row usingrow-selector
(the same as therow()
API) and acolumn-selector
. So something liketable.cell( '#' + id, 4 )
.defaultContent
is meant for situations where there is no data. Use it instead.Kevin
default content reference page is giving 404
Sorry its
columns.defaultContent
. You can find all the options, api's and events in the reference page.Kevin
var cell = tbl1.cell( '#row-' + cpu_array.a, 4 );
// tbl1.cell('#row-' + cpu_array.a).data(cpu_array.b);
//console.log(cell);
is it correct ?
Don't know, does it work?
Without seeing the data and the rest of the code its hard to say if its correct. Likely you don't need the
row-
unless that is used as part of your original data for theid
.Please post a link to your page or a running test case showing what you have so we can offer suggestions.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
link is:
http://webapp1.galaxy.net.pk/
it shows some array data when i console.log the cell values
i'm doing these in crawlCpu function
Looks like your site is not publicly available. I get this error:
I updated my above example to show how to use
cell()
.http://live.datatables.net/gazadeto/2/edit
Kevin
My recommendation is to start by doing something like I show in the example to update one cell. Once you can do that, then writing the code to update with you loop should be much easier to figure out.
Kevin
Maybe, this site is hosted locally so i've changed dns entry to public ip, it will resolve in some time publicly.
I've changed my code, now i'm getting
Uncaught TypeError: Cannot read properties of undefined (reading 'draw')
at (index):162
I do not implement loop yet
line 64 is:
cell.data( cpu_array.b ).draw();
This means something with
cell.data( cpu_array.b ).draw();
is undefined. Without seeing the rest of the code its hard to say. Use the browser's debugger and set a breakpoint for this line. You should then see what is undefined.Kevin
cpu_array.a and cpu_array.b is undefined.
i tried directly like:
var cell = tbl1.cell( '#row-' + 24, 4 );
cell.data( 'Updatad' ).draw();
and it does not works
i can see updated text in the respective column when i statically define values but it fades after drawing the table
var cell = tbl1.cell( '#row-' + 24, 4 );
cell.data( 'Updatad' ).draw();
i think dns is updated, can you flush dns entries and recheck ?
if you are on windows, open cmd and type ipconfig /flushdns then enter
it will resolve to ip 103.146.136.133
I updated the example with
data: null
. There doesn't seem to be a way to access this column withcell()
. One option is to set thecolumns.data
to an object name. If you setcolumns.default
to""
for example anddata: "my_data"
then it will work."my_data"
can be any name that is in the original dataset or one that doesn't exist. In the example it doesn't exist.http://live.datatables.net/gazadeto/3/edit
Kevin
i've already update it. but same result, row updates then data fades after redraw.
this is my code now:
var cell = tbl1.cell( '#row-' + cpu_array['data'][0]['a'], 4 );
//console.log(cell);
console.log( cpu_array['data'][0]['b'] );
cell.data( cpu_array['data'][0]['b'] ).draw();
http://webapp2.galaxy.net.pk/
try this