最近,我收到一个请求,要求在我的Blazor Server项目中的一个DataGrid中添加Sticky Header功能。我正在使用Blazorise,没有看到任何粘性标题的现有功能,并且在网上找到了有限的信息,所以我想我应该记录我的解决方案。
发布于 2020-08-04 00:18:49
在我的回答之前,我将首先说明这个解决方案是为chrome构建的,可能需要针对不同的浏览器进行调整。
首先,我在site.css文件中添加了一个粘性标题类:
.sticky-header {
position: sticky;
top: 0;
z-index: 10;
}
.sticky-header::before {
content: '';
width: 100%;
height: 1px;
position: absolute;
top: -1px;
left: 0;
background-color: #fcfcfc;
z-index: -1;
}
.sticky-header + .sticky-header::after {
content: '';
width: 1px;
height: 100%;
position: absolute;
top: 0;
left: -1px;
background-color: #fcfcfc;
z-index: -1;
}关于css的一些注意事项。将top设置为0可将标题设置为屏幕顶部,而z-index只需设置为比周围元素更高的索引,这样它就会位于其他元素的前面。
此外,我注意到position: sticky去掉了我的边框,因此我使用了::before和::after伪类来帮助充当内部边框,但这部分代码并不是粘性标头工作所必需的。
一旦你有了上面的css,你会想要把它添加到你的DataGrid中。对于您在DataGridColumns部分中定义的每个DataGridColumn,您需要添加以下代码:
<DataGrid>
<DataGridColumns>
<DataGridColumn HeaderCellClass="sticky-header" TItem="TEntity" Field="@nameof(TEntity.SomeProperty)" Caption="Some Caption" />
</DataGridColumns>
</DataGrid>https://stackoverflow.com/questions/63233158
复制相似问题