Set a (filtered) column to a value - best method?
Set a (filtered) column to a value - best method?
vogomatix
Posts: 38Questions: 3Answers: 7
Having applied a filter to a table, what is the best way to set all filtered elements of a column of that table to a value?
I currently have:
/* set value in price overrides */
var cell, i;
var cells = api.cells( undefined, api.columns('.columnClass'), {search: 'applied'}).flatten();
for (i = 0; i < cells.length; i++) {
cell = cells[i];
api.cell(cell).data(newValue);
}
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Looks good to me! You could use
each()
rather than a for loop, but the principle is the same:Allan
@allan I think there is a problem with setting cells on row 0 in 1.10.0
The following code does not realise it is a cell index if the cell is in row 0...
The result appears to be that it sets cell(0,0) instead of cell(0,x)
There is yes. That has been fixed in 1.10.1 which has just been released :-)
Allan
@allen Thankyou (I also noticed this change after I wrote the above - regrettably cannot upgrade at the moment, have solved problem by direct use of cell row and column values.
@allen,
I'm also noticing some strangeness. But I am new to datatables so I may be misunderstanding.
I'm trying to update a single column but instead of updating the column I chose, it updates columns 0 and 1.
I've tried these three different ways to select the column:
var column = $('#row_5 td:nth-of-type(8)');
var cell = table.cell('#row_5', column).data( "test" );
var cell = table.cell('#row_5', 'status:name').data( "test" );
var cell = table.cell('#row_5', 8).data( "test" );
edit: I'm on 1.10.1
To get round the row 0 bug I altered my code to do:
Using x,y coordinates instead of cell objects does not appear to be affected by the bug
@vogomatix,
Thanks a ton for the reply. Knowing what works helped me narrow down what my issue was.
For anyone else, my problem was I didn't understand that when you do
cell.data(newValue)
, DataTables runs through yourcolumnDefs
...specifically anyrender
calls within it that you might have.So I was trying to toggle an on/off button image by actually changing the text to
<img src='on.jpg'>
or<img src='off.jpg'>
(but the data values were 0 or 1). eg:cell.data("<img src='on.jpg'>")
.And in my
render
was anif/then
saying if the data was 0, show "on.jpg" and 1, show "off.jpg". So to fix it, rather than docell.data("on.jpg")
, I had to docell.data(1)
and let myrender
do it'sif/then
.(I shortened cell.data but it's the full call w/ row/col/etc.)
The
table.cell('#row_'+iID, 'status:name')
(where "status" is thename
value I gave it within mycolumnDefs
) notation didn't work at all and I had to use @vogomatix's method of just using a number for the column (which isn't actually the column, but rather the index into therow
param within myrender
within mycolumnDefs
).I'm still learning, so this might be a really bad way of doing it. But it works for me.