[英]When loop throgh HtmlTableElement and converting to json it convert only first table json object.but not socond object
我有多個表。 當循環遍歷每個 table.innerHtml 時,一張一張地打印所有表。但是當轉換為 object 時,它只給出一張表 object。
$( ".table" ).each(function( index ,e) {
let tableId = $(this).closest('table').attr('id')
var table = document.getElementById(tableId);
console.table(table.innerHTML+"tb");
let myObj = {
table: [],
add_rows: []
};
for (var i = 0; row = table.rows[i]; i++) {
let tr_obj = [];
for (var j = 0; col = row.cells[j]; j++) {
var drop_down = $("#drop\\[" + j + "\\]").val()
var text_value = $("#text\\[" + i + "\\]\\[" + j + "\\]").val();
tr_obj.push(create_object(drop_down, text_value));
}
myObj['table'].push(tr_obj);
}
console.log(JSON.stringify(myObj['table'])+"ttt")
var div="div"+tableId
var hidden="entry_field_"+tableId+""
document.getElementById(hidden).value = JSON.stringify(myObj).replace(/\\/g, "")
});
當我們控制table.InnerHtml
時,它會打印兩個 table.but MyObj 給出同一張表 object。
我改進了你的小提琴並且myObj
被正確創建(在table
屬性中是兩個表中的行)。 但是,如果您想以 json 格式渲染此 object,您必須重新設計此 object 或在兩個表中渲染相同的 ZA8CFDE6331BD59EB2AC96F8911。 如果要使用不同的tables
道具渲染兩個對象,則必須將myObj
轉換為單獨的對象。 看看我的小提琴:
table.forEach((e,i)=>{
let tr_obj = [];
Array.from(e.rows).forEach((ele,ind)=>{
let cells = []
Array.from(ele.cells).forEach((element,index)=>{
let drop_down = $("#drop\\[" + i + "\\]\\[" + ind + "\\]\\[" + index + "\\]").val();
let text_value = $("#text\\[" + i + "\\]\\[" + ind + "\\]\\[" + index + "\\]").val();
cells.push(create_object(drop_down, text_value));
})
tr_obj.push(cells)
});
myObj['table'].push(tr_obj);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.