PrimeNG ——Let Filtering, Sorting and Lazy loading work together!
来源:互联网 发布:表白的话感人的 知乎 编辑:程序博客网 时间:2024/05/22 00:23
Preface
Sorting, filtering is not working well with Virtual scroll (Lazy loading) in PrimeNG data table. Actually with large data set ,primeNg table become hang, for this resolution we can implement Virtual scroll(Lazy loading). But there is a problem that Sorting, Filtering is not working well with lazy loading.
When a user is inserting some value in the global filter input or the column filter input or clicking the sorting button with the lazy loading on, the lazy loading function will be invoked. So we can invoke filtering and sorting function when the lazy loading function invoked.
在PrimeNG的表格中,当我们开启懒加载,会有很多问题,比如不能排序,不能过滤,或者排序过滤不能在全部数据的范围内发生。然而如果加载大量数据, primeNG的表格就会阻塞, 为了解决这个问题,我们不得不使用懒加载。那么我们改如何解决这个问题呢?
当一个用户在全局搜索框或者列搜索框或者点击排序按钮时, 懒加载函数会被调用,因此我们可以在此时调用自定义的排序、过滤方法。
Resolution
Firstly, let’s create a function to implement global filtering
首先,我们写一个全局过滤的方法
filterGlobal(row, value) { for(let i=0; i<this.columns.length; i++) { let column: Column = this.columns[i]; if(row[column.name] == null) { continue; } let rowValue: String = row[column.name].toString().toLowerCase(); if(rowValue.includes(value.toLowerCase()) { return true; } } return false;}
Next, let’s create a filter to implement column filtering
接下来,我们创建一个列过滤的方法
filterField(row, filter) { for (var columnName in filter) { if (row[columnName] == null) { return false; } let rowValue: String = row[columnName].toString().toLowerCase(); let filterMatchMode: String = filter[columnName].matchMode; if (filterMatchMode.includes("contains") && rowValue.includes(filter[columnName].value.toLowerCase())) { return true; } else if (filterMatchMode.includes("startsWith") && rowValue.startsWith(filter[columnName].value.toLowerCase())) { return true; } else if (filterMatchMode.includes("in") && filter[columnName].value.includes(rowValue)) { return true; } else { return false; } } }
A comparison method for sorting
一个用于排序的比较方法
compareField(rowA, rowB, field: string): number { if (rowA[field] == null) return 1; if (typeof rowA[field] === 'string') { return rowA[field].localeCompare(rowB[field]); } if (typeof rowA[field] === 'number') { if (rowA[field] > rowB[field]) return 1; else return -1; }}
And then, we should create a lazy loading function
然后,我们应该写一个懒加载的函数
loadLazy(event: LazyLoadEvent) { this.loading = true; this.filteredRows = this.dataSource.filter(row => this.filterField(row, event.filters)); if(event.globalFilter.trim()) { this.filteredRows = this.filteredRows.filter(row => this.filterGlobal(row, event.globalFilter)); } this.filteredRows.sort((a, b) => this.compareField(a, b, event.sortField) * event.sortOrder); this.gridData = this.filteredRows.slice(event.first, (event.first + event.rows)); this.totalRows = this.filteredRows.length; this.loading = false;}
- PrimeNG ——Let Filtering, Sorting and Lazy loading work together!
- How Blocks And PHTML Work Together
- Powerpath and VxVM work together mechanism
- ActiveRecord, Lazy and Eager Loading
- Lazy Loading CSS and JS files
- How to get JBoss and Apache to work together
- How to make ROS and Opencv work together
- Telerik Grid Server Side Paging, Sorting and Filtering
- ADF Bussiness Components: In-Memory Sorting and Filtering
- lazy loading
- lazy loading
- LINQ to SQL: Lazy and Eager Loading Hiccups
- the design principles and code analysis of hibernate lazy loading
- Let Two DIV Scroll Together
- Do they actually work together?
- How PCTFREE and PCTUSED Work Together【每日一译】--2012-10-21
- EF学习和使用(五)Lazy Loading and Eager Loading
- MVC 5 with EF 6 in Visual Basic - Sorting, Filtering and Paging
- 深度学习笔记
- Bad magic number in super-block 当尝试打开 /dev/sda3 时 找不到有效的文件系统超级块.
- aws ec2主机配置ss
- 蓝桥杯练习---- 基础练习 数列排序
- 机器学习之决策树算法
- PrimeNG ——Let Filtering, Sorting and Lazy loading work together!
- 字节全讲解
- Java web 入门
- No qualifying bean of type(xxxxxx)
- HDOJ 2521 反素数
- 线程池中多线程设置超时退出监控
- 用Python从新浪下载A股复权因子信息
- 剑指offer——斐波那契数列
- 今天第一次写