bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题


Posted in Javascript onAugust 10, 2017

前言

  • 最近在研究bootstrap table的表格的单元格编辑功能,实现点击单元格修改内容,其中包括文本(text)方式修改,下拉选择(select)方式修改,日期(date)格式修改等。
  • 本文着重解决x-editable编辑的数据动态添加和显示数据为Empty的问题,还有给表格单元格的内容设置多样式,使得显示多样化。
  • 由于官网给的demo的数据都是html文件里写好的,select类型的不能动态添加(所以网上的大多都是官网的类似例子,本篇博客就是在这种情况下以自己的经验分享给大家,有问题可以留言哦),一旦动态添加就会出现显示数据为Empty,我表格原本是有数据的,但是一用这个插件就把数据变成Empty了,这可不是我想要的,所以笔者就自行解决了这个问题。

对比网上的例子

比如以下这种数据不是Empty的例子,但是是由于在html中写死了数据(awesome),不适合动态添加。

<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动态添加的,但是select类型就会出现数据为Empty的情况。

$('#db_dependences').bootstrapTable({
  method:'POST',
  dataType:'json',
  contentType: "application/x-www-form-urlencoded",
  cache: false,
  striped: true,      //是否显示行间隔色
  sidePagination: "client",   //分页方式:client客户端分页,server服务端分页(*)
  showColumns:true,
  pagination:true,
  minimumCountColumns:2,
  pageNumber:1,      //初始化加载第一页,默认第一页
  pageSize: 10,      //每页的记录行数(*)
  pageList: [10, 15, 20, 25],  //可供选择的每页的行数(*)
  uniqueId: "id",      //每一行的唯一标识,一般为主键列
  showExport: true,     
  exportDataType: 'all',
  exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'], //导出文件类型
  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: '序号'
  }, {
   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: '时间',
   editable: {
    type: 'date',
    format: 'yyyy-mm-dd', 
    viewformat: 'yyyy-mm-dd', 
    datepicker: {
     weekStart: 1
    }
   } 
  }]
 });

结果图如下:

bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题

由于开源,很快就找到原因,由于formatter我们没有写这个function导致调用的默认的formatter,默认的没有把表格的值传入html中,bootstrap-table-editable.js源码如下,初始定义_dont_edit_formatter为false,我们没有实现noeditFormatter的function就会执行第二个if语句,其中的标签中没有对内容赋值,导致最后显示结果为它默认的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;
    }
   };

由于要实现多样式,则把上面的代码改变,并在使用的时候实现noeditFormatter:function(value){…}就是了。将上面的代码改为如下(此为我自己改的,你可以根据自己的需要做修改):

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;
    }
   };

结果如下:

bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题

bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题

然后是bootstrap table的使用js文件,在其中实现noeditFormatter函数。返回的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",   //分页方式:client客户端分页,server服务端分页(*)
  showColumns:true,
  pagination:true,
  minimumCountColumns:2,
  pageNumber:1,      //初始化加载第一页,默认第一页
  pageSize: 10,      //每页的记录行数(*)
  pageList: [10, 15, 20, 25],  //可供选择的每页的行数(*)
  uniqueId: "id",      //每一行的唯一标识,一般为主键列
  showExport: true,     
  exportDataType: 'all',
  exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'], //导出文件类型
  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) { // 当编辑状态被隐藏时触发
//   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');
//   }
//  },
  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: '马达',
   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: '序号'
  }, {
   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: '时间',
   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;
    }
   } 
  }]
 });

关于bootstrap table的导出及使用可以看我另外一篇博客。

下载和引用

下载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>

然后讲上诉的一些文件修改添加,就完成了。

另外项目的结果展示

bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题

bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题

其中的样式都是自行在x-editable的基础上添加的。如配置出问题,以下是源码链接。

总结

