LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

JS對table多個列進行合計并在最后一行顯示多個列合計數

liguoquan
2025年8月1日 10:1 本文熱度 753
?:JS對table多個列進行合計并在最后一行顯示多個列合計數


function sumColumns(tableId, colIndices) {

const table = document.getElementById(tableId);

const sums = new Array(colIndices.length).fill(0);


// 遍歷除表頭外的所有行,累加各指定列的值

for(let i = 1; i < table.rows.length; i++) {

  colIndices.forEach((colIndex, idx) => {

    const cell = table.rows[i].cells[colIndex];

    if(cell) {

      const value = parseFloat(cell.textContent) || 0;

      sums[idx] += value;

    }

  });

}


// 創建合計行并插入(如果最后一行已經是合計行則先刪除)

const lastRow = table.rows[table.rows.length - 1];

if (lastRow && lastRow.getAttribute('data-summary-row') === 'true') {

  table.deleteRow(table.rows.length - 1);

}

const newRow = table.insertRow(-1);

newRow.setAttribute('data-summary-row', 'true');


const cellCount = table.rows[0].cells.length;

for(let i = 0; i < cellCount; i++) {

  const newCell = newRow.insertCell(i);

  newCell.style.textAlign = 'center';

  newCell.style.backgroundColor = '#FFFFAA';

  newCell.style.border = '1px solid #000000';


  if(i === 1) {

    newCell.textContent = '合計';

    newCell.style.fontWeight = 'bold';

  } else {

    const sumIndex = colIndices.indexOf(i);

    if(sumIndex !== -1) {

      newCell.textContent = sums[sumIndex];

      newCell.style.fontWeight = 'bold';

    } else {

      newCell.textContent = '';

    }

  }

}

}


function getColumnIndexByChar(tableId, char) {

const table = document.getElementById(tableId);

if (!table) return -1;

const firstRow = table.rows[0];

if (!firstRow) return -1;

for (let i = 0; i < firstRow.cells.length; i++) {

  if (firstRow.cells[i].textContent.includes(char)) {

    return i; // 返回包含該字符的列索引

  }

}

return -1; // 未找到包含該字符的列

}


// 使用示例

const index1 = getColumnIndexByChar('result_table', '件數');

const index2 = getColumnIndexByChar('result_table', '噸數');

console.log(index1);

console.log(index2);

var colindexarry=[];

colindexarry.push(index1);

colindexarry.push(index2);

sumColumns('result_table', colindexarry);

sumColumns('result_table', [2, 3, 5]);



該文章在 2025/8/1 16:39:56 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲视频手机在线观看 | 亚洲AⅤ视频一区二区三区 日韩一区二区欧美一区 | 最新国产精品视频第一页 | 香蕉啪视频在线观看视频久 | 色七七在线免费观看视频 | 亚洲国产精品色一区二区 |