使用 jQuery 对表格行进行分页 [英] Paginate table rows with jQuery

查看:27
本文介绍了使用 jQuery 对表格行进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Dynamics 365 中的客户门户网站.我必须使用 HTML 表中现有数据的分页插件.

We are working with Customer Portal in Dynamics 365. I have to use a pagination plugin with existant data in the HTML table.

当前代码:

<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. Here I have to paginate specific rows (<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);

在我写一堆行之前,我想知道是否有人知道一个插件来只对 rows 包含的内容进行分页.

Before I write a bunch of lines I would like to know if someone knows about a plugin to paginate only what rows contain.

例如我可以使用插件 Pagination.js 就像 文档 说:

For example I could use plugin Pagination.js like the documentation says:

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

对吗?

提前致谢

推荐答案

经过大量研究,我找到了一个可行的解决方案 Pagination.js.

After a lot of researching I found a posible solution with 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>

尽管如此,我还是没有完成这项工作,因为即使使用 $.getScript('url', function() { 加载脚本,.pagination() 方法也不起作用});document.createElement('script') 但我认为这是一个不同的问题并发布...

Still, I don't make this work since the .pagination() method is not working even if loaded the script with $.getScript('url', function() {}); or document.createElement('script') but I think this is a different issue and post...

这篇关于使用 jQuery 对表格行进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