Angular TypeScript 中如何获取过滤后表格数据的发送者总数量并展示在页脚
解决方法
你的过滤逻辑全部执行完成后,最终得到的selectedData就是表格当前展示的过滤后数据集,直接基于这个数组统计发送者数量,在filterTitles方法末尾补充赋值逻辑即可。
场景1:统计过滤后所有数据的总条目数(即表格展示的行数)
如果每页数据行对应一条发送记录,要统计总记录数,直接取数组长度即可,修改后的代码如下:
filterTitles() { console.log("Hello") const filterR = this.selectedItem if (this.selectedItem == 'ALL') { this.selectedData = this.unfilteredData; console.log("ALL"); // 如果你需要接收者数量也动态计算,可以替换下面的硬编码 this.receiverNum = 2; } else if (filterR.length > 0) { this.selectedData = this.data.filter( element => element.receiver.toLowerCase().indexOf(filterR.toLowerCase()) > -1 ); console.log("ra"); this.receiverNum = 1; } else { this.selectedData = this.data; } const filter = this.searchedKeyword || ''; if (filter.length > 0) { this.selectedData = this.selectedData.filter( (element: { sender: string; }) => element.sender.toLowerCase().indexOf(filter.toLowerCase()) > -1 ); } // 新增:计算过滤后的发送者总条数 this.senderNum = this.selectedData.length; }
场景2:统计去重后的发送者数量(同一发送人仅计数一次)
如果需要统计不同的发送者总数,过滤掉重复的发送人记录,可以用Set做去重处理,把上面末尾的赋值逻辑替换为:
// 提取所有sender字段值,去重后统计数量 const uniqueSenders = new Set(this.selectedData.map(item => item.sender.trim())); this.senderNum = uniqueSenders.size;
你现有的页脚模板不需要做任何修改,{{ senderNum }}会自动读取最新计算出的数值。
内容的提问来源于stack exchange,提问作者user16235826
相关产品推荐
相关产品推荐

