我创建了一个示例表并在该表上应用了
datatable
。但它给了我错误“无效的列计数”,但标题和正文中的列计数相同。我需要通过连接 sql
表来生成表。因此,我希望如果 Gridview
div 中的表格长度大于 0,那么只有 datatable
应该适用。下面是代码<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="GridView">
<table>
<tr>
<th>Sno</th>
<th>user</th>
<th>delete</th>
</tr>
<tr>
<td>1</td>
<td>Dummy Name</td>
<td><a>Delete</a></td>
</tr>
</table>
</div>
<script src="Scripts/jquery-3.6.4.min.js"></script>
<link href="Scripts/datatables.min.css" rel="stylesheet" />
<script src="Scripts/datatables.min.js"></script>
<script>
$(document).ready(function () {
if ($('#GridView').find("table").length > 0) {
$('#GridView').find("table").DataTable();
}
});
</script>
</form>
</body>
</html>
thead
标签内,并将数据行放在
tbody
标签内,如下所示:
$(document).ready(function () {
if ($('#GridView').find("table").length > 0) {
$('#GridView').find("table").DataTable();
}
});
<link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.js"></script>
<div id="GridView">
<table>
<thead>
<tr>
<th>Sno</th>
<th>user</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Dummy Name</td>
<td><a>Delete</a></td>
</tr>
</tbody>
</table>
</div>