Is there a way to load a row from ajax after datatable loads bu using username or id ?
Is there a way to load a row from ajax after datatable loads bu using username or id ?
arsalansiddiqui
Posts: 128Questions: 15Answers: 0
Hi, i want to load a row of datatable from ajax. I'm loading datatable from server side with ajax but i also want a row to get value after initialization and get value against username or id then fill the empty column rows with value with ajax data.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Does this mean you have
serverSide: true
in your config?If so then you won't be able to update the client side table as all the row data comes from the server.
If not then you can use a jQuery Ajax request to fetch the data then use
row.add()
orrows.add()
to add the rows to the table.Kevin
I want to add column not, row. I mistakenly type row instead of column.
I can add a blank column, but how do i put data there ?
I want to get username forward it to ajax then put the result in the empty column.
You can only define the columns at Datatables initialization using
columns
. You can usecolumns.defaultContent
to default the column to blank data.You can use
row().data()
orcell().data()
to updated a row or specific cell with the fetched data. You use arow-selector
orcell-selector
to choose the row or cell to update.If you need more help please provide more details of your Datatablers config and how you want to update the data.
Kevin
This is what my datatable looks like, i previously like to add column but it is more preferable to change color of usernames.
I want that whenever the datatable loads or i do pagination from one page to another or id do search some function will get value of username column and pass it to an ajax function which will do backend job and return status like 0,1,2 so on receiving data i want that username color will change according to status.
The best place to do that would be in the
draw
- this would get called in all those conditions you listed above. You could issue an ajax request there to do that messaging.Colin
https://datatables.net/reference/event/draw
doesn't help much.
I have used on draw event in this table so how do i get all username one by one and pass it to ajax function this is the first part of the question.
You can use
rows().data()
to get the row data. Use theselector-modifier
of{page: 'current'}
to get only the rows being displayed. Usepluck()
to get the username column. Finally usetoArray()
to take the plucked data and create a Javascript array. You can then use this array to send as adata
parameter in your ajax request. You can chain all those APIs in one line.I would consider alternatives, if possible, to fetching the data each time the page changes. Does the status change? Can you provide the status in the original JSON response as part of the row data?
Kevin
I recently start using datatables and this solution i find too hard to implement without help/guidance. Do you have any working demo where i take a look ?
Thanks for your reply @kthorngren, i will look into it later as i skip this task for later. So my project will not be delayed.