我在数据表格中的固定表头样式方面遇到了一些小问题。我编写了一个简单的Angular组件和特定的指令:
sticky.directive.ts
@Directive({
selector: '[sticky]'
})
export class StickyDirective {
constructor(private _element: ElementRef, private _window: WindowRef) {
console.log('debug')
}
@HostListener('window:scroll', ['$event'])
handleScrollEvent(e) {
if (this._window.nativeWindow.pageYOffset > 100) {
this._element.nativeElement.classList.add('stick');
} else {
this._element.nativeElement.classList.remove('stick');
}
}
}
该指令的目的是在用户滚动到标题下方时添加一个类 stick。因此,即使用户滚动了长表格,表头也应对用户可见。stick 类看起来像这样:
.stick {
position: fixed;
top: 55px;
}
以下是我的some.component.html的部分内容(在thead元素上使用指令):
<table class=" table table-bordered ">
<thead sticky>
<tr>
<th width="40%">Name
</th>
<th width="10%">Priority
</th>
<th width="25%">Date created
</th>
<th width="25%">Date modified
</th> </tr> </thead> <tbody> <tr *ngFor="let r of entitiesFiltered">
<td>
<div class="table-cell-flex">
<div class="cell-content">
{{r.name}}
</div>
</div>
</td>
<td>
<div class="table-cell-flex">
<div class="cell-content">
{{r.priority}}
</div>
</div>
</td>
...
我的代码实现了基本功能。这意味着当页面滚动时,标题仍保持在原位,但标题和列的宽度会发生变化。效果如下:
问题:
有人能告诉我该如何设置表格样式,使得固定的标题不会改变表格的形状吗?是否可能实现?
width
和/或left
和right
值。你能否为我们提供链接或重新创建该页面,或告诉我主表格如何居中? - Michael Coker