關於如何在ElementUI中實現統計Table篩選結果數量


在開發單位自己的系統時,領導提了這個需求,在看了ElementUI發現並沒有實現這個功能。

遂向官方求解,得回復:自己在filter-method 中實現。於是便有了思路。

 

這里本人使用了一個比較暴力的方法,僅供參考:

1、給所有column的filter-method事件綁定一個對應方法:

     filterMethodsXXX(value, row){
        if(row.brandName === value) {
          if(this.filterArray.filterXXXSet.indexOf(row.id)===-1){
            this.filterArray.filterXXXSet.push(row.id);
          }
          return true;
        }else{
            return false;
        }
      }

意思就是給每個column設置一個數組用於存儲篩選結果。

2、在Table的filter-change事件中綁定一個方法,使用使用ES6 中的Set 數據結構進行交集操作(數組為空則跳過計算)

 filterChange(filters){ let tempSet = new Set(this.filterArray.filterBrandNameArray); for (let key in this.filterArray) { if(this.filterArray[key].length===0){ continue; } tempSet = new Set(this.filterArray[key].filter(x => tempSet.has(x))); }

最終tempSet的長度即為統計值。

 

        let a = new Set([1, 2, 3]);
        let b = new Set([3, 5, 2]);
        // 交集
        let intersectionSet = new Set([...a].filter(x => b.has(x)));
        // [2,3]

 

之后發現其實可以用array的indexOf來做,沒必要用Set,於是:

        let tempSet = this.filterArray.filterBrandNameArray;
        for (let key in this.filterArray) {
          if(this.filterArray[key].length===0){
            continue;
          }
          tempSet = this.filterArray[key].filter(x => {return tempSet.indexOf(x)!==-1;});
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM