Advertisement
DarthCoffee

Untitled

Jul 18th, 2020
4,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // app.component.ts
  2. import { Component } from '@angular/core';
  3. import { MatTableDataSource } from '@angular/material/table';
  4.  
  5. @Component({
  6.   selector: 'app-root',
  7.   templateUrl: './app.component.html',
  8.   styleUrls: ['./app.component.scss']
  9. })
  10. export class AppComponent {
  11.  
  12.   filterValues = {};
  13.   dataSource = new MatTableDataSource();
  14.   displayedColumns: string[] = ['name', 'category', 'price', 'rating', 'brand'];
  15.  
  16.   filterSelectObj = [];
  17.   constructor(
  18.   ) {
  19.  
  20.     // Object to create Filter for
  21.     this.filterSelectObj = [
  22.       {
  23.         name: 'NAME',
  24.         columnProp: 'name',
  25.         options: []
  26.       }, {
  27.         name: 'CATEGORY',
  28.         columnProp: 'category',
  29.         options: []
  30.       }, {
  31.         name: 'BRAND',
  32.         columnProp: 'brand',
  33.         options: []
  34.       }, {
  35.         name: 'Price',
  36.         columnProp: 'price',
  37.         options: []
  38.       }, {
  39.         name: 'Rating',
  40.         columnProp: 'rating',
  41.         options: []
  42.       }
  43.     ]
  44.   }
  45.  
  46.   ngOnInit() {
  47.     this.getRemoteData();
  48.  
  49.     // Overrride default filter behaviour of Material Datatable
  50.     this.dataSource.filterPredicate = this.createFilter();
  51.   }
  52.  
  53.   // Get Uniqu values from columns to build filter
  54.   getFilterObject(fullObj, key) {
  55.     const uniqChk = [];
  56.     fullObj.filter((obj) => {
  57.       if (!uniqChk.includes(obj[key])) {
  58.         uniqChk.push(obj[key]);
  59.       }
  60.       return obj;
  61.     });
  62.     return uniqChk;
  63.   }
  64.  
  65.   // Get remote serve data using HTTP call
  66.   getRemoteData() {
  67.  
  68.     const remoteDummyData = [
  69.       {
  70.         "name": "Dell Inspiron Laptop",
  71.         "category": "Electronics",
  72.         "brand": "Dell",
  73.         "price": "Rs. 80,000",
  74.         "rating": "5 Star"
  75.       },
  76.       {
  77.         "name": "Dell Notebook",
  78.         "category": "Electronics",
  79.         "brand": "Dell",
  80.         "price": "Rs. 50,000",
  81.         "rating": "4 Star"
  82.       },
  83.       {
  84.         "name": "Dell Business Laptop",
  85.         "category": "Electronics",
  86.         "brand": "Dell",
  87.         "price": "Rs. 1,20,000",
  88.         "rating": "3 Star"
  89.       },
  90.       {
  91.         "name": "Tommy Hillfiger Shirt",
  92.         "category": "Apparel",
  93.         "brand": "Tommy Hillfiger",
  94.         "price": "Rs. 1,000",
  95.         "rating": "4 Star"
  96.       },
  97.       {
  98.         "name": "Aeropostale T-Shirt",
  99.         "category": "Apparel",
  100.         "brand": "Aeropostale",
  101.         "price": "Rs. 5,000",
  102.         "rating": "5 Star"
  103.       },
  104.       {
  105.         "name": "Tommy Hillfiger T-Shirt",
  106.         "category": "Apparel",
  107.         "brand": "Tommy Hillfiger",
  108.         "price": "Rs. 4,000",
  109.         "rating": "5 Star"
  110.       },
  111.       {
  112.         "name": "Aeropostale Shirt",
  113.         "category": "Apparel",
  114.         "brand": "Aeropostale",
  115.         "price": "Rs. 5,000",
  116.         "rating": "2 Star"
  117.       },
  118.       {
  119.         "name": "Ipad Pro",
  120.         "category": "Electronics",
  121.         "brand": "Apple",
  122.         "price": "Rs. 1,10,000",
  123.         "rating": "5 Star"
  124.       },
  125.       {
  126.         "name": "Macbook Pro",
  127.         "category": "Electronics",
  128.         "brand": "Apple",
  129.         "price": "Rs. 1,10,000",
  130.         "rating": "5 Star"
  131.       },
  132.       {
  133.         "name": "Iphone X",
  134.         "category": "Electronics",
  135.         "brand": "Apple",
  136.         "price": "Rs. 50,000",
  137.         "rating": "4 Star"
  138.       }
  139.     ];
  140.     this.dataSource.data = remoteDummyData;
  141.  
  142.     this.filterSelectObj.filter((o) => {
  143.       o.options = this.getFilterObject(remoteDummyData, o.columnProp);
  144.     });
  145.   }
  146.  
  147.   // Called on Filter change
  148.   filterChange(filter, event) {
  149.     //let filterValues = {}
  150.     console.log(filter)
  151.     console.log(event)
  152.     this.filterValues[filter.columnProp] = event.target.value.trim().toLowerCase()
  153.     this.dataSource.filter = JSON.stringify(this.filterValues)
  154.   }
  155.  
  156.   // Custom filter method fot Angular Material Datatable
  157.   createFilter() {
  158.     let filterFunction = function (data: any, filter: string): boolean {
  159.       let searchTerms = JSON.parse(filter);
  160.       let isFilterSet = false;
  161.       for (const col in searchTerms) {
  162.         if (searchTerms[col].toString() !== '') {
  163.           isFilterSet = true;
  164.         } else {
  165.           delete searchTerms[col];
  166.         }
  167.       }
  168.  
  169.       console.log(searchTerms);
  170.  
  171.       let nameSearch = () => {
  172.         let found = false;
  173.         if (isFilterSet) {
  174.           for (const col in searchTerms) {
  175.             searchTerms[col].trim().toLowerCase().split(' ').forEach(word => {
  176.               if (data[col].toString().toLowerCase().indexOf(word) != -1 && isFilterSet) {
  177.                 found = true
  178.               }
  179.             });
  180.           }
  181.           return found
  182.         } else {
  183.           return true;
  184.         }
  185.       }
  186.       return nameSearch()
  187.     }
  188.     return filterFunction
  189.   }
  190.  
  191.  
  192.   // Reset table filters
  193.   resetFilters() {
  194.     this.filterValues = {}
  195.     this.filterSelectObj.forEach((value, key) => {
  196.       value.modelValue = undefined;
  197.     })
  198.     this.dataSource.filter = "";
  199.   }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement