bootstrap table實(shí)現(xiàn)x-editable的行單元格編輯及解決數(shù)據(jù)Empty和支持多樣式問題
前言
- 最近在研究bootstrap table的表格的單元格編輯功能,實(shí)現(xiàn)點(diǎn)擊單元格修改內(nèi)容,其中包括文本(text)方式修改,下拉選擇(select)方式修改,日期(date)格式修改等。
- 本文著重解決x-editable編輯的數(shù)據(jù)動(dòng)態(tài)添加和顯示數(shù)據(jù)為Empty的問題,還有給表格單元格的內(nèi)容設(shè)置多樣式,使得顯示多樣化。
- 由于官網(wǎng)給的demo的數(shù)據(jù)都是html文件里寫好的,select類型的不能動(dòng)態(tài)添加(所以網(wǎng)上的大多都是官網(wǎng)的類似例子,本篇博客就是在這種情況下以自己的經(jīng)驗(yàn)分享給大家,有問題可以留言哦),一旦動(dòng)態(tài)添加就會(huì)出現(xiàn)顯示數(shù)據(jù)為Empty,我表格原本是有數(shù)據(jù)的,但是一用這個(gè)插件就把數(shù)據(jù)變成Empty了,這可不是我想要的,所以筆者就自行解決了這個(gè)問題。
對(duì)比網(wǎng)上的例子
比如以下這種數(shù)據(jù)不是Empty的例子,但是是由于在html中寫死了數(shù)據(jù)(awesome),不適合動(dòng)態(tài)添加。
<a href="#" rel="external nofollow" id="username" data-type="text" data-pk="1">awesome</a>
<script>
$(function(){
$('#username').editable({
url: '/post',
title: 'Enter username'
});
});
</script>
另外一種就是使用bootstrap table動(dòng)態(tài)添加的,但是select類型就會(huì)出現(xiàn)數(shù)據(jù)為Empty的情況。
$('#db_dependences').bootstrapTable({
method:'POST',
dataType:'json',
contentType: "application/x-www-form-urlencoded",
cache: false,
striped: true, //是否顯示行間隔色
sidePagination: "client", //分頁(yè)方式:client客戶端分頁(yè),server服務(wù)端分頁(yè)(*)
showColumns:true,
pagination:true,
minimumCountColumns:2,
pageNumber:1, //初始化加載第一頁(yè),默認(rèn)第一頁(yè)
pageSize: 10, //每頁(yè)的記錄行數(shù)(*)
pageList: [10, 15, 20, 25], //可供選擇的每頁(yè)的行數(shù)(*)
uniqueId: "id", //每一行的唯一標(biāo)識(shí),一般為主鍵列
showExport: true,
exportDataType: 'all',
exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'], //導(dǎo)出文件類型
onEditableSave: function (field, row, oldValue, $el) {
$.ajax({
success: function (data, status) {
if (status == "success") {
alert("編輯成功");
}
},
error: function () {
alert("Error");
},
complete: function () {
}
});
},
data: [{
id: 1,
name: '張三',
sex: '男',
time: '2017-08-09'
}, {
id: 2,
name: '王五',
sex: '女',
time: '2017-08-09'
}, {
id: 3,
name: '李四',
sex: '男',
time: '2017-08-09'
}, {
id: 4,
name: '楊朝來',
sex: '男',
time: '2017-08-09'
}, {
id: 5,
name: '蔣平',
sex: '男',
time: '2017-08-09'
}, {
id: 6,
name: '唐燦華',
sex: '男',
time: '2017-08-09'
}],
columns: [{
field: 'id',
title: '序號(hào)'
}, {
field: 'name',
title: '姓名',
editable: {
type: 'text',
validate: function (value) {
if ($.trim(value) == '') {
return '姓名不能為空!';
}
}
}
}, {
field: 'sex',
title: '性別',
editable: {
type: 'select',
pk: 1,
source: [
{value: 1, text: '男'},
{value: 2, text: '女'},
]
}
}, {
field: 'time',
title: '時(shí)間',
editable: {
type: 'date',
format: 'yyyy-mm-dd',
viewformat: 'yyyy-mm-dd',
datepicker: {
weekStart: 1
}
}
}]
});
結(jié)果圖如下:

