简体   繁体   English

jQuery Datatable获取元素的页数

[英]jQuery Datatable get the page number of element

We have used jQuery Datatables plugin to save update records in multiple rows. 我们已经使用jQuery Datatables插件将更新记录保存在多行中。

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). 但是,如果在其他页面上验证失败($(this)不是当前页面),则此操作将无效。

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). 在这里,我编写了一个js函数,将您移至具有给定行(在这种情况下为类“ selectedItem”的行)的页面。 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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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