【发布时间】:2019-07-06 16:36:55
【问题描述】:
我正在尝试制作一个六角网格。所以我从firestore中检索一组十六进制,按id排序,将它们移动到10行(所以前10个进入'row0',接下来的10个进入'row1'等),并使用ngFor显示它们.
问题是当我显示它们时,我得到了这个:
第0行
第1行
第 10 行
第 11 行
第2行
第3行
第4行
第5行
第6行
第7行
第8行
第9行
所以我显然遗漏了一些东西。我会认为它们有问题,但我是控制台。记录行并按预期按顺序查看它们:
行 [row0: Array(10), row1: Array(10), row2: Array(10), row3: Array(10), row4: Array(10), ...] 对象
我可以看到订单似乎基于“行”之后的第一个数字,但不知道为什么。
TS:
服务
export class FirestoreService {
constructor(private db:AngularFirestore) { }
getHexes(){
let hexRows = []
let hexes = this.db.collection('hexes', ref=> ref.orderBy('id')).valueChanges().subscribe(data => {
data.forEach((hex, index) => {
let rowNum = Math.floor(index / 10)
if(hexRows[`row${rowNum}`] == undefined){
hexRows[`row${rowNum}`] = []
}
hexRows[`row${rowNum}`].push(hex)
})
console.log('Rows', hexRows, typeof hexRows);
})
return hexRows
}
组件
ngOnInit() {
this.hexRows = this.afs.getHexes()
}
HTML:
组件
<div *ngFor="let row of hexRows | keyvalue">
{{row.key}}
</div>
更新
因此,在 Deborah 的带领下,我尝试使用 comparefn 并意外地“修复了它”。我做了一个名为“orderByRowNum”的快速测试函数,当我检查它是否运行时,它看起来像预期的那样对列表进行了排序:
TS:
orderByRowNum = (a,b) => {
console.log('A', a);
return a
}
HTML:
<div *ngFor="let row of hexRows | keyvalue:orderByRowNum">
{{row.key}}
</div>
这可能是一个可怕的实现,我不能 100% 确定它为什么会起作用,但我会发布它,因为它至少看起来像是进步。
【问题讨论】:
-
看起来它们是按字母顺序排列的,而不是按数字排列的,即“0”、“1”、“10”、“11”...
-
所以当我使用 console.log 时,它只是以某种方式为我订购了吗?
-
keyvalue管道正在订购它们。来自文档:The output array will be ordered by keys. By default the comparator will be by Unicode point value. You can optionally pass a compareFn if your keys are complex types.(angular.io/api/common/KeyValuePipe) -
太棒了,感谢您清除它。如果您想将其发布为答案,我很乐意接受!
-
@gv0000,你的比较器错了,一定是(a,b)=> { return (+a.substring(3))>(+b.substring(3))}跨度>
标签: angular typescript rxjs google-cloud-firestore ngfor