Ext JS 中的自动换行网格单元

发布于 2024-08-18 13:31:55 字数 908 浏览 3 评论 0原文

请随时回答!)

(这本身不是一个问题,我正在记录我使用 Ext JS 3.1.0 找到的解决方案。但是,如果您知道更好的解决方案, Ext JS Grid 对象没有允许自动换行文本的本机方法,但有一个 css 属性可以覆盖由 Ext JS Grid 对象创建的 TD 元素的内联 CSS网格。

不幸的是,TD 元素包含包装内容的 DIV 元素,并且 DIV 设置为 white-space:nowrap 由 Ext JS 的样式表定义,因此覆盖 TD CSS 没有任何好处。

我将以下内容添加到我的主 CSS 文件中,这是一个简单的修复,似乎不会破坏任何网格功能,但允许我应用于 TD 的任何空白设置通过直到DIV

.x-grid3-cell {
    /* TD is defaulted to word-wrap. Turn it off so
       it can be turned on for specific columns. */
    white-space:nowrap;
    }

.x-grid3-cell-inner {
    /* Inherit DIV's white-space from TD parent, since
       DIV's inline style is not accessible in the column
       definition. */
    white-space:inherit;
    }

YMMV,但它对我有用,想将其作为解决方案发布,因为我无法通过搜索互联网找到可行的解决方案。

(This is not a question per se, I'm documenting a solution I found using Ext JS 3.1.0. But, feel free to answer if you know of a better solution!)

The Column config for an Ext JS Grid object does not have a native way to allow word-wrapped text, but there is a css property to override the inline CSS of the TD elements created by the grid.

Unfortunately, the TD elements contain a DIV element wrapping the content, and that DIV is set to white-space:nowrap by Ext JS's stylesheet, so overriding the TD CSS does no good.

I added the following to my main CSS file, a simple fix that appears to not break any grid functionality, but allows any white-space setting I apply to the TD to pass through to the DIV.

.x-grid3-cell {
    /* TD is defaulted to word-wrap. Turn it off so
       it can be turned on for specific columns. */
    white-space:nowrap;
    }

.x-grid3-cell-inner {
    /* Inherit DIV's white-space from TD parent, since
       DIV's inline style is not accessible in the column
       definition. */
    white-space:inherit;
    }

YMMV, but it works for me, wanted to get it out there as a solution since I couldn't find a working solution by searching the Interwebs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

锦欢 2024-08-25 13:31:55

如果您只想将换行应用到一列,则可以添加自定义渲染器。

以下是要添加的函数:

function columnWrap(val){
    return '<div style="white-space:normal !important;">'+ val +'</div>';
}

然后将 renderer: columnWrap 添加到要换行的每个列

new Ext.grid.GridPanel({
[...],
columns:[{   
     id: 'someID',
     header: "someHeader",
     dataIndex: 'someID',
     hidden: false,
     sortable: true,
     renderer: columnWrap           
}]

If you only want to apply the wrapping to one column, you can add a custom renderer.

Here is the function to add:

function columnWrap(val){
    return '<div style="white-space:normal !important;">'+ val +'</div>';
}

Then add the renderer: columnWrap to each column you want to wrap

new Ext.grid.GridPanel({
[...],
columns:[{   
     id: 'someID',
     header: "someHeader",
     dataIndex: 'someID',
     hidden: false,
     sortable: true,
     renderer: columnWrap           
}]
孤单情人 2024-08-25 13:31:55

不是“更好的解决方案”,而是类似的解决方案。我最近需要允许每个网格中的所有单元格都换行。我使用了类似的基于 CSS 的修复(这是针对 Ext JS 2.2.1):

.x-grid3-cell-inner, .x-grid3-hd-inner {
  white-space: normal; /* changed from nowrap */
}

我没有费心在 td 上设置样式,我只是直接选择单元类。

Not a "better solution", but a similar one. I recently needed to allow ALL cells in every grid to wrap. I used a similar CSS-based fix (this was for Ext JS 2.2.1):

.x-grid3-cell-inner, .x-grid3-hd-inner {
  white-space: normal; /* changed from nowrap */
}

I didn't bother with setting a style on the td, I just went right for the cell class.

黎夕旧梦 2024-08-25 13:31:55

如果您只想在某些列中换行文本并使用 ExtJS 4,则可以为列中的单元格指定 CSS 类:

{
    text: 'My Column',
    dataIndex: 'data',
    tdCls: 'wrap'
}

并在 CSS 文件中:

.wrap .x-grid-cell-inner {
    white-space: normal;
}

If you only want to wrap text in certain columns and are using ExtJS 4, you can specify a CSS class for the cells in a column:

{
    text: 'My Column',
    dataIndex: 'data',
    tdCls: 'wrap'
}

And in your CSS file:

.wrap .x-grid-cell-inner {
    white-space: normal;
}
稀香 2024-08-25 13:31:55

其他解决方案是:

columns : [
    {
        header: 'Header',
        dataIndex : 'text',
        renderer: function(value, metaData, record, rowIndex, colIndex, view) {
            metaData.style = "white-space: normal;";
            return value;
        }
    }
]

Other solution is that:

columns : [
    {
        header: 'Header',
        dataIndex : 'text',
        renderer: function(value, metaData, record, rowIndex, colIndex, view) {
            metaData.style = "white-space: normal;";
            return value;
        }
    }
]
神也荒唐 2024-08-25 13:31:55

最好的方法是将 cellWrap 设置为 true,如下所示。

cellWrap: true

它在 EXTJS 5.0 中运行良好。

The best way to do is by setting the cellWrap to true as below.

cellWrap: true

Its working well in EXTJS 5.0.

抚你发端 2024-08-25 13:31:55

如果您仍然想使用

cellWrap: true

css,请始终尝试在主题内使用 ui、变量等,或使用 style 属性设置样式。

use

cellWrap: true

If you still want to use css always try to work with ui's, variables, etc. within themes, or set the style with the style property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文