fnGetData of visible rows only

fnGetData of visible rows only

wazzawazza Posts: 10Questions: 0Answers: 0
edited June 2012 in General
Hello everyone. My question might be really simple and already solved, however I couldn't find the solution within the API docs and forum.

Is there an API method to simply retrieve the aoData of the visible rows? (possibly, fnGetVisibleData )



My task is simple: I have a relatively large table (10k rows, 15 cols) and I have some complicated server-side processing of the rows. Therefore I want to retrive data from the currently visible columns and process only them (50 rows). The solution could be as follows:

- fngetNodes is used for the rows that I currently see and then query them in the datatable
- fnFindCellRowIndexes is used based on the retrived tr nodes
- aoData is retrieved based on found row indexes

this could be absolutely fine, but the column with unique values for selection can be hidden by ColVis. and therefore instead of selecting only 50 rows, I can recieve nearly 5000 of them.



So, is there an easier implemented way, or I should make my own indexing variable and embed it into fnRowCallback?

Replies

  • wazzawazza Posts: 10Questions: 0Answers: 0
    edited June 2012
    PS didn't find the edit button the first time, [resolved]
  • wazzawazza Posts: 10Questions: 0Answers: 0
    The solution is actually very easy, that's a pity no one shared it

    [code]
    var oTable = $("#example").dataTable();
    var anNodes = $("#example tbody tr");

    for (var i = 0; i < anNodes.length; ++i)
    {
    var rowData = oTable.fnGetData( anNodes[i] );

    //some stuff with the obtained data
    //...
    }
    [/code]

    Strange, but I can't feed the DOM array from the selector to the fnGetData, cause it causes an error. Yet, an element-wise cycle is fine.

    The selector is simple yet powerfull, taking into account only the truly shown tr-s, regarding the filtering, sorting, pagination and column-visibility.
  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    edited June 2012
    Actually the solution is simpler than that :-). Use the underscore function:

    [code]
    var data = oTable._('tr', {"filter":"applied"});
    [/code]

    The $ and _ API methods are really useful for parsing through the nodes and data of the table :-)

    Allan
  • fearednerdfearednerd Posts: 44Questions: 0Answers: 0
    so would there be a way to combine this with fnSelect for tabletools so you can select all visible rows?

    i guess kinda like

    [code]
    var oTT = TableTools.fnGetInstance( 'example1' );
    var data = oTable._('tr', {"filter":"applied"});

    for(var i = 0; i< data.length; i++)
    {
    oTT.fnSelect(data[i]);
    }
    [/code]
  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    With TableTools 2.1.1 what you can do to select all visible rows is:

    [code]
    var oTT = TableTools.fnGetInstance( 'example1' );
    oTT.fnSelect( oTable.$('tr', {"filter":"applied"}) );
    [/code]

    This is possible because TableTools 2.1.1 adds the ability to accept jQuery objects for fnSelect (and fnDeselect).

    Allan - trying to make life easier ;-)
  • styletronstyletron Posts: 1Questions: 0Answers: 0
    edited December 2012
    Edit: Nothing to see here.

    I was getting unexpected results when calling oTable.$('tr', {"filter":"applied"}) or oTable._('tr', {"filter":"applied"}). It ended up being that I had bDeferRender set to true, although I double and triple checked that.
This discussion has been closed.