angular 5中使用table时,tbody内容多时,出现纵向滚动条,thead不随内容滚动而滚动...

主要解决方法是:

1、table的布局保持不变,table外部包裹着一个div,用于设置height和overflow-y:auto

2、使用相对运动保持静止的视觉效果,具体实现是滚轮滚动的距离就是thead在纵向偏移的距离(使用transform: translateY),通过js动态控制

code如下:

1.HTML

<div class="p-l-20 p-r-20 mt-20" id="table-container" style="height: calc(100% - 67px);overflow-y: auto;">
   
        <table class="table">
            <thead>
                <tr>
                    <th nowrap="nowrap" class="left" *ngFor="let head of tableValObj.header">{{head | nullVoid}}</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let tr of tableValObj['tbody']">
                    <td nowrap="nowrap" class="left" *ngFor="let td of tr;let i = index">
                        <ng-container *ngIf="!td[tableValObj['header'][i]].modifiable && (!td[tableValObj['header'][i]].dataValidation.type || td[tableValObj['header'][i]].dataValidation.type == 1)">
                            {{td[tableValObj['header'][i]].value}}
                        </ng-container>
                        <ng-container *ngIf="td[tableValObj['header'][i]].dataValidation.type == 3">
                            <div class="select">
                                <select [(ngModel)]="td[tableValObj['header'][i]].value">
                                    <option *ngFor="let option of td[tableValObj['header'][i]].dataValidation.data[0]">{{option}}</option>
                                </select>
                            </div>
                        </ng-container>
                        <ng-container *ngIf="td[tableValObj['header'][i]].modifiable && (!td[tableValObj['header'][i]].dataValidation.type || td[tableValObj['header'][i]].dataValidation.type == 1)">
                            <input style="width: 50%" type="text" [(ngModel)]="td[tableValObj['header'][i]].value">
                        </ng-container>
                    </td>
                </tr>
            </tbody>
        </table>

    </div>

2.ts

import {  ElementRef } from '@angular/core';

constructor( private elm: ElementRef) { }

ngAfterViewInit() {
    let tableCount = this.elm.nativeElement.querySelector('#table-container');
    tableCount.addEventListener('scroll', () => {
      let scrollTop = tableCount.scrollTop;
      tableCount.querySelector('thead').style.transform = 'translateY(' + scrollTop + 'px)';
    })
  }

 

转载于:https://www.cnblogs.com/zhaoljblog/articles/9233149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值