由于開源,很快就找到原因,由于formatter我們沒有寫這個(gè)function導(dǎo)致調(diào)用的默認(rèn)的formatter,默認(rèn)的沒有把表格的值傳入html中,bootstrap-table-editable.js源碼如下,初始定義_dont_edit_formatter為false,我們沒有實(shí)現(xiàn)noeditFormatter的function就會(huì)執(zhí)行第二個(gè)if語(yǔ)句,其中的標(biāo)簽中沒有對(duì)內(nèi)容賦值,導(dǎo)致最后顯示結(jié)果為它默認(rèn)的Empty:
column.formatter = function(value, row, index) {
var result = column._formatter ? column._formatter(value, row, index) : value;
$.each(column, processDataOptions);
$.each(editableOptions, function(key, value) {
editableDataMarkup.push(' ' + key + '="' + value + '"');
});
var _dont_edit_formatter = false;
if (column.editable.hasOwnProperty('noeditFormatter')) {
_dont_edit_formatter = column.editable.noeditFormatter(value, row, index);
}
if (_dont_edit_formatter === false) {
return ['<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" ',
' data-name="' + column.field + '"',
' data-pk="' + row[that.options.idField] + '"',
' data-value="' + result + '"',
editableDataMarkup.join(''),
'>' + '</a>'
].join('');
} else {
return _dont_edit_formatter;
}
};
由于要實(shí)現(xiàn)多樣式,則把上面的代碼改變,并在使用的時(shí)候?qū)崿F(xiàn)noeditFormatter:function(value){…}就是了。將上面的代碼改為如下(此為我自己改的,你可以根據(jù)自己的需要做修改):
column.formatter = function(value, row, index) {
var result = column._formatter ? column._formatter(value, row, index) : value;
$.each(column, processDataOptions);
$.each(editableOptions, function(key, value) {
editableDataMarkup.push(' ' + key + '="' + value + '"');
});
var _dont_edit_formatter = false;
if (column.editable.hasOwnProperty('noeditFormatter')) {
var process = column.editable.noeditFormatter(value, row, index);
if(!process.hasOwnProperty('class')){
process.class = '';
}
if(!process.hasOwnProperty('style')){
process.style = '';
}
_dont_edit_formatter = ['<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" ',
' data-name="'+process.filed+'"',
' data-pk="1"',
' data-value="' + process.value + '"',
' class="'+process.class+'" style="'+process.style+'"',
'>' + process.value + '</a>'
].join('');
}
if (_dont_edit_formatter === false) {
return ['<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" ',
' data-name="' + column.field + '"',
' data-pk="' + row[that.options.idField] + '"',
' data-value="' + result + '"',
editableDataMarkup.join(''),
'>' + value + '</a>'
].join('');
} else {
return _dont_edit_formatter;
}
};
結(jié)果如下:


然后是bootstrap table的使用js文件,在其中實(shí)現(xiàn)noeditFormatter函數(shù)。返回的result必須包含filed和value,class和style可以不需要,class可以額外用其它插件之類,比如badge,style是增加樣式(背景,顏色,字體等)。
$('#db_dependences').bootstrapTable({
method:'POST',
dataType:'json',
contentType: "application/x-www-form-urlencoded",
cache: false,
striped: true, //是否顯示行間隔色
sidePagination: "client", //分頁(yè)方式:client客戶端分頁(yè),server服務(wù)端分頁(yè)(*)
showColumns:true,
pagination:true,
minimumCountColumns:2,
pageNumber:1, //初始化加載第一頁(yè),默認(rèn)第一頁(yè)
pageSize: 10, //每頁(yè)的記錄行數(shù)(*)
pageList: [10, 15, 20, 25], //可供選擇的每頁(yè)的行數(shù)(*)
uniqueId: "id", //每一行的唯一標(biāo)識(shí),一般為主鍵列
showExport: true,
exportDataType: 'all',
exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'], //導(dǎo)出文件類型
onEditableSave: function (field, row, oldValue, $el) {
$.ajax({
success: function (data, status) {
if (status == "success") {
alert("編輯成功");
}
},
error: function () {
alert("Error");
},
complete: function () {
}
});
},
// 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)向下一行移動(dòng)
// } else if(reason === 'nochange') {
// $el.closest('tr').next().find('.editable').editable('show');
// }
// },
data: [{
id: 1,
name: '張三',
sex: '男',
time: '2017-08-09'
}, {
id: 2,
name: '王五',
sex: '女',
time: '2017-08-09'
}, {
id: 3,
name: '李四',
sex: '男',
time: '2017-08-09'
}, {
id: 4,
name: '楊朝來',
sex: '男',
time: '2017-08-09'
}, {
id: 5,
name: '蔣平',
sex: '男',
time: '2017-08-09'
}, {
id: 6,
name: '唐燦華',
sex: '男',
time: '2017-08-09'
}, {
id: 7,
name: '馬達(dá)',
sex: '男',
time: '2017-08-09'
}, {
id: 8,
name: '趙小雪',
sex: '女',
time: '2017-08-09'
}, {
id: 9,
name: '薛文泉',
sex: '男',
time: '2017-08-09'
}, {
id: 10,
name: '丁建',
sex: '男',
time: '2017-08-09'
}, {
id: 11,
name: '王麗',
sex: '女',
time: '2017-08-09'
}],
columns: [{
field: 'id',
title: '序號(hào)'
}, {
field: 'name',
title: '姓名',
editable: {
type: 'text',
validate: function (value) {
if ($.trim(value) == '') {
return '姓名不能為空!';
}
}
}
}, {
field: 'sex',
title: '性別',
editable: {
type: 'select',
pk: 1,
source: [
{value: 1, text: '男'},
{value: 2, text: '女'},
],
noeditFormatter: function (value,row,index) {
var result={filed:"sex",value:value,class:"badge",style:"background:#333;padding:5px 10px;"};
return result;
}
}
}, {
field: 'time',
title: '時(shí)間',
editable: {
type: 'date',
format: 'yyyy-mm-dd',
viewformat: 'yyyy-mm-dd',
datepicker: {
weekStart: 1
},
noeditFormatter: function (value,row,index) {
var result={filed:"time",value:value,class:"badge",style:"background:#333;padding:5px 10px;"};
return result;
}
}
}]
});
關(guān)于bootstrap table的導(dǎo)出及使用可以看我另外一篇博客。
下載和引用
下載x-editable,并如下引用。
<link href="js/bootstrap_above/x-editable-develop/dist/bootstrap-editable/css/bootstrap-editable.css" rel="external nofollow" rel="stylesheet"> <script src="js/bootstrap_above/x-editable-develop/dist/bootstrap-editable/js/bootstrap-editable.js"></script> <script src="js/bootstrap_above/bootstrap-table-develop/dist/extensions/editable/bootstrap-table-editable.js"></script>
然后講上訴的一些文件修改添加,就完成了。
另外項(xiàng)目的結(jié)果展示


其中的樣式都是自行在x-editable的基礎(chǔ)上添加的。如配置出問題,以下是源碼鏈接。
總結(jié)
以上所述是小編給大家介紹的bootstrap table實(shí)現(xiàn)x-editable的行單元格編輯及解決數(shù)據(jù)Empty和支持多樣式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
uniapp頁(yè)面?zhèn)鲄⒌娜N方式實(shí)例總結(jié)
在進(jìn)行頁(yè)面的跳轉(zhuǎn)的時(shí)候,往往需要我們將一些參數(shù)攜帶著傳遞過去這里的class樣式,下面這篇文章主要給大家介紹了關(guān)于uniapp頁(yè)面?zhèn)鲄⒌娜N方式,需要的朋友可以參考下2022-11-11
Typescript中interface自動(dòng)化生成API文檔詳解
ypeScript 的核心原則之一是對(duì)值所具有的結(jié)構(gòu)進(jìn)行類型檢查,下面這篇文章主要給大家介紹了關(guān)于Typescript中interface自動(dòng)化生成API文檔的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
前端js實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳 后端PHP文件接收
這篇文章主要為大家詳細(xì)介紹了斷點(diǎn)續(xù)傳的簡(jiǎn)單例子,前端文件提交,后端PHP文件接收,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
百度地圖API之百度地圖退拽標(biāo)記點(diǎn)獲取經(jīng)緯度的實(shí)現(xiàn)代碼
這篇文章主要介紹了百度地圖API之百度地圖退拽標(biāo)記點(diǎn)獲取經(jīng)緯度的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-01-01
Javascript新手入門之字符串拼接與變量的應(yīng)用
這篇文章主要給大家介紹了關(guān)于Javascript新手入門之字符串拼接與變量應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
JavaScript實(shí)現(xiàn)捕獲鼠標(biāo)坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)捕獲鼠標(biāo)坐標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
JS獲得一個(gè)對(duì)象的所有屬性和方法實(shí)例
下面小編就為大家?guī)硪黄狫S獲得一個(gè)對(duì)象的所有屬性和方法實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
使用dynatrace-ajax跟蹤JavaScript的性能
DynaTrace 致力于分析后臺(tái)應(yīng)用性能的表現(xiàn)已經(jīng)好幾年了,最近,他們通過發(fā)布dynaTrace Ajax Edition進(jìn)入了前端性能分析領(lǐng)域. 它是一個(gè)運(yùn)行在IE下的BHO免費(fèi)工具. 雖然我喜歡Firefox和它下面的所有插件,但我知道基于IE的測(cè)試和調(diào)試也是很重要的。2010-04-04
jsonp跨域及實(shí)現(xiàn)百度首頁(yè)聯(lián)想功能的方法
這篇文章主要介紹了jsonp跨域及實(shí)現(xiàn)百度首頁(yè)聯(lián)想功能的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
BootStrap modal實(shí)現(xiàn)拖拽功能
這篇文章主要為大家詳細(xì)介紹了BootStrap modal實(shí)現(xiàn)拖拽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

