bootstrap-table 本身只負責渲染表格,想要「點一下格子就能改」,需要搭配 x-editable 插件,並引入它提供的 editable 擴展。二者結合後,單元格會在點擊時變成輸入框,失焦即保存,同時還能掛上自定義校驗。
第一步:引入依賴
順序有講究——jQuery 在最前,接著是 Bootstrap,最後才是 table 與 editable 相關文件。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<title>bootstrap-table demo</title>
<!-- Bootstrap 3.3.6 -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<!-- Bootstrap table -->
<link rel="stylesheet" href="bootstrap-table-1.11.0/bootstrap-table.css">
<!-- x-editable -->
<link rel="stylesheet" href="x-editable/bootstrap3-editable/css/bootstrap-editable.css">
</head>
<body>
<div class="container">
<p></p>
<table id="table" class="table table-bordered table-hover">
</table>
</div>
<!-- jQuery 2.2.0 -->
<script src="jQuery-2.2.0.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- bootstrap table -->
<script src="bootstrap-table-1.11.0/bootstrap-table.js"></script>
<script src="bootstrap-table-1.11.0/extensions/editable/bootstrap-table-editable.js"></script>
<script src="x-editable/bootstrap3-editable/js/bootstrap-editable.js"></script>
<script src="bootstrap-table-1.11.0/locale/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript">
$(function(){
$('#table').bootstrapTable({
url:'data.json',
columns:[
{field: 'id',title: 'ID'},
{field: 'name',title: '名稱'},
{field: 'price',title: '單價'},
{field: 'number',title: '數量',
sortable:true,
cellStyle:function(value,row,index) {
return { "css":{ padding:'0px' } };
},
formatter:function(value,row,index){
if(value == undefined) return "0"; else return value;
},
editable:{
type:'text',
clear:false,
validate:function(value){
if(isNaN(value)) return {newValue:0, msg:'只允許輸入數字'};
else if(value<0) return {newValue:0, msg:'數量不能小於0'};
else if(value>=1000000) return {newValue:0, msg:'當前最大只能輸入999999'};
},
display:function(value){
$(this).text(Number(value));
},
//onblur:'ignore',
showbuttons:false,
defaultValue:0,
mode:'inline'
}
},
{field:'amount', title: '總價'}
],
//height:300,
idField:'id',
onEditableHidden: function(field, row, $el, reason) {
// 當編輯狀態被隱藏時觸發
if(reason === 'save') {
var $td = $el.closest('tr').children();
$td.eq(-1).html((row.price*row.number).toFixed(2));
$el.closest('tr').next().find('.editable').editable('show');
//編輯狀態向下一行移動
} else if(reason === 'nochange') {
$el.closest('tr').next().find('.editable').editable('show');
}
}
});
$('#table').on( 'click', 'td:has(.editable)', function (e) {
//e.preventDefault();
e.stopPropagation(); // 阻止事件的冒泡行為
$(this).find('.editable').editable('show'); // 打開被點擊單元格的編輯狀態
});
});
</script>
</body>
</html>
幾處值得留意的設計
校驗閉環。 validate 裡返回 {newValue:0, msg:'...'} 這種結構,可以在提示錯誤的同時把值回退到合法範圍,避免髒數據進庫。
連鎖計算。 onEditableHidden 在編輯框收起時觸發,這裡用它重算「總價 = 單價 × 數量」,並且不論是否真正改動,都把編輯狀態順移到了下一行——連續錄入時體驗會好很多。
事件綁定方式。 最後那段點擊監聽用了 on 加事件委託,而不是直接給每個 td 綁定。因為表格行是動態生成的,直接綁定會失效,委託到 table 上才能一直有效,同時也順帶調用 stopPropagation 避免與行點擊衝突。
把這幾點串起來,一個可校驗、可連鎖計算、支持連續錄入的表格就完成了。讀者可以在此基礎上把 type 換成 select、date 等,擴展成下拉選擇或日期選擇的場景。