使用jQuery对表行进行分页

时间:2018-09-27 15:26:41

标签: javascript jquery html pagination dynamics-crm

我们正在使用Dynamics 365中的Customer Portal。我必须使用分页插件以及HTML表中的现有数据。

当前代码:

<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>

这里有一百多条记录。我必须在底部包括一个分页区域,以允许用户对表格进行分页。

在Internet上进行挖掘时,我看到有多个插件可以实现分页,但是它们的数据源通常是对象数组。在这里,我必须对特定的行(<tr>)进行分页。

到目前为止,我已经尝试过:

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

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

例如,我可以使用插件{strong> Pagination.js ,如documentation所说:

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

对吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

经过大量研究,我发现了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>

不过,由于.pagination()方法无法正常工作,即使使用$.getScript('url', function() {});document.createElement('script')加载了脚本,我也没有用,但是我认为这是一个不同的问题, ...