根据配置项生成表格

优化过后的:
function makeTable(id, _data) {
    var _tableStyle = "width: 100%; background-color: #fff; color: #666;",
        _allTdStyle = "position: relative;padding: 9px 15px;min-height: 20px;line-height: 20px;font-size: " +
      "14px;border-width: 1px;border-style: solid;border-color: #e6e6e6;";
    var _form = document.getElementById(id);
    _form.innerHTML = '';
    var _table = document.createElement('table');
    _table.setAttribute('style', _tableStyle + '');
    var firstTr = document.createElement('tr');
    _table.appendChild(firstTr);
    _form.appendChild(_table);
    _data.forEach(function (ele) {
        var trLast = document.querySelector('#_form table tr:last-child');
        var _tdKey = document.createElement('td');
        _tdKey.setAttribute('style', _allTdStyle + 'background: #f8f8f8; text-align: center; min-width: 100px;');
        _tdKey.setAttribute('rowspan', ele.rows); // 跨行数
        _tdKey.setAttribute('colspan', ele.kcols); // 跨列数
        _tdKey.setAttribute('data-key', ele.key); // 单元格标志
        _tdKey.innerText = ele.name; // key        trLast.appendChild(_tdKey);
        if (ele.th != true) {
            var _tdVal = document.createElement('td');
            _tdVal.setAttribute('style', _allTdStyle + '');
            _tdVal.innerText = ele.val;
            _tdVal.setAttribute('rowspan', ele.rows); // 跨行数
            _tdVal.setAttribute('colspan', ele.vcols); // 跨列数
            _tdVal.setAttribute('data-key', ele.key); // 单元格标志
            trLast.appendChild(_tdVal);
        }
        if (ele.newr) {
            var _tr = document.createElement('tr');
            _table.appendChild(_tr);
        }
    });
}

/*  ------------------------------------------------------------
<div id="_form" style="margin: 10px;"></div>
var _data = [
  {
      key: 'qymc', // key
      name: '企业名称',
      val: data.qymc, // val
      edit: 'ddd',  // 是否可编辑,暂时未处理
      rows: '', // 所占据行数
      kcols: '', // key 列数
      vcols: '3', // val 列数
      newr: '', // 是否要新开一行(以此结尾)
      th: '', // 是否是纯粹标题(无val
  },
]
makeTable('_form', _data); 
-----------------------------------------------------------------*/
原有的:
<div id="_form" class="search_form_div"> </div>
//获取数据并填充弹出层表格数据
function makeTable(id, _data) {
    var _form = document.getElementById(id);
    _form.innerHTML = '';
    var _table = document.createElement('table');
    _table.classList.add('layui-table');
    var firstTr = document.createElement('tr');
    _table.appendChild(firstTr);
    _form.appendChild(_table);
    _data.forEach(function (ele) {
        var _tdKey = document.createElement('td');
        var _tdVal;
        _tdKey.setAttribute('rowspan', ele.rows);
        _tdKey.setAttribute('colspan', ele.kcols);
        _tdKey.setAttribute('data-key', ele.key);
        _tdKey.innerText = ele.name;
        // _tdKey.classList.add('table-td-key');
        _tdKey.setAttribute('style', 'background: #f8f8f8; text-align: center; min-width: 100px; width: 20%;');

        if (ele.th != true){
            _tdVal = document.createElement('td');
            _tdVal.innerText = ele.val;
            _tdVal.setAttribute('rowspan', ele.rows);
            _tdVal.setAttribute('colspan', ele.vcols);
            _tdVal.setAttribute('data-key', ele.key);
            _tdVal.setAttribute('style', 'min-width: 200px;')
            // var newTrLast = document.querySelector('#_form table tr:last-child');
        }
        if (ele.newr) {
            var trLast = document.querySelector('#_form table tr:last-child');
            trLast.appendChild(_tdKey);
            if (_tdVal) {
                trLast.appendChild(_tdVal);
            }
            var _tr = document.createElement('tr');
            _table.appendChild(_tr);
        } else {
            var trLast = document.querySelector('#_form table tr:last-child');
            trLast.appendChild(_tdKey);
            if (_tdVal) {
                trLast.appendChild(_tdVal);
            }
        }
        if (ele.button==true){
            var _input = document.createElement('input');
            _input.setAttribute('type','button');
            _input.setAttribute('value','查看附件');
            _input.classList.add('attachment');
            _input.classList.add('layui-btn');
            _input.classList.add('layui-btn-primary');
            _tdVal .appendChild(_input)
        }
    });
}
// 查看详细信息
tableData.detail = function(rowData){
    var _data = [
        {
            key: 'qymc', // key            name: '企业名称',
            val: rowData.qymc, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'sbmc', // key            name: '设备名称',
            val: rowData.sbmc, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'sbbh', // key            name: '设备编号',
            val: rowData.sbbh, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'sblx', // key            name: '设备类型',
            val: rowData.sblx, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'ggxh', // key            name: '规格型号',
            val: rowData.ggxh, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'zycs', // key            name: '主要参数',
            val: rowData.zycs, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'zzcj', // key            name: '制造厂家',
            val: rowData.zzcj, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'gzsj', // key            name: '购置时间',
            val: rowData.gzsj, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'tysj', // key            name: '投用时间',
            val: rowData.tysj, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'sydd', // key            name: '使用地点',
            val: rowData.sydd, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'yxqk', // key            name: '运行情况',
            val: rowData.yxqk, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'sbsl', // key            name: '设备数量',
            val: rowData.sbsl, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'sfzdjktzsb', // key            name: '是否重点监控特种设备',
            val: rowData.sfzdjktzsb, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'syzbh', // key            name: '使用证编号',
            val: rowData.syzbh, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'syzfzrq', // key            name: '使用证发证日期',
            val: rowData.syzfzrq, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'fzdw', // key            name: '发证单位',
            val: rowData.fzdw, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'jyzq', // key            name: '检验周期',
            val: rowData.jyzq, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'xcjyrq', // key            name: '下次检验时间',
            val: rowData.xcjyrq, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'zhycjysj', // key            name: '最近一次检验时间',
            val: rowData.zhycjysj, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'jyqk', // key            name: '检验情况',
            val: rowData.jyqk, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'fzr', // key            name: '负责人',
            val: rowData.fzr, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'fzrgddh', // key            name: '负责人固定电话',
            val: rowData.fzrgddh, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'fzryddh', // key            name: '负责人移动电话',
            val: rowData.fzryddh, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: '', // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },{
            key: 'fzrdzyx', // key            name: '负责人电子邮箱',
            val: rowData.fzrdzyx, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '3', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },
        {
            key: 'bz', // key            name: '备注',
            val: rowData.bz, // val            edit: 'ddd',  // 是否可编辑,暂时未处理
            rows: '', // 所占据行数
            kcols: '', // key 列数
            vcols: '9', // val 列数
            newr: true, // 是否要新开一行(以此结尾)
            th: '', // 是否是纯粹标题(无val        },

    ];
    makeTable('_form', _data);

猜你喜欢

转载自blog.csdn.net/weixin_39717076/article/details/80852356