我有一个格式如下的数组,其中第一个参数是要显示的文本,第二个是列号,第三个是行号。我想根据它们的列和行位置显示这些元素。
[[ "a", "0", "0"],
["b", "0", "1"],
["c", "0", "2"],
["d", "1", "0"],
["e", "1", "1"],
["f", "2", "0"],
["g", "2", "1"]
]
期望的输出:
a d f
b e g
c
最佳答案
你可以这样做: Plunker
组件:
value : string;
rows : any[];
cols : any[];
arr : any = [[ "a", "0", "0"],
["b", "0", "1"],
["c", "0", "2"],
["d", "1", "0"],
["e", "1", "1"],
["f", "2", "0"],
["g", "2", "1"]];
constructor() {
this.rows = Array(3).fill();
this.cols = Array(3).fill();
}
activeElement(r,c) : string{
for(let elem of this.arr){
if(parseInt(elem[1]) === c && parseInt(elem[2]) === r){
console.log(true);
return elem[0];
}
}
return ""
}
模板:
<div *ngFor="let row of rows; let rowId = index">
<span *ngFor="let col of cols; let colId = index">
{{activeElement(rowId,colId)}}
</span>
</div>
关于css - Angular 2 : displaying items in row/column fashion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42980156/