简体   繁体   中英

jQuery Datatable get the page number of element

We have used jQuery Datatables plugin to save update records in multiple rows.

Also validation is built for all the columns, once validation is failed.

We have the following line of code to focus on element for which validation is failed.

$(this).focus();

However this will not work if validation fails on some other page( $(this) is not on current).

As a workaround to this we thought we will click on the page number hyperlink

$(#identifier).trigger('click');

The problem is

"How to know if an element with id A ( $(#A)) is in which page of jQuery Table ?"

Once we know that problem will be solved.

Here, I've written a js function to move you to the page with a given row (row with class "selectedItem" in this case). It should be possible to rewrite the code to find any elements, not just rows, in case you need to.

    function moveToPageWithSelectedItem() {
        var numberOfRows = itemsTable.data().length;
        var rowsOnOnePage = itemsTable.page.len();
        if (rowsOnOnePage < numberOfRows) {
            var selectedNode = itemsTable.row(".selectedItem").node();
            var nodePosition = itemsTable.rows({order: 'current'}).nodes().indexOf(selectedNode);
            var pageNumber = Math.floor(nodePosition / rowsOnOnePage);
            itemsTable.page(pageNumber).draw(false); //move to page with the element
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM