javascript - 在javascript中每次调用刷新行
问题描述
我正在用 HTML 构建一个表格。
使用 JavaScript,我根据数组中对象的数量附加了一些表行,然后附加单元格。效果很好,这里是代码:
function Build_table(filtered_data){
filtered_data.forEach((obj => {
// Create table rows for each object
var row = tbody.append("tr");
Object.entries(obj).forEach(function(x){
// Create cell within current row.
var cell = row.append("td");
// give cell a value
cell.text(x[1]);
});
})
);
<thead>
<tr>
<th class="table-head">Date</th>
<th class="table-head">City</th>
<th class="table-head">State</th>
<th class="table-head">Country</th>
<th class="table-head">Shape</th>
<th class="table-head">Duration</th>
<th class="table-head">Comments</th>
</tr>
</thead>
<tbody></tbody>
但问题是当我第二次输入搜索时,之前的搜索结果仍然存在。如何每次使用纯 Javascript 刷新?
解决方案
在这种情况下,您需要先清理表格主体的 html,例如:
document.getElementsByTagName("tbody")[0].innerHTML='';
然后是您的旧程序:
filtered_data.forEach((obj => {
// Create table rows for each object
var row = tbody.append("tr");
Object.entries(obj).forEach(function(x){
// Create cell within current row.
var cell = row.append("td");
// give cell a value
cell.text(x[1]);
});
}));
推荐阅读
- django - 我想在我的 url 中传递 productid 以便仅显示特定产品的详细信息
- c++ - 如何创建一个函数来执行节点删除
- javascript - 为什么在对象构造函数中定义的方法在使用“new”创建对象时有效,但在使用“Object.create”时无效?
- python - For循环输出所有相同的值
- visual-studio-code - 获取从工作区文件夹内开始的文件路径 - vscode
- r - R-生成4个字符的8个字母单词的可能模式
- pyomo - 使用 Mindtpy 解决 MINLP 时的 INFO 消息
- fortran - Fortan 完全可移植的 I/O 未格式化文件
- php - 使用 PHP 从电子邮件中提取数据时出现额外字符的问题
- hyperledger-fabric - 将orderer节点加入channel的权限问题