layui动态删除或添加行

layui动态删除或添加行_第1张图片jsp代码:

 


             

        

js代码:
 layui.config({base: basePath + "js/"}).use(['jquery', 'layer', 'form', 'table'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , table = layui.table;
 table.render({
            elem: '#parametes'
            , url: ''
            , toolbar: '#todo'
            ,cols:[[
     {type: 'checkbox', fixed: 'left'}
    , {field: 'parameterCode', title: paraEncode, width: '16%', edit: 'text'}
     , {field: 'parameterName', title: paraName, width: '16%', edit: 'text'}
                , {field: 'parameterType', title: paraType, width: '16%'}
    ]]
      });

       let j = 0;
        table.on('toolbar(parametes)', function (obj) {
            var oldData = table.cache["parametes"];
            switch (obj.event) {
                case 'add_but':
                    var data1 = {/*添加行的列先赋空*/
                        parameterCode: '',
                        parameterName: '',
                        parameterType: ''
                    };
                    //添加
                    oldData.push(data1);
                    //刷新
                    table.reload('parametes', {
                        url: '',
                        data: oldData
                    });
                    j++;
                    break;
                //删除选中的行
                case 'cut_but':
                    //获取选中的行
                     checkStatus = table.checkStatus('parametes');
                    //选中的数据,并转换为json格式
                     selectData = JSON.stringify(checkStatus.data);

                    if (checkStatus.data.length <= 0) {
                        layer.msg('最少选择一行');
                        return;
                    }
                    //新建按钮则删除缓存中的数据,是编辑按钮则从数据库中删除数据
                            //获取表格中缓存数据
                            var tableDT = table.cache.parametes;
                            var tableArr = [];
                            for (var i = 0; i < tableDT.length; i++) {//遍历表格缓存数组
                                var btnObj = tableDT[i];
                                if (btnObj.LAY_CHECKED) {//条件:选中
                                    tableDT.splice(i, 1);//移除后后造成数组下标索引发生变化,所以下面需要i--
                                    i--;
                                    j--;
                                }
                            }
                            tableArr = tableDT;
                            console.log(tableDT);
                            table.reload("parametes", {
                                url:"",
                                data: tableArr   // 将新数据重新载入表格
                            });
                            break;
                        }
        });
   });

你可能感兴趣的:(jquery,linq,javascript)