• bootsrap table动态添加数据的实现


    <div style="75%;margin-left:16.5%">
         <div class="columns pull-left">
              <button id="add" type="button" class="btn  btn-primary">
                  <i class="fa fa-plus" aria-hidden="true"></i>添加
              </button>
              <button  id="del" type="button" class="btn  btn-danger">
                  <i class="fa fa-trash" aria-hidden="true"></i>删除
              </button>
             <button  id="save" type="button" class="btn  btn-primary">
                  <i class="fa fa-save" aria-hidden="true"></i>保存
             </button>
        </div>
        <table id="table"></table>
    </div>

    ji代码

    var instrumentId = '[[${instrumentInfoDO.instrumentId}]]';
            var instrumentName = '[[${instrumentInfoDO.instrumentName}]]';
            function saveStTime(index, obj){
                var value = $(obj).val();
                $("#table").bootstrapTable('updateCell',{
                    index: index,
                    field: 'stTime',
                    value: value
                });
            }
            function saveEndTime(index, obj){
                var value = $(obj).val();
                $("#table").bootstrapTable('updateCell',{
                    index: index,
                    field: 'edTime',
                    value: value
                });
            }
            $("#save").click(function() {
                var info = $("#table").bootstrapTable('getData');
                var data = { instrumentId: instrumentId,timeSetList: []};
                for (var i = 0; i < info.length; i++) {
                    var timeSet = new Object();
                    timeSet.stTime = info[i].stTime;
                    timeSet.edTime = info[i].edTime;
                    data.timeSetList.push(timeSet);
                }
                var jsonString = JSON.stringify(data);
                $("#timeSetList").val(jsonString);
                save();
            })
    
            $(function() {
                var $table = $('#table');
                var $add = $('#add');
                var $remove = $('#del');
                $table.bootstrapTable({
                    url: 'data2.json',
                    toolbar: '#toolbar',
                    clickEdit: true,
                    showToggle: false,
                    pagination: false,       //显示分页条
                    showColumns: false,
                    showPaginationSwitch: false,     //显示切换分页按钮
                    showRefresh: false,      //显示刷新按钮
                    uniqueId: "ID",
                    strictSearch: false,
                    //clickToSelect: true,  //点击row选中radio或CheckBox
                    columns: [{
                        checkbox: true,
                         '2%'
                    }, {
                        field: 'instrumentId',
                        title: '仪器id',
                        formatter: function (value, row, index) {
                            return instrumentId
                        },
                         '10%',
                        visible: false
                    },{
                        field: 'instrumentName',
                        title: '仪器',
                        formatter: function (value, row, index) {
                            return instrumentName
                        },
                         '10%'
                    },{
                        field: 'stTime',
                        title: '开始时间',
                        formatter: function (value, row, index) {
                            return "<input type='time' class='input-sm form-control' id='stTime"+index+"' value='"+value+"' autocomplete='off' onblur='saveStTime("+ index +", this)' placeholder='请输入开始时间'/>";
                        },
                         '10%'
                    }, {
                        field: 'edTime',
                        title: '结束时间',
                         '5%',
                        formatter: function (value, row, index) {
                            return "<input type='time' class='input-sm form-control' id='edTime"+index+"' value='"+value+"' autocomplete='off' onblur='saveEndTime("+ index +", this)' placeholder='请输入结束时间'/>";
                        },
                    }],
                    onClickCell: function(field, value, row, $element) {
                        $element.attr('contenteditable', true);
                        $element.blur(function() {
                            var index = $element.parent().data('index');
                            var tdValue = $element.html();
                            saveData(index, field, tdValue);
                        })
                    }
                });
    
                function saveData(index, field, value) {
                    $table.bootstrapTable('updateCell', {
                        index: index,       //行索引
                        field: field,       //列名
                        value: value        //cell值
                    })
                }
    
                $remove.click(function () {
                    var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
                        return row.id;
                    });
                    $table.bootstrapTable('remove', {
                        field: 'id',
                        values: ids
                    });
                });
    
                $add.click(function () {
                    var index = $table.bootstrapTable('getData').length;
                    $table.bootstrapTable('insertRow', {
                        index: index,
                        row: {
                            id:index,
                            instrumentId: instrumentId,
                            instrumentName: instrumentName,
                            stTime: '',
                            edTime: ''
                        }
                    });
                });
    
            });
  • 相关阅读:
    CF687D Dividing Kingdom II
    图论——EK算法
    P6082 [JSOI2015]salesman
    联合权值——树上问题2014noip
    P2071 座位安排——二分图最大匹配
    匈牙利优化时间戳的正确性
    P1541 乌龟棋——线性动规
    P1858 多人背包
    P3558 [POI2013]BAJ-Bytecomputer——线性动归
    P2303 [SDOI2012] Longge 的问题
  • 原文地址:https://www.cnblogs.com/person008/p/11890763.html
Copyright © 2020-2023  润新知