Set column data
Set column data
N0tChris
Posts: 7Questions: 0Answers: 0
in DataTables
Hi,
I'm trying to copy values of selected column to the previous one like that :
function copyColumnLeft(currentColumn, prevColumn) {
const currentColumnData = currentColumn.data()
const currentColumnIndex = currentColumn.index()
if (currentColumnIndex < 1) {
return
}
// Here, I'm trying to copy current column data in previous column but it doesn't work...
prevColumn.data(currentColumnData).draw()
}
This way doesn't work and I didn't find on forums and documentation a great solution to copy values from a column to another...
Thanks for your help!
Replies
The
column().data()
docs state this:So its just a getter.
Datatables doesn't have any API's to bulk copy data from one column to another. You will need to loop through the rows using
rows().every()
then copy the data in the loop. Don't usedraw()
in the loop. After the loop usedraw()
so the table is drawn only once.Kevin
Another option is to use the ColReorder extension. You can use the
colReorder.move()
move the columns. Not sure that is what you want though.Kevin
Thank you for your answer!
I'm trying to apply your solution but I get another problem :
I don't if I should use draw or an other DataTables function to change column values.
I'm not sure what you are trying to do in that function. Here is a simple example of copying one column to the other:
http://live.datatables.net/detameqi/1/edit
Click the button to copy the Office column to the Position column. Is this what you are trying to do?
Kevin
Thank you for your perfect answer.
It fits perfectly with my needs!
Thanks again!