简体   繁体   English

使用jQuery对表行进行分页

[英]Paginate table rows with jQuery

We are working with Customer Portal in Dynamics 365. I have to use a pagination plugin with existant data in the HTML table. 我们正在使用Dynamics 365中的Customer Portal。我必须将分页插件与HTML表中的现有数据一起使用。

The current code: 当前代码:

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
            <th>Column 4</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>...</td>
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
        <tr>
            <td>...</td>
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
        ...
    </tbody>
</table>

Here there are more than one hundred of records. 这里有一百多条记录。 I have to include a pagination area in the bottom to allow users to paginate the table. 我必须在底部包括一个分页区域,以允许用户对表格进行分页。

While digging on internet I saw there are several plugins to achieve pagination but their data source are often an object array. 在Internet上进行挖掘时,我看到有多个插件可以实现分页,但是它们的数据源通常是对象数组。 Here I have to paginate specific rows ( <tr> ). 在这里,我必须对特定的行( <tr> )分页。

I have tried so far: 到目前为止,我已经尝试过:

rows = [];
$('table:first tbody tr').each(function(i, row) {
    rows.push(row);
});
recordsPerPage = 20;
pages = Math.ceil(rows.length / recordsPerPage);

Before I write a bunch of lines I would like to know if someone knows about a plugin to paginate only what rows contain. 在写rows之前,我想知道是否有人知道一个插件仅对rows包含的内容进行分页。

For example I could use plugin Pagination.js like the documentation says: 例如,我可以使用插件Pagination.js,文档所述

dataSource: function(done){
    var result = [];
    for (var i = 0; i < rows.length; i++) {
        result.push(rows[i]);
    }
    done(result);
}

Right? 对?

Thanks in advance 提前致谢

After a lot of researching I found a posible solution with Pagination.js . 经过大量研究,我发现了Pagination.js一个可行的解决方案。

 let rows = [] $('table tbody tr').each(function(i, row) { return rows.push(row); }); $('#pagination').pagination({ dataSource: rows, pageSize: 1, callback: function(data, pagination) { $('tbody').html(data); } }) 
 table { border: 1px solid black; } th, td { border: 1px solid black; } td { padding: 5px; } #pagination { width: 100%; text-align: center; } #pagination ul li { display: inline; margin-left: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://pagination.js.org/dist/2.1.4/pagination.min.js"></script> <table> <thead> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> <th>Column 4</th> </tr> </thead> <tbody id="data-container"> <tr> <td>Column 1: Row 1</td> <td>Column 2: Row 1</td> <td>Column 3: Row 1</td> <td>Column 4: Row 1</td> </tr> <tr> <td>Column 1: Row 2</td> <td>Column 2: Row 2</td> <td>Column 3: Row 2</td> <td>Column 4: Row 2</td> </tr> <tr> <td>Column 1: Row 3</td> <td>Column 2: Row 3</td> <td>Column 3: Row 3</td> <td>Column 4: Row 3</td> </tr> </tbody> </table> <div id="pagination"></div> 

Still, I don't make this work since the .pagination() method is not working even if loaded the script with $.getScript('url', function() {}); 不过,由于即使使用$.getScript('url', function() {});加载脚本, .pagination()方法也不起作用,因此我无法执行此操作$.getScript('url', function() {}); or document.createElement('script') but I think this is a different issue and post... document.createElement('script')但我认为这是一个不同的问题,并发布...

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

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