How can I pass my own parameter to the callback fu

2019-08-07 04:08发布

问题:

According to the definition of callback in JQuery pagination plugin's documents. It seems that it only get tow parameters of its own.So, how can I pass extra parameter to the callback function.

callback

A callback function that is called when a user clicks on a pagination link. The function receives two parameters: the new page index and the pagination container (a DOM element). If the callback returns false, the event propagation is stopped. Default value: function(){return false;}. This callback function is essential for the functionality of the pagination! It should contain code that updates your content. For a fast user experience you should NOT load content via AJAX in this function. Instead, pre-load some content pages and switch between them with this function.

e.g code of plugin usage [ from its demo]:

attach pagination plugin:

$("#News-Pagination").pagination(122, {
    items_per_page:20,
    callback:pageselectCallback
});

callback function:

    /**
     * Callback function that displays the content.
     *
     * Gets called every time the user clicks on a pagination link.
     *
     * @param {int}page_index New Page index
     * @param {jQuery} jq the container with the pagination links 
     *                 as a jQuery object
     */
     function pageselectCallback(page_index, jq){
       var new_content 
         = $('#hiddenresult div.result:eq(' + page_index + ')').clone();
       $('#Searchresult').empty().append(new_content);
       return false;
     }

Thank you very much!!

回答1:

Use a custom wrapper function, as is common in JS, to achieve that.

callback: function(jq1, jq2, ..., jqN) {
  return pageselectCallback(your1, your2, jq1, jq2, ..., jqN);
}

Here jq1 through jqN are the parameters given by jQuery pagination plugin, while your1, your2 are parameters you specify yourself.

IMPORTANT: The above is not valid JS syntax due to the ellipsis ..., - change to match your situation