日期:2014-05-16 浏览次数:20344 次
/**
* Function applyTableSum
*
* 函数功能: 为表格添加合计行,本函数依赖于jquery
*
* @param {}
* table : The table which append the sum row to 需要添加合计行的表格对象
* @param {}
* colArr : The col index Arry which shuld sum 需要合计的列序号数组,从0开始
* @param {}
* capIndex : caption index defalt is 0 合计标题放置的列,默认是0。即第一列
* @param {}
* emptyText : The text not sumed col default is '-' 未合计行的文本
*/
function applyTableSum(table, colArr, capIndex, emptyText) {
//alert(table.children("tr").text);
var me = this;
var _capIndex = capIndex || 0;
var _emptyText = emptyText || "-";
var _SD = "$", _hasD = new Array();
var _SY = "¥", _hasY = new Array();
var _comma = ",", _hasComma = new Array();
if (table != null && table != undefined && colArr != null) {
var lastTr, sumHtml;
var iColData=0, strData;
var trs = table.children("tbody").first().children("tr");
var trLen = trs.length;
var lastTr = trs.last();
var tds, tdLen, tr, td;
var i, j, k;
sumHtml = "<tr class='td_row'>";
var sumCols = new Array(trLen);
// 遍历所有行
for (i = 0; i < trLen; i++) {
tr = trs.get(i);
tr = JQ(tr);
tds = tr.children("td");
if (tds == null || tds == undefined) {
continue;
}
tdLen = tds.length;
for (j = 0, k = 0; j < tdLen; j++) {
td = tds.get(j);
td = JQ(td);
// 如果是要统计的列
if (colArr[k] == j) {
strData = td.text();
if(strData==null||strData==undefined||strData=="")
{
strData="0";
}
// console.log("strData="+strData);
while (strData.indexOf(_SD) != -1) {
strData = strData.replace(_SD, "");
_hasD[j] = true;
}
while (strData.indexOf(_SY) != -1) {
strData = strData.replace(_SY, "");
_hasY[j] = true;
}
while (strData.indexOf(_comma) != -1) {
strData = strData.replace(_comma, "");
_hasComma[j] = true;
}
try {
iColData = parseFloat(strData);
if (sumCols[j] == null || sumCols[j] == undefined) {
sumCols[j] = 0;
}
sumCols[j] = parseFloat(sumCols[j])+iColData;