/* * @Author: 疯狂秀才(Lucas Huang) * @Date: 2019-09-04 08:40:17 * @LastEditors: 疯狂秀才(Lucas Huang) * @LastEditTime: 2019-11-28 17:55:05 * @QQ: 1055818239 * @Version: v0.0.1 */ import { Component, OnInit, ViewChild } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { TreeTableComponent } from '@farris/ui-treetable'; @Component({ selector: 'virtual-load-demo', templateUrl: './demo-large-data.component.html', }) export class DemoLargeDataComponent implements OnInit { @ViewChild('tt') tt: TreeTableComponent; expandAll = false; cols = [ { field: 'name.dfName', title: '名称', width: 200 }, { field: 'code.dfCode', title: '编号', width: 100, sortable: true }, { field: 'fullName', title: '全称', width: 200, sortable: true }, { field: 'countryOrRegion.countryOrRegion.countryOrRegion_Name.dfName', title: '国家', width: 100 }, { field: 'countryOrRegion.countryOrRegion.countryOrRegion', title: '国家ID', width: 200 } ]; treedata = []; constructor(private http: HttpClient) { } ngOnInit(): void { this.loadData(); } loadData() { this.http.get('assets/data/bigdata-city.json').subscribe( (d: any) => { const start = new Date().getMilliseconds(); const treeResult = this.makeTree(d); const end = new Date().getMilliseconds(); console.log(`start:${start}, end: ${end}; count: ${ end - start }`); this.treedata = treeResult; }); } private makeTree(data) { const r = data.filter(t => t.treeInfo.layer === 1).map(t => { return { data: t, children: [], expanded: this.expandAll }; }); r.forEach(e => { const childs = data.filter( c => c.treeInfo.path.substr(0, 4) === e.data.treeInfo.path ); e.children = this.makeTreeChildren(childs, e.data.treeInfo); }); return r; } private makeTreeChildren(childs, treeInfo) { const pLayer = treeInfo.layer + 1; const pPathLen = treeInfo.layer * 4; return childs.filter( c => c.treeInfo.layer === pLayer && c.treeInfo.path.substr(0, pPathLen) === treeInfo.path).map( d => { return { data: d, children: this.makeTreeChildren(childs, d.treeInfo), expanded: this.expandAll }; }); } selectNodeById() { const id = prompt('选中行ID'); if (id !== '') { this.tt.selectNode(id); } } }