bootstrap table实现单击单元格可编辑功能

要使bootstrap-table实现可编辑,需要配合使用x-editable插件。

先在页面上导入必要的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: '单价'},
    {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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼编程技术建站教程。

您可能感兴趣的文章:

  • bootstrap table合并行数据并居中对齐效果
  • Bootstrap实现的表格合并单元格示例
  • bootstrap table实现x-editable的行单元格编辑及解决数据Empty和支持多样式问题
  • bootstrap table单元格新增行并编辑
  • bootstrap table实现合并单元格效果

相关推荐:

layui 实现table翻页滚动条位置保持不变的例子

最近使用了layuitable但是发现了一系列问题,由于被封装过不好自己修改,比如翻页后滑动条每次都会复位,导致体验不好,通过调试,我发现layuitable并没有给div加id标签,我通过class定位表格div位置。 可以看到 滚动条所在div class="layui-ta...

Arthas Bootstrap的源代码示例分析

今天就跟大家聊聊有关ArthasBootstrap的源代码示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Arthas(阿尔萨斯,https://github.com/alibaba/arthas)是Alibaba开源的一款Java诊断...

bootstrap日期控件问题(双日期、清空等问题解决)

bootstrap以它优美的外观和丰富的组件,使它成为目前最流行的前端框架。在项目开发中,我们使用它的日期控件确实遇到了一些问题:     1.日期控件后面两个图标点击触发失效     2.双日期关联问题     3.双日期清空时,之前输入日期关联仍然有效     4.输入年月     5.图标不显...

怎么使用bootstrap树状菜单

怎么使用bootstrap树状菜单?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。  bootstrap-treeview.js是一款强大的树菜单插件,本文演示bootstrap-treeview.js的调用方法。它可...

Bootstrap CSS语法编码规范有哪些

这篇文章主要讲解了“BootstrapCSS语法编码规范有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“BootstrapCSS语法编码规范有哪些”吧!1.用两个空格来代替制表符(tab)--这是唯一能保证在所有环境下获得一致展现的方法。2.为选...

如何在vue中引入bootstrap

这篇文章将为大家详细讲解有关如何在vue中引入bootstrap,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。为什么要使用VueVue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以创建可维护性和可测试性更强的代码库,Vue允许...

vue中引入jQuery和Bootstrap的方法

小编给大家分享一下vue中引入jQuery和Bootstrap的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、引入jQuery在当前项目的目录下(就是package.json),运行命令cnpminstalljqu...