bootstrap table實(shí)現(xiàn)單擊單元格可編輯功能
要使bootstrap-table實(shí)現(xiàn)可編輯,需要配合使用x-editable插件。
先在頁面上導(dǎo)入必要的css和js文件
<!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" rel="external nofollow" >
<!-- Bootstrap table -->
<link rel="stylesheet" href="bootstrap-table-1.11.0/bootstrap-table.css" rel="external nofollow" >
<!-- x-editable -->
<link rel="stylesheet" href="x-editable/bootstrap3-editable/css/bootstrap-editable.css" rel="external nofollow" >
</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: '單價(jià)'},
{field: 'number',title: '數(shù)量', 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:'只允許輸入數(shù)字'};
else if(value<0) return {newValue:0, msg:'數(shù)量不能小于0'};
else if(value>=1000000) return {newValue:0, msg:'當(dāng)前最大只能輸入999999'};
},
display:function(value){
$(this).text(Number(value));
},
//onblur:'ignore',
showbuttons:false,
defaultValue:0,
mode:'inline'
}
},
{field:'amount', title: '總價(jià)'}
],
//height:300,
idField:'id',
onEditableHidden: function(field, row, $el, reason) { // 當(dāng)編輯狀態(tài)被隱藏時(shí)觸發(fā)
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'); //編輯狀態(tài)向下一行移動
} 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'); // 打開被點(diǎn)擊單元格的編輯狀態(tài)
} );
});
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Bootstrap Table 在指定列中添加下拉框控件并獲取所選值
- Bootstrap Table列寬拖動的方法
- bootstrap table列和表頭對不齊的解決方法
- Bootstrap Table使用方法詳解
- JS組件Bootstrap Table使用方法詳解
- bootstrap table單元格新增行并編輯
- 值得分享的bootstrap table實(shí)例
- 值得分享的輕量級Bootstrap Table表格插件
- 基于BootStrap的Metronic框架實(shí)現(xiàn)頁面鏈接收藏夾功能按鈕移動收藏記錄(使用Sortable進(jìn)行拖動排序)
- Bootstrap?table列上下移動效果
相關(guān)文章
JavaScript兼容性總結(jié)之獲取非行間樣式案例
這篇文章主要介紹了JavaScript兼容性總結(jié)之獲取非行間樣式的相關(guān)資料,需要的朋友可以參考下2016-08-08
node.js Web應(yīng)用框架Express入門指南
這篇文章主要介紹了node.js Web應(yīng)用框架Express入門指南,從安裝到各種技術(shù)的應(yīng)用,都進(jìn)行了講解,是一篇不錯的Express入門教程,需要的朋友可以參考下2014-05-05
javascript正則表達(dá)式定義(語法)總結(jié)
這篇文章主要介紹了javascript正則表達(dá)式定義,對于JavaScript正則表達(dá)式的語法進(jìn)行了總結(jié)分析,需要的朋友可以參考下2016-01-01
javascript 組合按鍵事件監(jiān)聽實(shí)現(xiàn)代碼
這篇文章主要介紹了javascript 組合按鍵事件監(jiān)聽實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
js實(shí)現(xiàn)的頁面加載完畢之前l(fā)oading提示效果完整示例【附demo源碼下載】
這篇文章主要介紹了js實(shí)現(xiàn)的頁面加載完畢之前l(fā)oading提示效果,結(jié)合完整實(shí)例形式分析了js頁面加載時(shí)顯示loading效果的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
JavaScript的new date等日期函數(shù)在safari中遇到的坑
safari中對于JavaScript的new Date函數(shù)的支持有一個比較奇怪的問題,帶著這個奇怪的問題我們通過本文一起學(xué)習(xí)吧2016-10-10

