本文介绍了构建动态表whit whitHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在javascript whit innerHTML上构建动态表。但是当webapp运行时,它只打印console.log,但不构建表。
I'm try to build a dynamic table on javascript whit innerHTML. but when the webapp runs, it prints only the console.log, but doesn't build a table.
我试过两种方式:
第一种:
success: function (data, status, jqXHR) {
$.each(data, function (index, dati) {
console.log(dati)
var html = "<table width=\'450\' bgcolor=\'white\' border=\'2\' bordercolor=\'black\' cellpadding=\'10\' cellspacing=\'1\'>\n" +
" <tr style=min-width:850px>\n" +
" <td>dati.codiceCOmmessa </td>\n" +
" <td>dati.commessaMainSub</td>\n" +
" <td>dati.settoreCliente</td>\n" +
" <td>dati.nomeCliente</td>\n" +
" <td>dati.titoloQuals</td>\n" +
" <td>dati.keyWordsTopic</td>\n" +
" <td>dati.keyWordsActivities</td>\n" +
" <td>dati.anno</td>\n" +
" <td>dati.dataInizio</td>\n" +
" <td>dati.dataFine</td>\n" +
" <td>dati.referente</td>\n" +
" <td>dati.referenteDoc</td>\n" +
" <td>dati.sviluppatore</td>\n" +
" <td>dati.path</td>\n" +
" </tr>\n" +
"</table>"
html.innerHTML;
})
},
和第二个(whit body.innerHTML或node.innerHTML,应用程序出错了):
and the second(whit body.innerHTML or node.innerHTML, The app makes a mistake) :
success: function (data, status, jqXHR) {
$.each(data, function (index, dati) {
console.log(dati)
innerHTML = "<table width=\'450\' bgcolor=\'white\' border=\'2\' bordercolor=\'black\' cellpadding=\'10\' cellspacing=\'1\'>\n" +
" <tr style=min-width:850px>\n" +
" <td>dati.codiceCOmmessa </td>\n" +
" <td>dati.commessaMainSub</td>\n" +
" <td>dati.settoreCliente</td>\n" +
" <td>dati.nomeCliente</td>\n" +
" <td>dati.titoloQuals</td>\n" +
" <td>dati.keyWordsTopic</td>\n" +
" <td>dati.keyWordsActivities</td>\n" +
" <td>dati.anno</td>\n" +
" <td>dati.dataInizio</td>\n" +
" <td>dati.dataFine</td>\n" +
" <td>dati.referente</td>\n" +
" <td>dati.referenteDoc</td>\n" +
" <td>dati.sviluppatore</td>\n" +
" <td>dati.path</td>\n" +
" </tr>\n" +
"</table>"
})
}
有人可以帮帮我吗?哪里我错了?
somebody can help me? where I'm wrong?
推荐答案
首先,循环可以使事情变得更容易(以及字符串文字),因此可能只显示所有对象值:
At first, loops can make things easier (and string literals too), so may simply display all object values:
var html =
`<table width='450' bgcolor='white' border='2'bordercolor='black' cellpadding='10' cellspacing='1'>
<tr style='min-width:850px'>
${
Object.values(dati)
.map(
value => `<td>${value}</td>`
).join("\n")
}
</tr>
</table>`;
或者如果你不喜欢文字,同样也适用于连接:
Or if you dont like the literals, same works with concatenation too:
var html =
"<table width='450' bgcolor='white' border='2'bordercolor='black' cellpadding='10' cellspacing='1'><tr style='min-width:850px'>"
+ Object.values(dati)
.map(
value => "<td>"+value+"</td>"
).join("\n")
+ "</tr></table>";
你可能想用 html 做某事:
document.body.innerHTML += html;
这篇关于构建动态表whit whitHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!