以上所述是小编给大家介绍的bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
javascript 点击整页变灰的效果(可做退出效果)。
Jan 09 Javascript
jquery 表单取值常用代码
Dec 22 Javascript
JS、CSS加载中的小问题探讨
Nov 26 Javascript
jquery实现鼠标滑过小图时显示大图的方法
Jan 14 Javascript
js Canvas实现的日历时钟案例分享
Dec 25 Javascript
Angular2 组件间通过@Input @Output通讯示例
Aug 24 Javascript
浅谈React中组件间抽象
Jan 27 Javascript
vue+web端仿微信网页版聊天室功能
Apr 30 Javascript
javascript中undefined的本质解析
Jul 31 Javascript
对layui中的onevent 和event的使用详解
Sep 06 Javascript
vue路由权限校验功能的实现代码
Jun 07 Javascript
详细分析React 表单与事件
Jul 08 Javascript
纯js实现页面返回顶部的动画(超简单)
Aug 10 #Javascript
基于daterangepicker日历插件使用参数注意的问题
Aug 10 #Javascript
通过示例彻底搞懂js闭包
Aug 10 #Javascript
用js屏蔽被http劫持的浮动广告实现方法
Aug 10 #Javascript
JS实现下拉菜单列表与登录注册弹窗效果
Aug 10 #Javascript
浅谈sass在vue注意的地方
Aug 10 #Javascript
详解jQuery同步Ajax带来的UI线程阻塞问题及解决办法
Aug 09 #jQuery
You might like
分享一个超好用的php header下载函数
2014/01/31 PHP
PHP错误WARNING: SESSION_START() [FUNCTION.SESSION-START]解决方法
2014/05/04 PHP
codeigniter框架The URI you submitted has disallowed characters错误解决方法
2014/05/06 PHP
PHP实现简单汉字验证码
2015/07/28 PHP
全面解读PHP的人气开发框架Laravel
2015/10/15 PHP
Yii配置与使用memcached缓存的方法
2016/07/13 PHP
Laravel+jQuery实现AJAX分页效果
2016/09/14 PHP
thinkPHP5.0框架整体架构总览【应用,模块,MVC,驱动,行为,命名空间等】
2017/03/25 PHP
php读取本地json文件的实例
2018/03/07 PHP
Extjs4中Form的使用之本地hiddenfield
2013/11/26 Javascript
Angular.js实现注册系统的实例详解
2016/12/18 Javascript
那些精彩的JavaScript代码片段
2017/01/12 Javascript
浅析为什么a=&quot;abc&quot; 不等于 a=new String(&quot;abc&quot;)
2017/10/25 Javascript
解决Vue打包之后文件路径出错的问题
2018/03/06 Javascript
原生js实现商品筛选功能
2019/10/28 Javascript
微信小程序自定义联系人弹窗
2020/05/26 Javascript
在vue中封装的弹窗组件使用队列模式实现方法
2020/07/23 Javascript
[03:49]2016完美“圣”典风云人物:AMS专访
2016/12/06 DOTA
python中的格式化输出用法总结
2016/07/28 Python
Python对列表中的各项进行关联详解
2017/08/15 Python
Python基础之高级变量类型实例详解
2020/01/03 Python
python 实现将Numpy数组保存为图像
2020/01/09 Python
Python logging日志模块 配置文件方式
2020/07/12 Python
Python如何进行时间处理
2020/08/06 Python
使用Python制作一个数据预处理小工具(多种操作一键完成)
2021/02/07 Python
HTML5 解决苹果手机不能自动播放音乐问题
2017/12/27 HTML / CSS
意大利自行车商店:Cingolani Bike Shop
2019/09/03 全球购物
师范生个人推荐信
2013/11/29 职场文书
中学生操行评语
2014/04/24 职场文书
学生党员公开承诺书
2014/05/28 职场文书
留学生求职信
2014/06/03 职场文书
2015法院个人工作总结范文
2015/05/25 职场文书
同学聚会致辞集锦
2015/07/28 职场文书
国际贸易实训总结
2015/08/03 职场文书
vue首次渲染全过程
2021/04/21 Vue.js
JPA如何使用entityManager执行SQL并指定返回类型
2021/06/15 Java/Android