javascript - Angular 7迭代表
问题描述
我想用一个属性迭代表,但在一行中有 4 个 td,并继续这样直到结束......
我想要这样:
<table>
<tr>
<td>
<mat-checkbox>1</mat-checkbox>
</td>
<td>
<mat-checkbox>2</mat-checkbox>
</td>
<td>
<mat-checkbox>3</mat-checkbox>
</td>
<td>
<mat-checkbox>4</mat-checkbox>
</td>
</tr>
<tr>
<td>
<mat-checkbox>5</mat-checkbox>
</td>
<td>
<mat-checkbox>6</mat-checkbox>
</td>
<td>
<mat-checkbox>7</mat-checkbox>
</td>
</tr>
</table>
我试过这样,但它在一列中:
lista = [
{ value: "1" },
{ value: "2" },
{ value: "3" },
{ value: "4" },
{ value: "5" },
{ value: "6" },
{ value: "7" },
<table *ngFor="let list of lista">
<tr>
<td>
<mat-checkbox>{{ list.value }}</mat-checkbox>
</td>
</tr>
</table>
解决方案
您需要首先将数组分组为4
(块大小)组,然后在模板中简单地对其进行迭代。
在您的组件中:
const lista = [
{ value: "1" },
{ value: "2" },
{ value: "3" },
{ value: "4" },
{ value: "5" },
{ value: "6" },
{ value: "7" }
];
const chunkSize = 4;
// Group in chunks of 4 and take only truthy values
const groups = lista
.map((x, index) => {
return index % chunkSize === 0 ? lista.slice(index, index + chunkSize): null;
})
.filter(x => x);
在您的模板中:
<table >
<tr *ngFor="let item of groups">
<td *ngFor = "let innerItem of item">
<mat-checkbox>{{ innerItem.value }}</mat-checkbox>
</td>
</tr>
</table>
推荐阅读
- r - 仅使用 tidy 有条件地用 R 中的列名替换行子集的值
- javascript - TypeError:饼图不是函数
- python - 解析嵌套的自定义 yaml 标签
- jquery - Jquery中的ID和类属性
- javascript - 将 JS 对象的字符串名称转换为 JS 对象的实例
- r - 从 R 对 RethinkDB 中的对象运行函数
- javascript - php json文件加载后如何发送promise get请求?
- excel - 如何在excel中创建一个公式来检查整数范围
- python - Python - pyad 创建用户名称中带有逗号
- scala - Antlr4 使用参数列表执行程序(Scala)