要让制表器(例如 DataTables)与 Select2 结合使用报头过滤器,你需要进行以下步骤:
change
事件中调用 table.draw()
方法。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DataTables with Select2 Filter</title>
<link href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
</head>
<body>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th>Filter</th>
</tr>
</thead>
<tbody>
<!-- 表格数据 -->
</tbody>
</table>
<select>
<option value="all">All</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
<script>
$(document).ready(function() {
var table = $('#example').DataTable();
$('select').select2();
$('select').on('change', function() {
table.draw();
});
});
</script>
</body>
</html>
通过以上步骤,你可以成功地将 Select2 与 DataTables 结合使用,实现报头过滤功能。
领取专属 10元无门槛券
手把手带您无忧上云