html中对表格数据进行计算,把html表格中数据转化为数据

function tableInfo(id) {//把页面的表转为两维数组

var tr_s = $("tr", $("#grideData"));//grideData是表格的id

var col_length = 0;

var rows = $("td,th", $(tr_s[0]));

$.each(rows, function (i, n) {

if ($(n).attr("colspan") == null) {

col_length++;

} else {

col_length = parseInt(col_length) + parseInt($(n).attr("colspan"));

}

});

var table_array = new Array();

for (var i = 0; i < tr_s.length; i++) {

var col_array = new Array();

for (var j = 0; j < col_length; j++) {

col_array.push("#");

}

table_array.push(col_array);

}

for (var i = 0; i < tr_s.length; i++) {

var rows = $("td,th", $(tr_s[i]));

for (j = 0; j < rows.length; j++) {

var col_num = $(rows[j]).attr("colspan");

var row_num = $(rows[j]).attr("rowspan");

if (col_num != null || row_num > 1) {

if (col_num != null) {

var count = parseInt(col_num);

for (var c = j; c < (j + count); c++) {

if (table_array[i][c] == "#") {

table_array[i][c] = $.trim($(rows[j]).text());

} else {

for (var h = j; h < table_array[i].length; h++) {

if (table_array[i][h] != "#") {

continue;

}

if ($.trim($(rows[j]).text()) != "%") {

table_array[i][h] = $.trim($(rows[j]).text());

} else {

table_array[i][h] = $.trim($(rows[j - 1]).text()) + "%";

}

break;

}

}

}

}

if (row_num > 1) {

var count = parseInt(row_num);

for (var c = i; c < (i + count); c++) {

if (table_array[c][j] == "#") {

table_array[c][j] = $.trim($(rows[j]).text());

}

}

}

} else {

if (table_array[i][j] == "#") {

if ($.trim($(rows[j]).text()) != "%") {

table_array[i][j] = $.trim($(rows[j]).text());

} else {

table_array[i][j] = $.trim($(rows[j - 1]).text()) + "%";

}

} else {

for (var h = j; h < table_array[i].length; h++) {

if (table_array[i][h] != "#") {

continue;

}

if ($.trim($(rows[j]).text()) != "%") {

table_array[i][h] = $.trim($(rows[j]).text());

} else {

table_array[i][h] = $.trim($(rows[j - 1]).text()) + "%";

}

break;

}

}

}

}

}

return table_array;

}

function changArray(array) {//数组x轴和y轴对换

var arrays = [];

for (var i = 0; i < array.length; i++) {

for (var j = 0; j < array[j].length; j++) {

arrays[i][j] = array[j][i];

}

}

return arrays;

}

function getArrayByTitle(titles, tableInfo, type, types) {//根据文本计算数据下标

var str = "{";

if (types == 1) {//左边有全拼行的情况下

tableInfo = changArray(tableInfo);

}

if (type == "col") {

for (var j = 0; j < titles.length; j++) {

str += "\"" + titles[j] + "\":[";

for (var i = 0; i < tableInfo[0].length; i++) {

if ($.trim(titles[j]) == $.trim(tableInfo[0][i])) {

str += i + ",";

}

}

str = str.substring(0, str.length - 1);

str += "],";

}

str = str.substring(0, str.length - 1);

str += "}";

} else {

if (type == "row") {

for (var j = 0; j < titles.length; j++) {

str += "\"" + titles[j] + "\":[";

for (var i = 0; i < tableInfo.length; i++) {

if ($.trim(titles[j]) == $.trim(tableInfo[i][0])) {

str += i + ",";

}

}

str = str.substring(0, str.length - 1);

str += "],";

}

str = str.substring(0, str.length - 1);

str += "}";

}

}

return str;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值