import { Component, OnInit } from '@angular/core'; import { ChangeDetectorRef } from '@angular/core'; import { BasicDataService } from '../../basic-data-service'; import * as $ from 'jquery'; @Component({ selector: 'app-city', templateUrl: './city.component.html', styleUrls: ['./city.component.scss'] }) export class CityComponent implements OnInit { constructor( private basicDataService: BasicDataService, private changeDetectorRef: ChangeDetectorRef ) { } public col_lists = [ { col_code: 'cityCode', col_chn: '城市代码' }, { col_code: 'cityNameChn', col_chn: '城市中文名' }, { col_code: 'cityNameEng', col_chn: '城市英文名' }, { col_code: 'countryNameChn', col_chn: '所在国家' }, ]; // 搜索条件 SEARCH = ''; //手动输入条件 FLT_POS = 1; //过滤还是定位 SEL_COL = ''; //选择列名 //下拉框 public filterItems = { countryId: '', } countrys: Array; //国家 citys: Array; public row_state: any = { renderFlight: {} }; public render_data: Array = []; //显示数据,即需要显示的数据 ngOnInit() { this.countrys = this.basicDataService.country; this.citys = this.basicDataService.city; this.render_data = this.basicDataService.city; this.basicDataService.city_subject.subscribe( data => { this.citys = data; this.render_data = data; } ) this.basicDataService.country_subject.subscribe( data => { this.countrys = data if(this.countrys && this.countrys.length > 0){//国家列表数据转换 this.basicDataTransHandle(); } } ) if(this.render_data){ //国家列表数据转换 this.basicDataTransHandle(); } /** * 固定表头 */ var tableCont = document.querySelector('#table-container'); function scrollHandle() { var scrollTop = this.scrollTop; this.querySelector('thead').style.transform = 'translateY(' + scrollTop + 'px)'; } tableCont.addEventListener('scroll', scrollHandle); } //国家数据转换处理 basicDataTransHandle() { this.render_data.forEach(item => { let countryLinkIndex = this.countrys.findIndex(val => val.countryId === item['countryId']); if(this.countrys[countryLinkIndex]){ item['countryNameChn'] = this.countrys[countryLinkIndex].countryNameChn; } }) } /** * 选中某行 * @param i */ selectRow(i) { this.row_state = i; } //输入框回车事件 inputEnter() { if (this.FLT_POS === 1) { this.selectChange() } else { this.inputPosition(this.render_data) } } //下拉框选择筛选 selectChange() { this.render_data = this.citys.filter(item => { let is_satis = true; for (const key in this.filterItems) { if (this.filterItems.hasOwnProperty(key) && this.filterItems[key]) { if (item[key] == this.filterItems[key]) { is_satis = true; continue; } else { is_satis = false; break; } } } return is_satis; }) if (this.FLT_POS === 1) { this.inputFilter(this.render_data); } else { this.row_state = this.render_data.length > 0 ? this.render_data[0] : { renderFlight: {} }; } } //输入框筛选 inputFilter(filterData) { if (this.SEARCH) { if (this.SEL_COL) { let sel_col = this.SEL_COL; this.render_data = filterData.filter(item => { if (item[sel_col]) { if (item[sel_col].toString().match(new RegExp(this.SEARCH, 'i'))) { return true; } } else { return false; } }) } else { this.render_data = filterData.filter(item => { let is_satis = false; let cols = this.col_lists; for (let i = 0; i < cols.length; i++) { if (item[cols[i].col_code]) { if (item[cols[i].col_code].toString().match(new RegExp(this.SEARCH, 'i'))) { is_satis = true; break; } else { is_satis = false; continue; } } else { is_satis = false; continue; } } return is_satis; }) } } else { this.render_data = filterData; } } positionArray: Array = []; positionArrayIndex: number = 0; //输入框定位 inputPosition(filterData) { let transArray = [] if (this.SEL_COL) { let sel_col = this.SEL_COL; filterData.forEach(element => { if (element[sel_col]) { if (element[sel_col].toString().match(new RegExp(this.SEARCH, 'i'))) { transArray.push(element) } } else { if (!this.SEARCH) { transArray.push(element) } } }); } else { filterData.forEach(element => { let cols = this.col_lists; for (let i = 0; i < cols.length; i++) { if (element[cols[i].col_code]) { if (element[cols[i].col_code].toString().match(new RegExp(this.SEARCH, 'i'))) { transArray.push(element) break; } else { continue; } } else { if (this.SEARCH) { continue; } else { transArray.push(element) break; } } } }); } if (transArray.length > 0) { if (transArray.toString() == this.positionArray.toString()) { this.positionArrayIndex++; if (this.positionArrayIndex === this.positionArray.length) { this.positionArrayIndex = 0; } this.row_state = this.positionArray[this.positionArrayIndex]; } else { this.positionArray = transArray; this.positionArrayIndex = 0; this.row_state = this.positionArray[this.positionArrayIndex]; } } else { alert('没有搜索到相关数据'); } this.changeDetectorRef.detectChanges(); let tr = $(".selected"); let objTr = tr[0]; if (objTr) { $("#table-container").animate({ scrollTop: objTr.offsetTop - 42 }, "slow"); } } }