How to pass generic hrefPrefix in SimplePagination Jquery?

613 Views Asked by At

I was recently looking at dynamic paging through javascript in an asp.net application.

I am across Simple Pagination - SimplePagination

I could integrate it into my application easily and I loved it.

But now I have come across a problem and I dunno how to go about it.

The code I have used is :

$(function () {

    var totalPages = @Model.Pagination.TotalPage;
    var prevText = "@Model.Pagination.PrevPageTitle";
    var nextText = "@Model.Pagination.NextPageTitle";
    var currentPage = @Model.Pagination.CurrentPage;
    var edges = 1;

    //For edge display
    var displayedPages = 3;

    $(".paging").pagination({
        displayedPages: displayedPages,
        cssStyle: "light-theme",
        edges: edges,
        pages: totalPages,
        currentPage: currentPage,
        hrefTextPrefix : "?pg=",
        prevText: prevText,
        nextText : nextText,
        selectOnClick : false
    });
});

In the above code, I am stuck with hrefTextPrefix : "?pg=",

The above code should fit 3 types of URL's:

http://example.com/test.aspx -> it works fine here when I use ?pg=

http://example.com/test.aspx? -> here it fails if I use ?pg= ; it should be just pg= here

http://example.com/dynamic/?q=train&r=mono -> here again it fails for ?pg= ; it should be &pg= here

Now my question is - how do I use this hrefTextPrefix such that it always works for any kind of URL. It would be really good if i need not hard-code "pg" and read it from URL or some other way. Please help !!

1

There are 1 best solutions below

1
On

I found an answer for this. Actually I can always use "?pg=" and it works for all URL's by default.

I handled in javascript like this:

   var url = window.location.search;
    url = url.replace(/&?((page)|(pg))=([^&]$|[^&]*)/i, ""); //remove page or pg
    var queryString = url.substring( url.indexOf('?') + 1 );
    queryString = queryString == "" ? queryString : queryString + "&";

     ...
     ...

   $(".paging").pagination({
        displayedPages: displayedPages,
        edges: edges,
        pages: totalPages,
        currentPage: currentPage,
        hrefTextPrefix : "?" + queryString + "pg=",
        prevText: prevText,
        nextText : nextText,
        selectOnClick : false,
        onInit: function (pageNumber) {

        }
    });