我使用 DataTables 对包含用户数据的表进行分页和排序。
完整的数据集通过 .NET 应用程序来自 MySQL 数据库。
表的第一列必须包含(当前)行计数,以使计数“免疫”重新排列行(换句话说,排序)。这就是我使用基于 CSS 的行计数的原因,如下所示:
new DataTable('#employees', {
info: false,
paging: true,
"aLengthMenu": [5, 10, 20],
// "dom": 'rtip',
});
.dt-column-order:before {
margin-bottom: 1px;
}
th {
outline: none !important;
}
.dt-paging {
margin-top: 15px !important;
}
.pagination {
justify-content: flex-end;
}
/* CSS row count */
table {
counter-reset: row-num;
}
table tbody tr {
counter-increment: row-num;
}
table tbody tr td:first-child::before {
content: counter(row-num) "";
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
<div class="container-fluid my-2">
<h2>Data Tables</h2>
<table id="employees" class="table table-bordered table-striped mx-1">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011-04-25</td>
<td>$320,800</td>
</tr>
<tr>
<td></td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011-07-25</td>
<td>$170,750</td>
</tr>
<tr>
<td></td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009-01-12</td>
<td>$86,000</td>
</tr>
<tr>
<td></td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012-03-29</td>
<td>$433,060</td>
</tr>
<tr>
<td></td>
<td>Ștefan Popa</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008-11-28</td>
<td>$162,700</td>
</tr>
<tr>
<td></td>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012-12-02</td>
<td>$372,000</td>
</tr>
<tr>
<td></td>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012-08-06</td>
<td>$137,500</td>
</tr>
<tr>
<td></td>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010-10-14</td>
<td>$327,900</td>
</tr>
<tr>
<td></td>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009-09-15</td>
<td>$205,500</td>
</tr>
<tr>
<td></td>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008-12-13</td>
<td>$103,600</td>
</tr>
<tr>
<td></td>
<td>Jena Gaines</td>
<td>Office Manager</td>
<td>London</td>
<td>30</td>
<td>2008-12-19</td>
<td>$90,560</td>
</tr>
<tr>
<td></td>
<td>Quinn Flynn</td>
<td>Support Lead</td>
<td>Edinburgh</td>
<td>22</td>
<td>2013-03-03</td>
<td>$342,000</td>
</tr>
</tbody>
</table>
</div>
这个解决方案有一个问题:每当结果集足够大以至于需要分页时,每页的行数都会从 1 开始。
所以,我实际上需要基于 JavaScript 的行计数,这不会重置每个页面的计数。我没有找到一种方法可以将其插入到数据表中。
问题document
,因为
<table>
以及层次结构中更高的元素可能被销毁和创建。无论如何,尝试获取正确的页面并通过查找当前位于
<button>
中的
<li class="active">
来获取索引号是很棘手的。现在正确的索引是正确的,我使用了一个包含 3 个数字的数组,这 3 个数字是每页的起始行号。最后,我得到了一个 CSS 变量
--start
来更改
counter-set
的值,这也是分配给
counter-reset
的
<table>
的第二个值。因此,每次页面更改时,该值都会从 0 更改为 5,再更改为 10。
new DataTable('#employees', {
info: false,
paging: true,
"aLengthMenu": [5, 10, 20],
// "dom": 'rtip',
});
document.addEventListener("click", adjCounters);
function adjCounters(e) {
const clk = e.target;
if (clk.matches("button")) {
const act = document.querySelector(".active .page-link")
const ref = [0, 5, 10];
const idx = Number(act.getAttribute("data-dt-idx"));
act.style.cssText += "outline:3px red dashed";
console.log(idx)
const pgs = ref[idx];
const table = document.querySelector("table");
table.style.setProperty("--start", pgs);
}
}
:root {
--start: 0
}
.dt-layout-full {
padding: 0 !important;
}
.dt-column-order:before {
margin-bottom: 1px;
}
th {
outline: none !important;
}
.dt-paging {
margin-top: 15px !important;
}
.pagination {
justify-content: flex-end;
}
/* CSS row count */
table {
counter-reset: row-num var(--start)
}
table tbody tr {
counter-increment: row-num;
}
table tbody tr td:first-child::before {
content: counter(row-num) "";
}
.as-console-wrapper {
left: auto !important;
top: 0;
width: 60%;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.min.js"></script>
<script src="https://cdn.datatables.net/2.1.8/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
<div class="container-fluid my-2">
<h2>Data Tables</h2>
<table id="employees" class="table table-bordered table-striped mx-1">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011-04-25</td>
<td>$320,800</td>
</tr>
<tr>
<td></td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011-07-25</td>
<td>$170,750</td>
</tr>
<tr>
<td></td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009-01-12</td>
<td>$86,000</td>
</tr>
<tr>
<td></td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012-03-29</td>
<td>$433,060</td>
</tr>
<tr>
<td></td>
<td>Ștefan Popa</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008-11-28</td>
<td>$162,700</td>
</tr>
<tr>
<td></td>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012-12-02</td>
<td>$372,000</td>
</tr>
<tr>
<td></td>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012-08-06</td>
<td>$137,500</td>
</tr>
<tr>
<td></td>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010-10-14</td>
<td>$327,900</td>
</tr>
<tr>
<td></td>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009-09-15</td>
<td>$205,500</td>
</tr>
<tr>
<td></td>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008-12-13</td>
<td>$103,600</td>
</tr>
<tr>
<td></td>
<td>Jena Gaines</td>
<td>Office Manager</td>
<td>London</td>
<td>30</td>
<td>2008-12-19</td>
<td>$90,560</td>
</tr>
<tr>
<td></td>
<td>Quinn Flynn</td>
<td>Support Lead</td>
<td>Edinburgh</td>
<td>22</td>
<td>2013-03-03</td>
<td>$342,000</td>
</tr>
</tbody>
</table>
</div>