Browse Source

editable extension - Click edit row (#2586)

* click to edit a row

click to edit a row

* add example page

add example page

* update error

* update

* remove a unneeded semicolon

* update plugin logic to set column to  input or select

update plugin logic to set input or select
Horken 8 years ago
parent
commit
284b6e9b40

+ 46 - 0
src/extensions/click-edit-row/Editable(row).html

@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Editable(row)</title>
+    <meta charset="utf-8">
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
+    <link rel="stylesheet" href="../../bootstrap-table.css">
+    <link rel="stylesheet" href="editable.css">
+    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
+    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
+    <script src="../../bootstrap-table.js"></script>
+    <script src="./bootstrap-table-click-edit.js"></script>
+</head>
+<body>
+    <div class="container">
+        <h1>Editable(row)</h1>
+        <table id="table"
+               data-toggle="table"
+               data-click-edit="true"
+               data-unique-id="treeId"
+               data-url="data1.json">
+            <thead>
+            <tr>
+                <th data-field="id">ID</th>
+                <th data-field="name" data-editable="input">Item Name</th>
+                <th data-field="price" data-editable="select">Item Price</th>
+            </tr>
+            </thead>
+        </table>
+    </div>
+    <script>
+        $(function () {
+          //Setup options for your 'Select'options, obj name must same as your data-field name.
+          $.selectArray=
+            {price: [{idxNum: '$', name: '100'}, {idxNum: '$', name: '500'}, {idxNum: 'NT$', name: '1000'}, {idxNum: 'NT$', name: '2500'}, {idxNum: 'NT$', name: '5000'}],
+             name: [{idxNum: 1, name: 'Jenny'}, {idxNum: 2, name: 'Aby'}, {idxNum: 3, name: 'Martin'}, {idxNum: 4, name: 'Hellen'}, {idxNum: 5, name: 'Steve'}]};
+          
+          $('#table').bootstrapTable(); // init via javascript
+
+          $(window).resize(function () {
+              $('#table').bootstrapTable('resetView');
+          });
+        });
+    </script>
+</body>
+</html>

+ 35 - 0
src/extensions/click-edit-row/README.md

@@ -0,0 +1,35 @@
+# Table click-edit-row
+
+Use Plugin: [bootstrap-click-edit-row](https://github.com/wenzhixin/bootstrap-table/tree/develop/src/extensions/click-edit-row) </br>
+You must include the editable.css file in order to get the appropriate style.
+Ps. Used this plugin is better on table columns not more than five.
+
+## Usage
+
+```html
+<script src="extensions/click-edit-row/bootstrap-table-click-edit.js"></script>
+```
+
+## Options
+
+### editable
+
+* type: input、select
+* default: `input`
+* description: Set select must setup `$.selectArray` for select options.
+* $.selectArray example: `$.selectArray= {price: [{idxNum: '$', name: '100'}, {idxNum: '$', name: '500'}, {idxNum: '$', name: '5000'}]}`, obj name must same as 'data-field' value.
+
+All options can be defined via `data-editable-*` HTML attributes. Table wide options are used for every row but can be overridden:
+
+````html
+<table id="my_table_id"
+  data-url="data1.json">
+  <thead>
+    <tr>
+      <th data-field="id">ID</th>
+      <th data-field="name" data-editable="input">Item Name</th>
+      <th data-field="price" data-editable="select">Item Price</th>
+    </tr>
+  </thead>
+</table>
+````

+ 142 - 0
src/extensions/click-edit-row/bootstrap-table-click-edit.js

@@ -0,0 +1,142 @@
+/**
+ * @author horken wong <horken.wong@gmail.com>
+ * @version: v1.0.0
+ * https://github.com/horkenw/bootstrap-table
+ * Click to edit row for bootstrap-table
+ */
+
+(function ($) {
+    'use strict';
+
+    $.extend($.fn.bootstrapTable.defaults, {
+        clickEdit: false
+    });
+
+    function setDivision(node, options){
+        var $option = $('<option />');
+        if(options){
+            $(options).each(function(i, v){
+                $option.clone().text(v.idxNum + ' ' +v.name).val(v.idxNum).appendTo(node);
+            })
+        }
+        else{
+            console.log('Please setup options first!!')
+        }
+    }
+
+    function clikcToEdit(evt, tarNode){
+        var txt = [], table = evt,
+            submit = '<button type="button" class="btn btn-primary btn-sm editable-submit"><i class="glyphicon glyphicon-ok"></i></button>',
+            cancel = '<button type="button" class="btn btn-default btn-sm editable-cancel"><i class="glyphicon glyphicon-remove"></i></button>';
+
+        var replaceData = function(){
+            txt = [];
+            tarNode.find('td').find('input[type="text"]').each(function(i, td){
+                txt.push($(td).eq(0).val());
+            });
+            tarNode.find('select').each(function(i, td){
+                txt.push($('#'+td.id+' option:selected').val());
+            });
+            $('#table').bootstrapTable('updateRow', {
+                index: table.$data.thId,
+                row: {
+                    noOld: txt[0],
+                    area: tarNode.find('select').eq(0).children(':selected').text(),
+                    town: tarNode.find('select').eq(1).children(':selected').text(),
+                    address: txt[1]
+                }
+            });
+            $('#tooling').remove();
+            table.editing = true;
+            // updateToServerSide(table.$data.itemid, txt);
+            return false;
+        };
+
+        var recoveryData = function(){
+          $('#table').bootstrapTable('updateRow', {
+                index: table.$data.thId,
+                row: {},
+            });
+          $('#tooling').remove();
+          table.editing = true;
+          return false;
+        };
+
+        if(table.editing){
+            var  rootid = 0;
+            table.editing = false;
+            table.columns.forEach(function(column, i){
+                if (!column.editable) return;
+
+                switch(column.editable){
+                    case 'input':
+                        var div=$('<div class="editable-input col-md-12 col-sm-12 col-xs-12" style="position: relative;"/>');
+                        txt.push(tarNode.find('td').eq(column.fieldIndex).text());
+                        div.append($('<input type="text" class="form-control input-sm"/>'));
+                        div.append($('<span class="clear"><i class="fa fa-times-circle-o" aria-hidden="true"></i></span>'));
+                        tarNode.find('td').eq(column.fieldIndex).text('').append(div);
+                        break;
+                    case 'select':
+                        var select=$('<select id="'+column.field+'">'), options = $.selectArray[column.field];
+                        tarNode.find('td').eq(column.fieldIndex).text('').append(select);
+                        setDivision($('#'+column.field), options);
+                        break;
+                    case 'textarea':
+                        break;
+                    default:
+                        console.log(column.fieldIndex+' '+column.editable);
+                }
+
+            }, evt);
+            for(var i=0, l=txt.length; i<l; i++){
+                tarNode.find('input[type="text"]').eq(i).val(txt[i]);
+            }
+            tarNode.find('td').last().append('<div id="tooling" class="editable-buttons"/>');
+            $('.clear').on('click', function(){ $(this).parent().find('input').val('');});
+            $(submit).on('click', replaceData).appendTo('#tooling');
+            $(cancel).on('click', recoveryData).appendTo('#tooling');
+        }
+    }
+
+    function updateToServerSide(item, data){
+        var itemid = $(item).find('a').attr('href').match(/\d+/g)[0];
+        var datas = {'treeId': itemid, 'oldTreeSerialNo': data[0], 'adminDivision': data[2], 'adminUnit': data[3], 'treeAddr': data[1]}; //傳送至伺服器端的Data產生處,需手動修改對應表格
+        store( 'data/update', datas)
+    }
+
+    var BootstrapTable = $.fn.bootstrapTable.Constructor,
+        _initTable = BootstrapTable.prototype.initTable,
+        _initBody = BootstrapTable.prototype.initBody;
+
+    BootstrapTable.prototype.initTable = function(){
+        var that = this;
+        this.$data = {};
+        _initTable.apply(this, Array.prototype.slice.apply(arguments));
+
+        if (!this.options.clickEdit) {
+            return;
+        }
+
+    };
+
+    BootstrapTable.prototype.initBody = function () {
+        var that = this;
+        _initBody.apply(this, Array.prototype.slice.apply(arguments));
+
+        if (!this.options.clickEdit) {
+            return;
+        }
+
+        var table = this.$tableBody.find('table');
+        that.editing=true;
+
+        table.on('click-row.bs.table', function (e, row, $element, field) {
+            if(field ==='no') return; //|| field ==='noOld'
+            this.$data.thId = $element.data().index;
+            this.$data.itemid = $element.data().uniqueid;
+            this.$data.divi = parseInt(row.area);
+            this.$data.town=parseInt(row.town);
+            clikcToEdit(this, $element);
+        }.bind(this));
+    };
+})(jQuery);

+ 107 - 0
src/extensions/click-edit-row/data1.json

@@ -0,0 +1,107 @@
+[
+    {
+        "id": 0,
+        "name": "Item 0",
+        "price": "$0"
+    },
+    {
+        "id": 1,
+        "name": "Item 1",
+        "price": "$1"
+    },
+    {
+        "id": 2,
+        "name": "Item 2",
+        "price": "$2"
+    },
+    {
+        "id": 3,
+        "name": "Item 3",
+        "price": "$3"
+    },
+    {
+        "id": 4,
+        "name": "Item 4",
+        "price": "$4"
+    },
+    {
+        "id": 5,
+        "name": "Item 5",
+        "price": "$5"
+    },
+    {
+        "id": 6,
+        "name": "Item 6",
+        "price": "$6"
+    },
+    {
+        "id": 7,
+        "name": "Item 7",
+        "price": "$7"
+    },
+    {
+        "id": 8,
+        "name": "Item 8",
+        "price": "$8"
+    },
+    {
+        "id": 9,
+        "name": "Item 9",
+        "price": "$9"
+    },
+    {
+        "id": 10,
+        "name": "Item 10",
+        "price": "$10"
+    },
+    {
+        "id": 11,
+        "name": "Item 11",
+        "price": "$11"
+    },
+    {
+        "id": 12,
+        "name": "Item 12",
+        "price": "$12"
+    },
+    {
+        "id": 13,
+        "name": "Item 13",
+        "price": "$13"
+    },
+    {
+        "id": 14,
+        "name": "Item 14",
+        "price": "$14"
+    },
+    {
+        "id": 15,
+        "name": "Item 15",
+        "price": "$15"
+    },
+    {
+        "id": 16,
+        "name": "Item 16",
+        "price": "$16"
+    },
+    {
+        "id": 17,
+        "name": "Item 17",
+        "price": "$17"
+    },
+    {
+        "id": 18,
+        "name": "Item 18",
+        "price": "$18"
+    },
+    {
+        "id": 19,
+        "name": "Item 19",
+        "price": "$19"
+    },
+    {
+        "id": 20,
+        "name": "Item 20",
+        "price": "$20"
+    }
+]

+ 21 - 0
src/extensions/click-edit-row/editable.css

@@ -0,0 +1,21 @@
+#tooling{
+  float: right;
+}
+.clear{
+  display: block;
+  width: 13px;
+  height: 13px;
+  position: absolute;
+  opacity: 0.6;
+  z-index: 100;
+  top: 50%;
+  right: 26px;
+  margin-top: -10px;
+  cursor: pointer;
+}
+.clear > i{
+  font-size: 1.5em;
+}
+.clear > i:hover{
+  color: hsl(0, 0%, 75%);
+}