josecebe / twbs-pagination

jQuery pagination plugin (bootstrap powered)
http://josecebe.github.io/twbs-pagination/
Other
1.1k stars 405 forks source link

Not re-initialize after destroy #189

Closed nielslucas closed 4 years ago

nielslucas commented 5 years ago

Hi,

I have an problem with re-initialize. Cause it is not showing the pagination.

here is my code:

let page = $('.tags-pagination');
let total = 5;

page.twbsPagination({
    totalPages: total,
    onPageClick: function (evt, page) {
        doSomething(data, false);
    }
});

page.twbsPagination('destroy');

page.twbsPagination({
    totalPages: total,
    ]onPageClick: function (evt, page) {
        doSomething(data, false);
    }
});

very simple test, create it, destroy it, create it again. If I dont destroy it and call it again, then the pagination works just fine. But i need to destroy it for search results and the page count will change.

information:

/*
 * jQuery Bootstrap Pagination v1.4.2
 * https://github.com/josecebe/twbs-pagination
 *
 * Copyright 2014-2018, Eugene Simakin <john-24@list.ru>
 * Released under Apache-2.0 license
 * http://apache.org/licenses/LICENSE-2.0.html
 */
ExLuzZziVo commented 5 years ago

@nielslucas This happens because of memory leak in line 94: this.$element.removeData('twbs-pagination'); When you call destroy method second time and initialize new pagination, variable "data" in line 318 takes recently destroyed pagination value: var data = $this.data('twbs-pagination'); The solution of this problem is:

  1. Put your pagination container inside some div:
    <div id="pag">
        <div id="pagination-container"></div>
    </div>
  2. After calling $pagination.twbsPagination('destroy'); remove pagination container $pagination.remove();,
  3. Add, asign and init it again $('#pag').html('<div id="pagination-container" class="d-flex justify-content-center"></div>'); $pagination = $('#pagination-container'); $pagination.twbsPagination(paginationOptions);
jurchiks commented 3 years ago

For some reason, this does not happen to me. My code is:

<ul id="sm_pagination"></ul>
const $pagination = $('#sm_pagination');

function setupPagination(pagination)
{
    const totalPagesCount = Math.max(Math.ceil(pagination.nbResults / ROWS_PER_PAGE), 1);
    const visiblePagesCount = Math.min(totalPagesCount, 5);

    $pagination.twbsPagination('destroy');
    $pagination.twbsPagination({
        startPage: pagination.currentPage,
        totalPages: totalPagesCount,
        visiblePages: visiblePagesCount,
        initiateStartPageClick: false,
        onPageClick(event, page) {
            loadTable(page);
        }
    });
}

setupPagination() is called on every page click, but it seems to be doing just fine.

sakandemirpttem commented 2 months ago

Just use <ul>instead <div> as container.