How to retrieve cached row data?
How to retrieve cached row data?
Example Callback Code
Consider the following code -- I didn't include the table initialization or a working demo, since it shouldn't be necessary for this question.
function customDrawCallback (settings) {
var api = this.api();
// iterate over column values
api.column(0, {page:'current'}).data().each(function(text,rowNum,stack){
var current_row = api.row(rowNum,{page:'current'}).data(); // <-- problem
});
}
Known
The
column()
iterates over the table using the order as it appears on the screen, as opposed to how it was initiated. This is good and preferred. The table above has ordering based on column 0.Using
row()
, though, pulls the data not from cache/rendered view, but from the data as the table was initialized. That hurts. There arerow().cache()
functions, but I haven't determined how I can retrieve the data of a cached row. For instanceapi.rows(rowNum).cache('order')
does not work.
Alternative Approaches
- I could use vanilla JS or jQuery (e.g.,
$table.find('tr').eq(rowNum)
) to parse the DOM, but if this data already exists somewhere, I'd rather keep the page somewhat performant by not doubling the same work. - I could get the rows up front and work from that dataset, but it is just an alternative approach:
function customDrawCallback (settings) {
var api = this.api(),
rows = api.rows({page:'current'}).data();
// iterate over column values
api.column(0, {page:'current'}).data().each( function( text, rowNum, colStack ){
var current_row = rows[rowNum];
});
}
Unsuccessful Attempts
These are things I've tried, which haven't had any success:
api.row( rowNum, {order: 'current'} );
// note: 'current', 'index', 'applied', 'original' all have the same affect
applied
andoriginal
are just aliases ofcurrent
andindex
. I was hopeful thatcurrent
would work, since by the documentation, that is what I'm after, but it isn't working here (not in thedrawCallback
).
This describes the problem, that when including the row-selector
and the selector-modifier
(ordering), together, the selector-modifier
is not applied. If that is the case, there may be a bug in the code, which I'll have to look into.
This question has an accepted answers - jump to answer
Answers
I might be missing it, but I'm not sure you've stated what the problem is. You note that there is a problem with
row().data()
, but not what the problem actually is (a test case would probably clarify that ;-) ).However, you might want to look at the
cells().render()
method to get the displayed data (assuming I understand the issue).Allan
I'll try to clarify the post above when I have a second.
The problem is that
row(rowNum, {page:'current'} )
doesn't return the row of the sorted data (how it's rendered), butrow({page:'current'})
does; however, it returns all rows.He desire is to retrieve the rendered data, by an index.
Does that make sense?
The
row()
method will only ever return a single row.rows()
will return many.Use the
:eq()
selector - for example::eq(0)
to get the first row per the DOM order, not the data index order.Allan
row() vs rows()
Yes, I think my use of
row
in both cases was a mistype and supposed to be plural - I'm not sure that my mobile device was the culprit, or if it was oversight on my part. Unfortunately, I can't go back and edit the post, but I think you understood (hopefully, other viewers now do too).Opportunity for Improvement
Still, the
rows()
without a number specified is in render-order, but when a selector is given, it doesn't honor the ordering of the selector-modifier, which I think it should. In my hasty opinion, it should return the selector-modifier should order the rows, and the selector should be able to cherry pick that. In my case, the selector-modifier waspage:'current'
, which should also applyorder:'current'
by default, and passing a rownum of 2 should give me the 3rd row of the render-ordered set.:eq a Savior?
Thank you! I'll give the
:eq
a shot when I am back to my working example (different computer). I have a feeling (and high hopes) that will work, but really appreciate the timely response.Background
The goal of this exercise was to dynamically add a new total column to a datatable, without affecting the source data. I found that it was too expensive to do this, since there were issues with adding a header, as well with auto-width calculations since the data was only in the DOM and not the source for the DataTable.
The eventual solution was to have a hidden total column by default (included in the source), with toggled visibility.
I don't believe that the behaviour of passing an integer as the row selector should change based on the options given for the
selector-modifier
. An index will always select based on the row data index. If you want anything else then a selector such as the:eq()
modifier should be used. Possibly I should introduce another option such as{i}:order
which might be a little easier.Allan