fnPageChange should allow numbers

fnPageChange should allow numbers

ragulkaragulka Posts: 3Questions: 0Answers: 0
edited August 2011 in Feature requests
It would be extremely useful if the fnPageChange would allow to chnage the page by setting the page number, not just strings like 'next' or 'previous'

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited August 2011
    good point, and it's easy enough to write an API extension function to accomplish this.

    I had written a library of convenience functions ("Flexible"), I should post it. but here is the code for page change:


    usage:
    [code]
    oTable.Flex_page(mPage); // or aliased to oTable.DTpage(mPage)
    // mPage can be int or:
    // 'first', '<<', 'f'
    // 'last', '>>', 'l'
    // 'prev', '<', 'previous', 'p'
    // 'next', '>', 'n'
    // '+n', '-n' to move forward or backward by n pages
    [/code]

    [code]
    FLEX_SHORTNAMES = true; // disable or delete this to not include short names

    if (FLEX_SHORTNAMES)
    $.fn.dataTableExt.oApi.DTpage = DT_Flexible_Plugin.DTpage;
    $.fn.dataTableExt.oApi.Flex_page = DT_Flexible_Plugin.DTpage;

    if (FLEX_SHORTNAMES)
    $.fn.dataTableExt.oApi.DTpages = DT_Flexible_Plugin.DTpages;
    $.fn.dataTableExt.oApi.Flex_num_pages = DT_Flexible_Plugin.DTpages;



    /*
    * DTpage() / Flex_page() - get/set page in dt (0-indexed!)
    *
    * arguments:
    * [optional] page - an integer OR
    * string "first" or "f" or "<<" for first page
    * string "last" or "l" or ">>" for last page
    * string "next" or "n" or ">" for next page
    * string "previous" or "prev" or "p" or "<" for previous page
    * string "+"+integer for current + integer page
    * string "-"+integer for current - integer page
    *
    * returns: page int
    */
    DT_Flexible_Plugin.DTpage = function ( oSettings, iPage ) {
    var current_page = 0;
    var last_page = 0;
    var page_size = oSettings._iDisplayLength;
    if (page_size) current_page = Math.floor(oSettings._iDisplayStart / page_size);
    if (page_size) last_page = Math.ceil( oSettings.fnRecordsDisplay() / page_size) - 1;

    if (typeof iPage == 'string') {
    iPage = iPage.toLowerCase();
    if (iPage == "first" || iPage == "f" || iPage == "<<") iPage = 0;
    else if (iPage == "last" || iPage == "l" || iPage == ">>") iPage = last_page;
    else if (iPage == "next" || iPage == "n" || iPage == ">" || iPage == "+") iPage = current_page + 1;
    else if (iPage == "previous" || iPage == "prev" || iPage == "p" || iPage == "<" || iPage == "-") iPage = current_page - 1;
    else if (iPage.charAt(0) == "+" && !isNaN(iPage.substring(1))) iPage += iPage.substring(1);
    else if (iPage.charAt(0) == "-" && !isNaN(iPage.substring(1))) iPage += -(iPage.substring(1));
    }

    if (typeof iPage == "number") {
    // bounds checking
    if (iPage > last_page) iPage = Math.floor(last_page);
    if (iPage < 0) iPage = 0;
    }

    // move to that page number if different than current_page
    if (iPage && iPage != current_page) {
    oSettings._iDisplayStart = (iPage) * page_size;
    current_page = iPage;
    oSettings.oInstance.fnDraw(false);
    }

    return current_page;
    }


    /*
    * DTpages() - get number of pages in dt
    *
    * arguments: none
    *
    * returns: page int
    */
    DT_Flexible_Plugin.DTpages = function ( oSettings, iPage ) {
    if (typeof iPage != "undefined") { "you passed an argument to DTpages / Flex_num_pages. did you mean to call DTpage / Flex_page?"; debugger; }
    var last_page = 0;
    var page_size = oSettings._iDisplayLength;
    if (page_size) last_page = Math.ceil( oSettings.fnRecordsDisplay() / page_size);

    return last_page;
    }


    [/code]
This discussion has been closed.