Dynamically change page length

Dynamically change page length

robin1robin1 Posts: 4Questions: 2Answers: 0

Hi,
I want to dynamically change the rows count when user goes into the page. For example; the table has 5 pages. I want to show 5 records in page1 and 7 records in page 2.... I tried below code but not working. It always returns the same count.

var oTable = $('#example').DataTable( {

     "fnDrawCallback" : function() { 

        if ( typeof oTable != 'undefined' )  {                          
            var rowCount = getRowCount(oTable);             
            redrawWithNewCount(oTable, rowCount);                           }   
    }

});

function redrawWithNewCount(t, row_count)

{

//Lifted more or less right out of the DataTables source

var oSettings = t.fnSettings();
oSettings._iDisplayLength = parseInt(row_count, 10);
t._fnCalculateEnd( oSettings );

/* If we have space to show extra rows (backing up from the end point - then do so */

if ( oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay() )

{

  oSettings._iDisplayStart = oSettings.fnDisplayEnd() - oSettings._iDisplayLength;

  if ( oSettings._iDisplayStart < 0 )

  {

    oSettings._iDisplayStart = 0;

  }

}



if ( oSettings._iDisplayLength == -1 )

{

  oSettings._iDisplayStart = 0;

}

t.fnDraw( oSettings );



return t;

}

Also I tried below function and didn't work.

oTable.on( 'page', function () {

    var rowCount = getRowCount(oTable);
    redrawWithNewCount(oTable, rowCount);

} );

Any help would be appreciated.

Replies

This discussion has been closed.