// ---表格组件-html
<!-- 表格滚动 -->
<div class="tableContainer">
<div class="right_table_div" [ngStyle]="{height:scrollHeight+'px'}">
<div class="table_title_div table_width_item">
<div *ngIf="isSnumber" style="width: 120px;">序号</div>
<div *ngFor="let item of theadListData;let index = index" [ngStyle]="{width: item['width']}">{{item.text}}</div>
</div>
<div class="table_items_div" #scrollerListBox [ngStyle]="{'overflow': scrollerOverflow}" (mouseenter)="mouseEnterScroll()" (mouseleave)="mouseLeaveScroll()">
<div class="table_item_div table_width_item" *ngFor="let item of tableData;let index = index" (click)="handleTableClick(item,index)" [ngClass]="avtiveTdIndex == index ? 'table-list-action' : ''">
<div *ngIf="isSnumber" style="width: 120px;">{{index+1}}</div>
<div *ngFor="let itemTbody of theadListData" [ngStyle]="{width: itemTbody['width']}" [ngClass]="itemTbody['isOverflow'] ? 'textOverflow' : ''">{{isNullHandleFn(item[itemTbody['fieldName']])}}</div>
</div>
</div>
</div>
</div>
// ---表格组件-ts
import {Component,ViewEncapsulation,Input,Output,SimpleChanges,EventEmitter,ElementRef,ViewChildren, ViewChild,QueryList} from '@angular/core';
/**数据传参格式、参数说明
* scrollHeight //容器高度(数字)
* isSnumber //第一列是否显示序号(布尔值)
* theadListData[{text:'',fieldName:'',width:'',isOverflow:true,}]:表头相关(数组)(有几列就写几条数据)
* text-表头文字(字符串)、
* fieldName-数据取值字段名(字符串)、
* width-每一列宽度(例如'25%'或者'100px')(字符串)、
* isOverflow-当前列是否超出显示...(布尔值)
* tableData[]:数据JSON
*/
@Component({
selector: 'table-scroll',
templateUrl: './table-scroll.component.html',
styleUrls: ['./table-scroll.component.less'],
})
export class TableScrollComponent {
constructor(
) {}
@Input() scrollHeight;//容器高度
@Input() isSnumber;//第一列是否显示序号
@Input() theadListData;//表头数据JSON
@Input() tableData;//数据JSON
@Output() clickTrRowFn= new EventEmitter<string>();//点击每一行
//ngOnChanges(changes: SimpleChanges) {
//}
// ---表格---
avtiveTdIndex = -1;
// 滚动列表
@ViewChild('scrollerListBox', {static: false}) scrollerListBox: ElementRef;
// 是否允许手动滚动
scrollerOverflow: string = 'hidden';
timer: any;
times = 0;
// 滚动列表方法
scrollerFn() {
// 获取页面元素
const element = this.scrollerListBox.nativeElement;
// 开始循环滚动
this.timer = setInterval( () => {
this.times++
element.scrollTop = this.times;
const marginBottom = element.scrollHeight - element.scrollTop - element.clientHeight;
// 如果当前滚动到底 10px 时追加数据
if (marginBottom <= 5) {
this.times = 0
}
}, 50); // 50 ms 滚动一次,基本是平滑滚动,再大一点就有点卡卡的感觉了
}
// 鼠标移入停止滚动
mouseEnterScroll() {
clearInterval(this.timer);
// 允许手动滚动
this.scrollerOverflow = 'auto';
}
// 鼠标移出开始滚动
mouseLeaveScroll() {
this.times = this.scrollerListBox.nativeElement.scrollTop;
this.scrollerFn();
// 采用自动滚动,隐藏滚动条
this.scrollerOverflow = 'hidden';
}
handleTableClick(item,index){
console.log(item,index)
if(this.avtiveTdIndex === index){
this.avtiveTdIndex = -1;
}else{
this.avtiveTdIndex = index;
}
this.clickTrRowFn.emit(item)
}
// ---表格---
ngOnInit(): void {
}
ngAfterViewInit() {
this.scrollerFn()
}
ngOnDestroy(){
clearInterval(this.timer);
}
// 数值非空判断
isNullHandleFn(val){
if(val == undefined || val == "" || val == null || val == " "){
return "-";
}else{
var source = String(val).split('.');//按小数点分成2部分
source[0] = source[0].replace(new RegExp('(\\d)(?=(\\d{3})+$)', 'ig'), '$1,');//只将整数部分进行都好分割
return source.join('.');//再将小数部分合并进来
}
}
}
// ---表格组件-less
.tableContainer{
.right_table_div{
width: 100%;
min-height: 300px;
.table_title_div{
height: 50px;
width: 100%;
text-align: center;
display: inline-flex;
justify-content: space-between;
align-items: center;
background: url(theadBj.png) no-repeat;
background-size: 100% 100%;
font-size: 18px;
font-family: Alibaba PuHuiTi;
font-weight: 400;
color: #52DBFF;
line-height: 50px;
}
.table_items_div{
height: calc(100% - 50px);
width: 100%;
overflow-y: auto;
&::-webkit-scrollbar {
width: 4px;
}
&::-webkit-scrollbar-thumb {
border-radius: 10px;
background: RGBA(46, 174, 255, 1);
}
&::-webkit-scrollbar-track {
border-radius: 0;
background: rgba(0,0,0,0.1);
}
.table_item_div{
cursor: pointer;
background: url(tbodyBj-2.png) no-repeat;
background-size: 100% 100%;
width: 100%;
height: 56px;
display: inline-flex;
justify-content: space-between;
align-items: center;
text-align: center;
font-size: 18px;
font-family: Source Han Sans CN;
font-weight: 400;
color: #D2E2FF;
line-height: 56px;
}
}
.textOverflow{
overflow:hidden;
white-space: nowrap;
text-overflow: ellipsis;
-o-text-overflow:ellipsis;
}
// 高亮当前行
// .table-list-action{
// background: url(table_active.png) no-repeat!important;
// color: #FFFFFF!important;
// background-size: 99% 100%!important;
// }
}
}
// ---在用到的地方引用表格组件
<!-- 表格 -->
<table-scroll [scrollHeight]="600" [isSnumber]="true" [theadListData]="theadListData" [tableData]="tableData" (clickTrRowFn)="onClickTrRowFn($event)"></table-scroll>
tableData = [
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
{ID_UNITNAME:'11',ID_VALUE:'22',TB:'33'},
]
theadListData = [
{text:'名称',fieldName:'ID_UNITNAME',width:'25%',isOverflow:true,},
{text:'收益率(%)',fieldName:'ID_VALUE',width:'25%',isOverflow:false,},
{text:'同比(±)',fieldName:'TB',width:'25%',isOverflow:false,},
]
onClickTrRowFn(val){
console.log(val)
}
// ---效果图
![](https://img-blog.csdnimg.cn/direct/078c1095d1b0437c87f0608795e99440.png)