列标题中的文本能否在 Vaadin 14 的网格中换行到多行? [英] Can the text in a column header wrap to multiple lines in a Grid in Vaadin 14?
问题描述
在上图中,我创建了一个 Grid
Vaadin 14. 注意列标题没有环绕的红线.
In above image, I have a Grid
created in Vaadin 14. Notice the red lines where the column titles were not wrapping.
我试图弄清楚是否有可能(并且安全/容易/标准)让列标签换行(例如,就像在 Excel 中那样).
I'm trying to figure out if it's possible (and safe/easy/standard) to get the column labels to wrap (like they would in Excel, for example).
我和我的同事都无法弄清楚如何做到这一点.
Neither I nor my colleagues could figure out how to do so.
推荐答案
您需要按照以下代码段调整 Vaadin Grid 样式:
You will need to adapt the Vaadin Grid styles as in the following snippet:
/* wrap text in grid column headers */
[part~="header-cell"] {
white-space: normal;
}
通过 @CssImport
注释包含 CSS,主题化网格:
Include the CSS via @CssImport
annotation, theming the grid:
@CssImport(value = "./styles/grid-styles.css", themeFor = "vaadin-grid")
public class YourViewOrLayout extends Composite<Div> {
...
}
主要混淆 Web 组件样式的原因是 shadow DOM 概念.每个 Web 组件都有自己的样式范围.这意味着 shadow DOM 中的所有 HTML 标签都必须通过 shadow DOM 中的 标签来设置样式.Web 组件(此处为 Vaadin Grid)带有样式,但您可以使用上述
@CssImport
注释附加自己的样式.尝试使用浏览器的开发工具(IE/Edge 除外)检查
标记的 HTML 结构,您会注意到 shadow DOM.但是,位于 web 组件下的一些标签将不是 shadow DOM 的一部分.这些由
引用,并且全局样式将应用于它们.对于 Vaadin Grid,单元格内容是开槽的.
The main confusion styling web components arises with the shadow DOM concept. Each web component has its own style scope. That means all HTML tags within the shadow DOM must be styled via a <style>
tag within the shadow DOM. The web component (Vaadin Grid here) comes with styles but you can append your own styles using the @CssImport
annotation as above. Try inspecting the HTML structure of a <vaadin-grid>
tag with the developer tools of your browser (except IE/Edge) and you will notice the shadow DOM. However, some tags located under the web component will not be part of the shadow DOM. These are referenced by <slot>
s and global styles will be applied to them. For Vaadin Grid, the cell contents are slotted.
注意:以前版本的 Vaadin Flow 包含自定义 Web 组件样式的方式略有不同.请参阅文档.
Note: Previous versions of Vaadin Flow had a slightly different way to include custom web component styles. See documentation.
这篇关于列标题中的文本能否在 Vaadin 14 的网格中换行到多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!