Assistance with fnPageChange...

Assistance with fnPageChange...

applapsapplaps Posts: 12Questions: 0Answers: 0
edited August 2013 in General
Hello,

What I am trying to do is have the datatable point the user to page one on each new search (i.e. each time the user clicks submit):

I tried to attach the fnPageChange to the onlick event, but cannot get it to work:

jsp:
[code]onclick="setDatalistPageOne()"[/code]


js:

[code]function setDatalistPageOne() {
var dataTable = $(#dataList).dataTable();
dataTable.fnPageChange('first', false);[/code]


My datatable gets initialised in the "$(document).ready(function() {" event. Eg:

[code]
$(document).ready(function() {
var dataTableInit = $("#dataListSearchSubject").dataTable( {
"bAutoWidth": true,
"bProcessing": true,
"bServerSide": true,
"bStateSave": true,
etc. etc etc. ,
"fnServerData": function ( sSource, aoData, fnCallback ) {
*some code in here*
}
});
} );
[/code]

Should i be calling fnChangePage in a similar way to the "fnServerData" - should it be outside the initialisation but still in the document.ready or should it be in an onclick like I have, but I am doing it wrong? I am a beginner to DataTables and this is some existing (working) code I have to maintain...

NOTE: I do need bStateSave to be true; so setting that to false won't solve my issue..

Thanks heaps

Replies

  • applapsapplaps Posts: 12Questions: 0Answers: 0
    Ok, so I tried just adding the function in the onload and that doesnt work...

    "dataTableInit.fnPageChange('first',false);"

    behaviour is as follows:

    1. Search for a people with name 'BOB'
    2. returns several pages of people containing Bob in their name
    3. Go to page 2 of results
    4. Search for a specific Bob (should only return one result)
    5. Page is set as 1, however a note is displayed saying no search results found (which is incorrect cause I know this person exists)
    6. If I then just click search again, the search is performed and the correct record is displayed on page 1

    not sure why, ill keep investigating and hope someone posts :(
  • applapsapplaps Posts: 12Questions: 0Answers: 0
    I have also tried setting iDisplayStart to 0 on initialisation of the datatable, but that also hasn't worked:

    [code]"iDisplayStart" : 0,[/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Why are you passing false as the second parameter? That will cause the table to not redraw and thus the page won't appear to have changed.

    Allan
  • applapsapplaps Posts: 12Questions: 0Answers: 0
    edited August 2013
    "I am a beginner to DataTables and this is some existing (working) code I have to maintain..."

    Passing true did the the trick...

    I assumed the initialisation was doing a draw, so I didn't think I would want to redraw it.. (i.e. perform two draws for one search)
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Got it. Good to hear the change worked :-)

    Allan
  • applapsapplaps Posts: 12Questions: 0Answers: 0
    Thanks :)
This discussion has been closed.