gcire
gcire

Reputation: 11

Can jqPagination's paged callback be extended to provide a second parameter indicating which jquery object triggers the callback?

i am looking for a jquery pagination plugin to enable pagination for the content on my web page and i have found jqPagination a very nice plugin. But I have a use case where i need to use multiple instances of the plugin UI to control different groups of content on a same web page, the following is a demo of how I use the plugin. As the javascript code shows, the 2nd parameter i need is the jquery object that triggers the callback. The reason is that the current page number as the single parameter of the callback doesn't allow me to tell which one from the two instances is the source of the event and hence no way for me to control the associated group of content.

    <p>First</p>
    <div id="111" class="pagination">
        <a href="#" class="first" data-action="first">&laquo;</a>
        <a href="#" class="previous" data-action="previous">&lsaquo;</a>
        <input type="text" readonly="readonly" />
        <a href="#" class="next" data-action="next">&rsaquo;</a>
        <a href="#" class="last" data-action="last">&raquo;</a>
    </div>

    <p>Second</p>
    <div id="222" class="pagination">
        <a href="#" class="first" data-action="first">&laquo;</a>
        <a href="#" class="previous" data-action="previous">&lsaquo;</a>
        <input type="text" readonly="readonly" />
        <a href="#" class="next" data-action="next">&rsaquo;</a>
        <a href="#" class="last" data-action="last">&raquo;</a>
    </div>

Javascript:

    <script>
        $(function() {
            $('.pagination').jqPagination({
                max_page: 99,
                page_string: "Deployment {current_page} / {max_page}",
                paged: function(page, jqObject) {
                    console.log("page - " + page + "\njqObject.outerHTML = " + JSON.stringify(jqObject.outerHTML, null, 4) + "\n\n")
                }
            });     
        });
    </script>

I have taken a look at the plugin source code and found passing base.el as the 2nd parameter might be a quick solution for my issue. But I was wondering if this is worth being put into the jqPagination project on github in case someone else will have the similar need. Thanks!

    // ATTN this isn't really the correct name is it?
    base.updateInput = function (prevent_paged) {

        var current_page = parseInt(base.options.current_page, 10);

        // set the input value
        base.setInputValue(current_page);

        // set the link href attributes
        base.setLinks(current_page);

        // we may want to prevent the paged callback from being fired
        if (prevent_paged !== true) {

            // fire the callback function with the current page
            base.options.paged(current_page, base.el);

        }

    };

Upvotes: 1

Views: 47

Answers (0)

Related Questions