// 获取表格数据 function getDomData() { const dom = $('#updateTable').prev() if (!dom.length) return const table = $('#updateTable').prev().find('.o_list_table') const customTable = table.clone() customTable.addClass('customTable') table.parent().append(customTable) table.hide() const thead = customTable.children('thead') const tbody = customTable.children('tbody') const tfooter = customTable.children('tfoot') const tableData = [] const tbody_child = tbody.children() const tbody_child_len = tbody_child.length for (let v = 0; v < tbody_child_len; v++) { // 将数据取出来到tableData里面 const data = tbody_child[v].innerText.split('\t') const [index, deep, name, Φ, value] = data tableData.push({ index, deep, name, Φ, value }) } const ΦList = [...new Set(tableData.map(_ => _.Φ))] // ΦList去重 const newTableData = {} tableData.forEach(_ => { const key = _.deep + '|' + _.name !newTableData[key] ? newTableData[key] = { i: _.index } : ''; if (_.Φ) { // 去除没有Φ的脏数据 newTableData[key]['Φ' + _.Φ] = _.value newTableData[key]['Φ' + _.Φ + 'i'] = _.index } }) // console.log(tableData, ΦList, newTableData); if (ΦList.filter(_ => _).length == 0) return; handleThead(thead, ΦList, tfooter) handleTbody(tbody, newTableData, ΦList, table ) } // 重新设置表头、 function handleThead(thead, ΦList, tfooter) { const dom = thead.children().eq(0).children() const len = dom.length dom.eq(0).attr('rowspan', 2) dom.eq(1).attr('rowspan', 2) len == 5 ? dom.eq(2).attr('rowspan', 2) : '' dom.eq(-2).attr('colspan', ΦList.length) dom.eq(-1).remove() if(tfooter && tfooter.length) { tfooter.children().each(function () { $(this).children().eq(-1).remove() }) } const tr = document.createElement('tr') for (let v = 0; v < ΦList.length; v++) { const th = document.createElement('th') th.innerText = 'Φ' + ΦList[v] tr.append(th) } thead.append(tr) } // 重新设置表格 function handleTbody(tbody, newTableData, ΦList, table) { console.log(newTableData) tbody.html('') let i = 0 const data = Object.keys(newTableData) // data.sort((a, b) => { // a = a.split('=')[1].split('%')[0] // b = b.split('=')[1].split('%')[0] // return a - b // }) data.forEach(_ => { i++ const tr = $('') const td0 = $('') td0.text(i) tr.append(td0) const lit = _.split('|') // const td1 = $('') const td2 = $('') td1.text(lit[0]) td2.text(lit[1] || '') tr.append(td1) tr.append(td2) ΦList.forEach(Φ => { const td = $('') td.text(newTableData[_]['Φ' + Φ]) td.attr('col', newTableData[_]['Φ' + Φ + 'i']) td.attr('val', newTableData[_]['Φ' + Φ]) td.attr('coustomTd', 1) tr.append(td) }) // // for (let j = 0; j < ΦList.length; j++) { // // const td = document.createElement('td') // // td.innerText = newTableData[data[v]][_] // // th.append(td) // // } tbody.append(tr) }) } getDomData()