大佬教程收集整理的这篇文章主要介绍了如何将两个 sql 查询添加到一个 html 表中,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个连接到 MSsql 数据库并在其上调用两个 sql 查询的应用程序。它适用于 nodeJs 后端。现在我尝试将 reactJs 中的这两个结果合并到一个 HTML table 中,但我仍然不能。
我有这个代码:
return (
<div classname="App">
<h1>Search in database</h1>
<div classname="form">
<center>
<label>Search Data from DB</label></center>
<input type="text" name="material" />
<button>Search</button>
</div>
<div class="div-table">
<thead>
<div class="div-table-row">
<div class="div-table-col"><th>Type</th></div>
<div class="div-table-col"><th>Place</th></div>
<div class="div-table-col"><th>Free space:</th></div>
</div>
</thead>
<tbody>
{materialList.recordset.map(value=>{
return (
<tbody>
<div class="div-table-col"><td>{value.MATNR}</td></div>
<div class="div-table-col"><td>{value.NLPLA}</td></div>
</tbody>
)
})}
{emptySkate.recordset.map(val=>{
return (
<tbody>
<div class="div-table-col"><td></td></div>
<div class="div-table-col"><td></td></div>
<div class="div-table-col"><td>{val.linE}</td></div>
</tbody>
)
})}
</tbody>
</div>
</div>
)
结果是:
我想要这样的结果:
我多次尝试更改tbody和table中的table,等等,但还是没有得到我想要的结果,你不知道如何编辑代码?
如果我正确理解您的问题,您可以执行以下操作:
function getRows(materialList,emptySkatE) {
// some assumptions
// 1. materialList and emptySkate are linked with index.
// 2. materialList[0] and emptySkate[0] makes up the first row
// 3. materialList is the base list,if this is empty,no rows will be returned
// 4. if emptySkate is empty,the last column will be null or empty
const result = materialList.map((ml,indeX) => {
const es = emptySkate[index] || {};
return {
...ml,...es
}
});
return result;
}
现在,在您的 tbody
中,您可以在 getRows
调用返回的数组上循环,而不是循环遍历两个不同的数组。
const rows = getRows(materialList.recordset,emptyStake.recordset);
return (
<tbody>
{rows.map(value=>{
return (
<tr>
-- below markup is just to illustrate
<div class="div-table-col"><td>{value.MATNR}</td></div>
<div class="div-table-col"><td>{value.NLPLA}</td></div>
<div class="div-table-col"><td>{val.LinE}</td></div>
</tr>
)})}
</tbody>
)
以上是大佬教程为你收集整理的如何将两个 sql 查询添加到一个 html 表中全部内容,希望文章能够帮你解决如何将两个 sql 查询添加到一个 html 表中所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。