bootstraptable實現(xiàn)單擊單元格可編輯功能-創(chuàng)新互聯(lián)

要使bootstrap-table實現(xiàn)可編輯,需要配合使用x-editable插件。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于成都網站設計、成都做網站、古縣網絡推廣、小程序定制開發(fā)、古縣網絡營銷、古縣企業(yè)策劃、古縣品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供古縣建站搭建服務,24小時服務熱線:18982081108,官方網址:muchs.cn

先在頁面上導入必要的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: '數(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:'當前大只能輸入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) { // 當編輯狀態(tài)被隱藏時觸發(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'); // 打開被點擊單元格的編輯狀態(tài)
  } );

 });
 </script>

</body>
</html>

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享名稱:bootstraptable實現(xiàn)單擊單元格可編輯功能-創(chuàng)新互聯(lián)
標題路徑:http://muchs.cn/article38/dejspp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、軟件開發(fā)用戶體驗、品牌網站制作、Google、網站營銷

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網頁設計公司