zoukankan      html  css  js  c++  java
  • 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: ''
                        }
                    });
                });
    
            });
  • 相关阅读:
    【WPF】 前言
    【设计模式】 建造者
    拖动调整显示框的显示区域大小
    HTTP权威指南
    input自动获取焦点
    元素JS拖动的实现
    手机端html5触屏事件(touch事件)
    使用".."指定git提交范围与"..."指定git提交范围的区别
    Long-term stable release maintenance
    MEMS--微机电系统
  • 原文地址:https://www.cnblogs.com/person008/p/11890763.html
Copyright © 2011-2022 走看看