import { Component } from '@angular/core'; @Component({ selector: 'cm-table-doc-5', templateUrl: 'table-doc-5.component.html', styleUrls: ['table-doc-5.component.css'] }) export class TableDoc5Component { filterNameArray = [ { name: 'Joe', value: false }, { name: 'Jim', value: false }, ]; filterAddressArray = [ { name: 'London', value: false }, { name: 'Sidney', value: false } ]; sortMap: any = { name: null, age: null, address: null }; sortName: any = null; sortValue: any = null; data = [ { name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', }, { name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', }, { name: 'Jim Red', age: 32, address: 'London No. 2 Lake Park', } ]; copyData = [...this.data]; sort(sortName: any, value: any) { this.sortName = sortName; this.sortValue = value; Object.keys(this.sortMap).forEach(key => { if (key !== sortName) { this.sortMap[key] = null; } else { this.sortMap[key] = value; } }); this.search(); } search() { const searchAddress = this.filterAddressArray.filter(address => address.value); const searchName = this.filterNameArray.filter(name => name.value); const filterFunc = (item: any) => { return (searchAddress.length ? searchAddress.some(address => item.address.indexOf(address.name) !== -1) : true) && (searchName.length ? searchName.some(name => item.name.indexOf(name.name) !== -1) : true) }; this.data = [...this.copyData.filter(item => filterFunc(item))]; this.data = [...this.data.sort((a, b) => { if (a[this.sortName] > b[this.sortName]) { return (this.sortValue === 'ascend') ? 1 : -1; } else if (a[this.sortName] < b[this.sortName]) { return (this.sortValue === 'ascend') ? -1 : 1; } else { return 0; } })]; } reset(array: any) { array.forEach((item: any) => { item.value = false; }); this.search(); } }