PrimeNG ——Let Filtering, Sorting and Lazy loading work together!

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;
}

猜你喜欢

转载自blog.csdn.net/weixin_40264719/article/details/78670282