Iterate through every cell in a given column
Iterate through every cell in a given column
mehti
Posts: 35Questions: 13Answers: 0
Hi guys,
I am trying to iterate through every cell for a given column
http://datatables.net/reference/type/cell-selector
I used below code, however for some reason it is not working properly.
table.column(4).nodes().each(function (cell, i) {
console.log(table.cell(i, 2).data());
});
You can see the web page below, just move the slider and you'll see the output of the 2nd visible column.
[retracted]
Many thanks
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Looks like it is working to me - the console shows the data from the second column in the table. What were you expecting / looking for?
Allan
Hey Allan,
Thank you for your response. I actually found the issue not sure how to resolve it though.
The issue is with the order in which the results appear.
Eventhought I've ordered the table by 2nd column they appear in order if the table was ordered by 1st column. Also I use value from 2nd column to do calculations and output result into 4th and 5th columns. Because of the mismatch with the order in which they appear calculated values in 4th and 5th column are incorrect. If I order table by 1st column it all works.
By ordering I mean clicking on the column header.
Thanks for the clarification. By default the selectors will use the data order of the table. If you want to use the current sort order, you need to use the
selector-modifier
options of thecolumn()
method.Allan
Thank for your response. I read the API and my understanding is that I don't need to set selector modifier as by default it would be current which is to process rows in the order currently applied to the table, however even if I do set modifier like this, it still doesn't do in the order that the table currently sorted.
table.column(4, {order:'current'}).nodes().each(function (cell, i) { console.log(table.cell(i, 2).data());
});
Mehti
Sorry for sooo many questions... just trying to get to the bottom of this...
I have a follow up question, which I think might be the reason why it isn't working for me. If I set a filter on range (say column 7 (min) and 8 (max)) in my table and then want to iterate through the visible rows but pick values from column 4. Would selector-modifier need to be applied to the column(column 4) that i'm iterating through or to the column where the filter was applied (column 7 and 8)?
Hope this makes sense.
okay, i finally fixed the issue... thank you Alan again for your suggestion!!
Here is how I fixed it.
table.rows({search: 'applied'}).eq(0).each(function (i) {
//some code here
}).draw();