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 ?

arsalansiddiquiarsalansiddiqui 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

Answers

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    edited December 2019

    I'm loading datatable from server side with ajax

    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() or rows.add() to add the rows to the table.

    Kevin

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0
    edited December 2019

    I want to add column not, row. I mistakenly type row instead of column.

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    I want to add column not, row. I mistakenly type row instead of column.

    You can only define the columns at Datatables initialization using columns. You can use columns.defaultContent to default the column to blank data.

    I want to get username forward it to ajax then put the result in the empty column.

    You can use row().data() or cell().data() to updated a row or specific cell with the fetched data. You use a row-selector or cell-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

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0

    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.

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    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

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0
  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    edited December 2019 Answer ✓

    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 the selector-modifier of {page: 'current'} to get only the rows being displayed. Use pluck() to get the username column. Finally use toArray() to take the plucked data and create a Javascript array. You can then use this array to send as a data 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

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0

    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 ?

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0
    edited December 2019

    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.

This discussion has been closed.