bootstrap-table显示行号

bootstrap-table显示行号

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<head>
<meta charset="utf-8">
<title>bootstrap-table显示行号</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-theme.min.css" rel="stylesheet">
<link href="css/bootstrap-table.css" rel="stylesheet">
</head>
<body>
<div class="container" role="main">
<div class="row">
<div class="col-md-12">
<table class="table table-hover table-bordered" id="gravidaTable">
<thead>
<tr>
<th ></th>
<th >栏目标识</th>
<th>栏目名称</th>
</tr>
</thead>
<tbody>
<tr>
<td ></td>
<td >cs</td>
<td>测试11111</td>
</tr>
<tr>
<td ></td>
<td >cs</td>
<td>测试11111</td>
</tr>
<tr>
<td ></td>
<td >cs</td>
<td>测试11111</td>
</tr>
<tr>
<td ></td>
<td >cs</td>
<td>测试11111</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-table.js"></script>

<script type="text/javascript">
$('#gravidaTable').bootstrapTable({
columns: [{
title: '行号',
align: 'center',
valign: 'bottom',
formatter: function(value, row, index) {
return index + 1;
}
}
]
});
</script>
</body>
</html>


通过bootstrap-table这个插件可以通过页面和js做到动态显示表格行号的效果,上面js代码里columns是页面加载的时候重新设置了表格的内容属性。默认从第一列开始设置。
官方文档:http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation
通过这个文档可以查阅其他属性的设置
### Bootstrap Table 行拖拽并更新行号的实现方法 为了实现在使用 Bootstrap 的情况下,通过拖拽功能调整表格行顺序的同时动态更新行号的功能,以下是详细的解决方案: #### 1. 引入必要的依赖库 除了基础的 jQuery 和 Bootstrap 文件外,还需要引入 `jquery.tablednd.js` 插件以及 `bootstrap-table-reorder-rows.js` 扩展文件。这些文件提供了支持拖拽的核心逻辑。 ```html <script src="path/to/jquery.min.js"></script> <link rel="stylesheet" href="path/to/bootstrap.min.css"> <script src="path/to/bootstrap.min.js"></script> <script src="path/to/jquery.tablednd.js"></script> <script src="path/to/bootstrap-table-reorder-rows.js"></script> ``` 此部分确保了前端环境能够正常运行拖拽操作[^1]。 --- #### 2. 配置 HTML 结构 在 `<table>` 元素中添加特定属性以启用拖拽功能。具体来说,设置 `data-use-row-attr-func="true"` 和 `data-reorderable-rows="true"` 属性即可激活行拖拽行为。 ```html <table id="exampleTable" data-toggle="table" data-use-row-attr-func="true" data-reorderable-rows="true"> <thead> <tr> <th>序号</th> <th>名称</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Alice</td> <td>User A</td> </tr> <tr> <td>2</td> <td>Bob</td> <td>User B</td> </tr> <!-- 更多行 --> </tbody> </table> ``` 上述配置使得表格具备可拖拽的基础能力[^1]。 --- #### 3. 处理拖拽事件并更新行号 利用 `onReorderRow` 方法捕获拖拽结束后的回调函数,在其中重新计算每行的序号,并将其应用到对应的单元格中。 ```javascript $('#exampleTable').on('reorder-row.bs.table', function (event, newData) { const $table = $('#exampleTable'); let rowIndex = 1; // 遍历每一行并更新序号列的内容 $table.find('tbody tr').each(function () { $(this).find('td:first-child').text(rowIndex++); }); }); ``` 在此代码片段中,每当发生拖拽动作完成后,都会触发 `reorder-row.bs.table` 事件。通过遍历所有的表体行 (`<tbody><tr></tr>`) 并逐一修改第一个单元格(即序号列),从而达到实时同步的效果。 --- #### 4. 可选:保存新顺序至服务器端 如果希望持久化用户的排序更改,则可以在 `onReorderRowsDrop` 或其他适当位置向后端发送请求,传递最新的数据结构以便存储于数据库中。 ```javascript const saveOrderToServer = async (newData) => { try { await $.ajax({ url: '/api/save-order', method: 'POST', contentType: 'application/json', data: JSON.stringify(newData), }); console.log('New order saved successfully.'); } catch (error) { console.error('Failed to save new order:', error); } }; // 调用示例 $('#exampleTable').on('reorder-row.bs.table', function (event, newData) { updateRowNumbers(); saveOrderToServer(newData); // 将新的排列顺序提交给后台处理 }); function updateRowNumbers() { const $table = $('#exampleTable'); let index = 1; $table.find('tbody tr').each((_, row) => { $(row).children('td:first-child').text(index++); }); } ``` 这里展示了如何结合 AJAX 请求将最新状态通知服务端。 --- ### 总结 综上所述,借助 `jquery.tablednd.js` 和 `bootstrap-table-reorder-rows.js` 提供的支持,配合自定义 JavaScript 函数可以轻松达成既定目标——不仅允许用户自由调整记录的位置关系,还能即时反映变动情况下的编号变化状况。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值