Bootstrap 行內編輯並保存


這里內容主要依據與https://blog.csdn.net/wuminglq/article/details/83009664,如果看完可以,就沒必要往下看了。

先將行內編輯的代碼復制

 1 /**
 2  * bootstrap API中的 onDblClickCell 方法,當用戶雙擊某一列的時候觸發
 3  * @param field:點擊列的 field 名稱
 4  * @param value:點擊列的 value 值
 5  * @param row:點擊列的整行數據
 6  * @param $element:td 元素
 7  */
 8 onDblClickCell : function (field, value, row, $element) {
 9     // $element[0]: <td style="">0</td> 當前表格的內容
10     // $element[0].parentElement: <tr data-index="0">...</tr> 表格父級tr
11     // $element[0].parentElement.rowIndex: 1 返回某一行在表格的行集合中的位置,這是官方解釋,通俗就是第幾行
12     // upIndex: 0 行索引,用於更新時確認行索引,-1是因為這里返回第一行為1,更新時索引第一行為0
13     var upIndex = $element[0].parentElement.rowIndex - 1;
14     // options.editFiled:需要編輯的屬性,及列名,這里可動態配置多列
15     if (field == options.editFiled) {
16         // 將表格<td></td>中間內容替換為一個如下input
17         // innerHTML 設置或返回表格行的開始和結束標簽之間的HTML,官方解釋,簡單說就是獲取或者替換表格內容
18         $element[0].innerHTML =
19                 "<input id='inputCell' type='text' name='inputCell' style ='width: 40px' value='" + value    + "'>";
20         // 獲取焦點,鼠標點擊
21         $("#inputCell").focus();
22         // 失去焦點,鼠標點擊其他位置,離開輸入區域,默認為輸入結束,進行保存
23         $("#inputCell").blur(function () {
24             // 獲取輸入內容
25             var newValue = $("#inputCell").val();
26             // 這里可以對內容進行校驗,是否為空、數字、長度等
27             // 將修改后的結果設置到行中
28             row[field] = newValue;
29             // 移除鼠標點擊事件
30             $(this).remove();
31             /**
32              * bootstrap API中的 updateCell 方法 更新一個單元格
33              * @params index: 行索引 這里就是上邊需要-1
34              * @param field: 字段名稱
35              * @param value: 新字段值
36              */
37             $('#bootstrap-table').bootstrapTable('updateCell', {
38                 index: upIndex,
39                 field: field,
40                 value: newValue
41             });
42             // 封裝方法,對編輯后的結果進行處理
43             $.operate.rowedit(row);
44         });
45     }
46 }

 

以上是這段代碼的全部注釋,其中未包含的,動態設置編輯列代碼如下:

1 $(function () {
2     var options = {
3         editFiled: '編輯名稱',
4         editFiled_1: '多個'
5     }
6 })

 封裝的方法如下:

 1 // 操作封裝處理
 2 operate: {
 3     // post請求傳輸
 4     post: function (url, data) {
 5         $.operate.submit(url, "post", "json", data);
 6     }
 7 ,
 8     // 修改行
 9     rowedit: function (row) {
10         $.modal.loading("正在處理中,請稍后...");
11         var url = $.table._option.roweditUrl;
12         var config = {
13             url: url,
14             type: "post",
15             dataType: "json",
16             data: row,
17             success: function (result) {
18                 $.operate.ajaxSuccess(result);
19             }
20         };
21         $.ajax(config)
22     }
23 ,
24     // 保存結果彈出msg刷新table表格
25     ajaxSuccess: function (result) {
26         if (result.code == web_status.SUCCESS) {
27             $.modal.msgSuccess(result.msg);
28             $.table.refresh();
29         } else {
30             $.modal.alertError(result.msg);
31         }
32         $.modal.closeLoading();
33     }
34 }

 

 

這是復制的,個人實際使用的如下:

 1 function rowedit(row) {
 2     jp.post(
 3             "",
 4             {
 5                 id: row.id,
 6                 field: row.field
 7             },
 8             function (data) {
 9                 if (data.success) {
10                     refresh();
11                     jp.toastr_success(data.msg);
12                 } else {
13                     jp.toastr_error(data.msg);
14                 }
15             })
16 }

 

這個就按各自需求做處理了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM