Is it possible to connect datatables to Node.js server ?
Is it possible to connect datatables to Node.js server ?
jemz
Posts: 131Questions: 40Answers: 1
Hi, can I ask is it possible to use datables to Node.js server ?
Thank you in advance.
This discussion has been closed.
Answers
Sure. You can use anything you want as long as it outputs HTML or JSON.
Allan
Hi @Allan, Thank you for the reply, can I also use this in socket.io ?...I tried searching here but I could not get any examples using socket.io and node.js...I hope you can help me.
Thank you in advance.
If you are using socket.io you would use the API to dynamically add, update and remove rows based on the messages received. Have a look at the API reference.
I don't have a specific example using socket.io - if you are unsure how to use socket.io then I would suggest asking in one of their support channels or StackOverflow. Once you have the JSON data from socket, it is just a case of calling
row.add()
,row().data()
orrow().remove()
.Allan
Hi @allan, I tried to do it in socket.io but I am confuse in the configuration and binding the data to the datatable , I also get error Requested unknown parameter 'devid'.
Thank you in advance.
index.html
Does the
data
object you are adding to the table have adevid
parameter? We'd need more information to be able to offer any help beyond that - what the value ofdata
is for example (you probably need to parse it as JSON).Allan
Hi @allan,
Here is the result from my database http://pastie.org/10190050
yes I used also JSON.parse like this
but still having this Requested unknown parameter 'devid' for row 0
Thank you in advance.
HI @allan ,
I fixed it by using table.rows.add(JSON.parse(data)).draw();
but my browser will crash.after 1 min.
Thank you in advance.
Hi @allan,
can I ask is there a way to load my request data not to use rows.add ?
because I found out that it will keep adding the row.but the original row is only 50 rows,but using rows.add it will continuously add.
Thank you in advance.
If you want to add a new row then
row.add()
is the API method to use.If it is continuously adding new rows incorrectly, then I can only presume that is is being called incorrectly. Perhaps you want to use
row().data()
to update existing row for rows that already exist.Allan
Hi @allan,
I am confuse, how can I update if I have no data yet loaded to to the table.I cannot use row.add because it will continuously added new row. the reason why it added continuously because I have setInterval requesting data to server by 1 sec. without using socket.io I can achieve like this
then calling ajaxreoload in setInveral for 1 sec.
In socket.io I am confuse because I am calling the row.add() method in order to load the data or rows.add() that I requested to server.
Thank you in advance.
Hi @allan,
Is it effecient way to do rows.remove then rows.add() ? I cannot use row.data() to update exsisting row.because I don't know how to load the data without using the rows.add(). just like I am using in normal way wtihout using socket.io
Not really, but if you can't use
row().data()
then you have little choice! Unless you have a large table, I doubt you'll notice the hit.Allan
HI @allan,
How can I load 50 records in my datable ? if I use rows.add() it continously adding 50 records every time my setInterval execute...you said that I will use row().data,so that it will not continously adding 50 records every time my setInterval will run.how do I implement the row().data ? I am confuse in the example in the documentation.
Thank you in advance.
What you need to do is use
row.add()
if you are adding a new row. Androw().data()
if you are updating an existing row. For each row you add / update you will need to check if it exists or not. You could use a selector to do that - for example use theDT_RowId
property to have the DataTable set the row id and then use that as a selector. Check if it was found usingany()
, then use the appropriate method.Allan
please delete this
please delete this
Hi @allan,
I specify the
DT_RowID
property in my data object and the return data is now like thissee http://pastie.org/10196001 .
how do I check every row the
DT_RowId
if it is already exist ?Thank you in advance.
Hi @allan,
I am confuse in using the .any() on how to check my table if there is already existing id in the row?
Thank you in advance.
The
any()
method does not accept any parameters (as noted in the documentation for that method).If you want to use an ID selector then simply do
table.rows('#'+d.DT_RowId).any()
.Allan
Hi @allan,
I almost got the idea but it will only add 1 row this is how I check
Thank you in advance.
That isn't what I suggested above :-)
Allan
HI @allan,
I apologize I have not seen your reply :) ..Thank you so much it works :)
HI @allan,
I just want to ask something, what if the query result from my database will get 50,000 rows?now in client side I will display this to my datatables by using the working code above.my code will check 50,000 times to see if the id exist in row. and run again the setInterval for every 1 second another 50,000 rows will be checked again.
Is this will not affect performance ?
Thank you in advance.
At 50k rows I would suggest using server-side processing - which is probably a fairly significant change for you if you are using sockets.
You are transferring all 50k rows every second?! Wow - that will burn through bandwidth! Can you not just transfer the changes?
Allan
Hi @allan,
How can I apply server-side processing using socket.io ?. can I use this kind of coding.
or still using the row.add() and row.data(). ?this is where I am stuck.
Thank you in advance.
You would need to use
ajax
as a function and have it proxy all requests to the socket. It is not something I have any example code that share I'm afraid.Allan
HI @allan
what do you mean
and have it proxy all requests to the socket
?. I tried to implement like thisbut it says cannot reinitialize datatbles
Yes, you want to initialise the table only once. Use
ajax
as a function (see its documentation for the parameters). That function would then be used to send and receive over the socket.Allan
Hi @allan,
How do I initialize the datables only once. the datables is inside in the socket eventListener
socket.on('getdata')
.If I put outside the datables on the eventListener, how do I bind the data that is coming fromThank you in advance
Move it outside of the listener... :-). Assign the result to a variable and use the API.
Allan