How to go to page no by using dataTable API

How to go to page no by using dataTable API

csplrjcsplrj Posts: 2Questions: 0Answers: 0
edited January 2010 in General
How to go to page no by using dataTable API?

On Click of some other link in the page it should goto Page no how to achive this.

Thanks in advance

CSJakharia

Replies

  • shauntainshauntain Posts: 6Questions: 0Answers: 0
    I would like to know this as well, in addition to determining your current page number.
  • allanallan Posts: 63,397Questions: 1Answers: 10,451 Site admin
    HI csplrj,

    There are a couple of plug-ins which might help you along this road: fnPageChange and fnDisplayRow ( http://datatables.net/plug-ins/api ). But there isn't a plug-in which will specifically go to page X. However, it would be fairly trivial to modify the code from the previous suggested plug-ins to do what you need.

    Regards,
    Allan
  • ashwinashwin Posts: 4Questions: 0Answers: 0
    edited March 2012
    This discussion is quite old, but I ran into this problem as well.

    I found a solution that works for me in combination with a ServerSide requests.

    I thought of sharing this with the community, since I guess there are alot of others who would like to know this as well.

    This is the code I run:

    [code]
    var oTable;
    function dCb() {
    $("#gotoPage").remove();
    $(".dataTables_wrapper .dataTables_paginate:visible").append("/"+Math.ceil(oTable.fnSettings()._iRecordsDisplay / oTable.fnSettings()._iDisplayLength)+"");
    $("#gotoPage").die('change').live('change',function(){
    var l = $(".dataTables_length select option")[$(".dataTables_length select")[0].selectedIndex].value;
    var s = (parseInt($("#gotoPage input").val()) - 1) * oTable.fnSettings()._iDisplayLength;
    var sas = oTable.fnSettings().sAjaxSource;
    var a = sas.indexOf("&iDisplayStart=")+"&iDisplayStart=".length;
    var b = sas.indexOf("&",a);
    var url = getTableDataURL();
    if (typeof oTable == "object") oTable.fnDestroy();
    oTable = $("#oTable").dataTable({
    iDisplayStart: s,
    bServerSide: true,
    sPaginationType: "full_numbers",
    sAjaxSource: url,
    fnDrawCallback: dCb
    });
    });
    }
    oTable = $("#oTable").dataTable({
    bServerSide: true,
    sPaginationType: "full_numbers",
    sAjaxSource: url,
    fnDrawCallback: dCb
    });
    [/code]

    Obviously getTableDataURL is a function to gererate my url to the request backend.
  • pablofariapablofaria Posts: 4Questions: 1Answers: 0
    Well, I could not make the code of "ashwin" work, but I managed to change it to the code below, which is working for me:

    [code]
    function dGotoPage() {
    var oSettings = oTable.fnSettings();

    // Configura a exibição do campo
    $("#gotoPage").remove();
    $(".dataTables_wrapper .dataTables_paginate:visible")
    .append("" +
    Math.ceil(oSettings._iRecordsDisplay / oSettings._iDisplayLength) +
    "");

    // Configura o evento
    $("#gotoPage").die('change').live('change',function(){
    // Set new page
    var iPage = (parseInt($("#gotoPage input").val()) - 1) * oSettings._iDisplayLength;
    oSettings._iDisplayStart = iPage;

    // Redraw table
    $(oSettings.oInstance).trigger('page', oSettings);
    oSettings.oApi._fnCalculateEnd( oSettings );
    oSettings.oApi._fnDraw( oSettings );
    });
    }
    [/code]

    One needs just to include the option [code]"fnDrawCallback": dGotoPage[/code] to the Data Tables initialization.
This discussion has been closed.