我想創建一個動態表jQuery中使用我有一些數據對象。動態創建jQuery中幾個for循環的表
我想實現的是在這個JSFiddle
我有數據
的下列對象,這是用來將數據庫中的數據(fieldMapping)映射到在正確的單元格名稱的例子表
[{
"input_source_type":"input",
"field_id":"100786",
"field_name":"name",
"input_id":{"List Table":"Label 1 ref"},
"input_ref":"Label 1 ref"
},
{
"input_source_type":"input",
"field_id":"100787",
"field_name":"desc",
"input_id":{"List Table":"Label 2 ref"},
"input_ref":"Label 2 ref"
},
{
"input_source_type":"input",
"field_id":"100788",
"field_name":"desc",
"input_id":{"List Table":"Label 3 ref"},
"input_ref":"Label 3 ref"
},
{
"input_source_type":"input",
"field_id":"100786",
"field_name":"name",
"input_id":{"List Table":"Label 4 ref"},
"input_ref":"Label 4 ref"
},
{
"input_source_type":"input",
"field_id":"100787",
"field_name":"desc",
"input_id":{"List Table":"Label 5 ref"},
"input_ref":"Label 5 ref"
},
{
"input_source_type":"input",
"field_id":"100788",
"field_name":"desc",
"input_id":{"List Table":"Label 6 ref"},
"input_ref":"Label 6 ref"
}]
這是表數據(字段) - 基本上每行最多可以有2列。以下有陣列的列表,每個陣列是一個行
[
["Label 1 ref"],
["Label 2 ref","Label 3 ref"],
["Label 4 ref","Label 5 ref"],
["Label 6 ref"]
]
每個DataStrucutre從數據庫調用創建,帶有ID和字段一起。這些字段從上面的fieldMapping對象的field_name鍵中鏈接。 這是數據變量,一個基本的例子圖像here
這是我的示例代碼
for (var i in data)
{
if (row instanceof DataStructure)
{
for (var i in fields)
{
var fieldRef = fields[i];
for (var f in this.fieldMapping)
{
if (this.fieldMapping[f].input_id instanceof Object)
{
var colspan = 1;
if(fieldRef.length == 1)
colspan = 2;
var fieldName = '';
for (var x in this.fieldMapping[f].input_id)
{
fieldName = x;
}
var fName = this.fieldMapping[f].field_name;
if (fName == "ID")
fName = "id";
var cellValue = row.getValue(fName);
if (cellValue instanceof DataVariant)
{
if(colspan == 1)
{
if(fieldRef[0] && fieldRef[1])
{
html += '<tr>';
for (var r in fieldRef)
{
if(fieldRef[r] == this.fieldMapping[f].input_id[fieldName])
{
html += '<td>'+cellValue.getData()+'</td>';
}
}
html += '</tr>';
}
}
else if (colspan == 2)
{
if (fieldRef == this.fieldMapping[f].input_id[fieldName])
{
html += '<tr>';
html += '<td colspan="2">'+cellValue.getData()+'</td>';
html += '</tr>';
}
}
}
}
}
}
}
}
}
我想獲得高於JSFiddle的結果,但我不斷收到以下(Fiddle2)
<table>
<tr>
<td colspan="2">Title info</td>
</tr>
<tr>
</tr>
<tr>
<td>This is the main body of the map info window component.</td>
</tr>
<tr>
<td>54.991987, -1.522710</td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td>This is the title for the second map component</td>
</tr>
<tr>
<td>This is the main body of the map info window component.</td>
</tr>
<tr></tr>
<tr>
<td colspan="2">54.991987, -1.522710</td>
</tr>
</table>
我知道這是因爲我使用的循環量導致了問題。我怎樣才能改變這個讓我達到預期的效果?
在正確的方向的任何幫助或指導,將不勝感激
由於
的HTML DIV所附的jQuery
最後動態創建的表被添加到頁面這是好的,但此不是我要求的。根據數組的長度,我需要創建2列或每行1。你甚至沒有使用我的例子 – Pooshonk