javascript - 如何从使用 ngFor 指令动态添加的 HTML 表中恢复标题单元格数据
问题描述
我正在尝试从我的 typescript .ts 文件中的数组标题中检索单元格数据。知道这些单元格是使用指令<th>
动态生成的。ngFor
我只得到用硬(action、a、b、c、d)编写的列名,而不是从我的 API 动态生成的其他名称。我想检索动态生成的列名。
这是我的 HTML 文件:
<table class="table table-striped">
<thead id="thead">
<tr id="head">
<th class="columnName">Action</th>
<th class="columnName">a</th>
<th class="columnName">b</th>
<th class="columnName">c</th>
<th class="columnName">d</th>
<th scope="col" *ngFor="let column of table.columns" class="columnName">{{ column.name }}</th>
</tr>
</thead>
</table>
这是我的 component.ts 文件
getColumnName() {
const theadRow = document.getElementsByClassName('columnName');
// const thList = theadRow.children;
console.log(theadRow);
for (let i = 0; i < 23; i++) {
console.log(theadRow[i].innerHTML);
}
这是我的结果:
解决方案
您可以调用var names = table.columns.map(col => col.name)
以提取名称。
这是有关Array.map()的更多信息
推荐阅读
- python - 如何更快地组合大量 Pandas DataFrame?
- java - 将花式字体重置为正常
- google-chrome - 如何确定哪个 Google Chrome 扩展程序正在请求通知访问?
- jenkins - 将包含“$”的参数传递给 ParameterizedRemoteTrigger 插件?
- javascript - 从 Divi(WP 主题)中删除移动菜单中自动生成的 s
- node.js - 使用 s3 aws sdk 上传文件时出现结束错误后写入
- c++ - 使用文件路径和哈希类型的标准输入运行 certutil -hashfile (c++)
- node.js - 如果集合 B 上存在引用,则 Mongoose 从集合 A 中获取数据
- next.js - getStaticProps() 和 getStaticPaths() 哪里出错了?
- android - 使用本机反应在服务器上下载文件