import { extend } from 'lodash-es'; /* * @Author: 疯狂秀才(lucas huang) * @Date: 2018-12-18 13:38:51 * @LastEditors: 疯狂秀才(Lucas Huang) * @LastEditTime: 2019-11-15 15:13:56 * @Company: Inspur * @Version: v0.0.1 */ import { TreeTableComponent } from './treetable.component'; import { RowNode, TreeNode } from './types/treenode'; import { cloneDeep } from 'lodash-es'; export class SearchHandle { allNodes = []; constructor(private ttInstance: TreeTableComponent) { } search(field: string, value: string, from: 'client'|'server' = 'client'): any { if (!this.allNodes.length) { this.allNodes = cloneDeep(this.ttInstance.state.rowNodes); } switch (from) { case 'server': this.searchOnServer(field, value); break; default: if (value !== '' && value !== undefined) { const values = this.searchOnClient(field, value, this.allNodes); this.ttInstance.state.searchRowNodes = null; this._updateSerializedValues(values); } else { this.ttInstance.updateSerializedValue(); } if (this.ttInstance.checkeds && this.ttInstance.checkeds.length) { this.ttInstance.checkedNodes(this.ttInstance.checkeds.map(n => n.data[this.ttInstance.idField])); } else { this.ttInstance.detectChanges(); } break; } } private _updateSerializedValues(visibleItems: RowNode[]) { const pids = (visibleItems.map(n => [...n.parents, n.id]) as any).flat(); const pidArr = Array.from(new Set(pids)); const rowNodes = this.allNodes.filter(n => pidArr.some(item=>item==n.id)).map(r => { r.expanded = true; this.ttInstance.updateNodeProperty(r.id, {expanded: true }); return r; }); this.ttInstance.serializedValue = this.resetTreeData(null, rowNodes); this.ttInstance.state.searchRowNodes = this.ttInstance.serializedValue; } findParent(item: TreeNode, allNodes: any[]) { let res = []; if (item && allNodes && allNodes.length) { const p = allNodes.find(t1 => t1.id === item.data[this.ttInstance.idField]); res.push(p); if (p.parent) { res = res.concat(this.findParent(p.parent, allNodes)); } } return res; } private searchExpression(item: RowNode, value: string, fields: string[] = []) { const _fields = fields.length ? fields : this.ttInstance.columns.map(c => c.field); const results = _fields.map(f => { const targetValue = '' + this.getValue(f, item.node.data); if (targetValue !== undefined) { if (typeof targetValue === 'number') { return targetValue === parseFloat(value); } else { return targetValue.indexOf(value) > -1; } } else { console.warn(`不存在列 ${f}`); } }); return results.reduce((flag, curr) => { return flag || curr; }, false); } private getValue(field, data) { if (field) { if (field.indexOf('.') > -1) { try { return field.split('.').reduce( (r, f) => { if (r) { return r[f]; } else { return null; } }, data ); } catch { console.log(field); } } else { return data[field]; } } } getFindTextTotal(field: string, value: string, nodes: RowNode[]) { let t = 0; const getCount = (fields): any => { let c = 0; nodes.forEach(n => { fields.forEach(f => { const targetValue = '' + this.getValue(f, n.node.data); if (targetValue !== undefined) { if (targetValue.indexOf(value) > -1) { c++; } } }); }); return c; }; let _fields = [field]; if (field === '*') { _fields = this.ttInstance.columns.map(c => c.field); } else if (field.indexOf(',') > -1) { _fields = field.split(',').map(f => f.trim()); } t = getCount(_fields); return t; } searchOnClient(field: string, value: string, nodes: RowNode[]) { let resultNodes: RowNode[] = []; if (!value) { return []; } if (field === '*') { resultNodes = nodes.filter(n => this.searchExpression(n, value)); } else if (field.indexOf(',') > -1) { resultNodes = nodes.filter(n => this.searchExpression(n, value, field.split(',').map(f => f.trim()))); } else { value = value.toLowerCase(); if (field.indexOf('.') === -1) { resultNodes = nodes.filter(n => ('' + n.node.data[field]).toLowerCase().indexOf(value) > -1); } else { resultNodes = nodes.filter(n => ('' + this.getValue(field, n.node.data)).toLowerCase().indexOf(value) > -1); } } return resultNodes; } findParents(rowNodes, allNodes) { let res = []; rowNodes.forEach(item => { res = res.concat(this.findParent(item.node, allNodes)); }); return Array.from(new Set(res)); } private resetTreeData(parentNode: RowNode, visibleItems: RowNode[]) { let res = []; let arr = []; if (parentNode === null) { arr = visibleItems.filter(t2 => t2.parent === parentNode); } else { parentNode.node.expanded = true; arr = visibleItems.filter(t2 => t2.parent && t2.parent.data[this.ttInstance.idField] === parentNode.id); if (!arr.length) { parentNode.node.children = []; } else { parentNode.node.children = arr.map( tn => tn.node ); } } arr.forEach( a => { a.visible = true; res.push(a); res = res.concat(this.resetTreeData(a, visibleItems)); }); return res; } private searchOnServer(field: string, value: string) { } }