使 ExtJS 4 网格内容可选择

发布于 2024-12-03 02:41:59 字数 1246 浏览 1 评论 0原文

ExtJS 4 (Sencha) 中的网格默认不允许选择内容。但我想让这成为可能。

我已经尝试过这个网格配置:

viewConfig: {
    disableSelection: true,
    stripeRows: false,
    getRowClass: function(record, rowIndex, rowParams, store){
        return "x-selectable";
    }
},

使用这些 css 类(基本上针对我在 Chrome 中看到的每个元素):

/* allow grid text selection in Firefox and WebKit based browsers */

.x-selectable,
.x-selectable * {
                -moz-user-select: text !important;
                -khtml-user-select: text !important;
    -webkit-user-select: text !important;
}

.x-grid-row td,
.x-grid-summary-row td,
.x-grid-cell-text,
.x-grid-hd-text,
.x-grid-hd,
.x-grid-row,

.x-grid-row,
.x-grid-cell,
.x-unselectable
{
    -moz-user-select: text !important;
    -khtml-user-select: text !important;
    -webkit-user-select: text !important;
}

我知道您可以覆盖 Ext 3 中的网格行模板,如下所示,但我不知道如何执行分机 4 中相同:

templates: {
    cell: new Ext.Template(
    '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
           '<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
    '</td>'
    )
}

非常感谢任何建议。

Grids in ExtJS 4 (Sencha) don’t allow to select content by default. But I want to make this possible.

I've tried this grid config:

viewConfig: {
    disableSelection: true,
    stripeRows: false,
    getRowClass: function(record, rowIndex, rowParams, store){
        return "x-selectable";
    }
},

with these css classes (basically targeting every element I can see in Chrome):

/* allow grid text selection in Firefox and WebKit based browsers */

.x-selectable,
.x-selectable * {
                -moz-user-select: text !important;
                -khtml-user-select: text !important;
    -webkit-user-select: text !important;
}

.x-grid-row td,
.x-grid-summary-row td,
.x-grid-cell-text,
.x-grid-hd-text,
.x-grid-hd,
.x-grid-row,

.x-grid-row,
.x-grid-cell,
.x-unselectable
{
    -moz-user-select: text !important;
    -khtml-user-select: text !important;
    -webkit-user-select: text !important;
}

I know that you can override the grid row template in Ext 3 as below, but I don't know how to do the same in Ext 4:

templates: {
    cell: new Ext.Template(
    '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
           '<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
    '</td>'
    )
}

Any suggestions much appreciated.

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

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

发布评论

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

评论(8

毁梦 2024-12-10 02:41:59

您可以将enableTextSelection: true添加到您的viewConfig中,或者使用以下命令对每个网格全局应用更改:

Ext.override(Ext.grid.View, { enableTextSelection: true });

You can add enableTextSelection: true to your viewConfig or apply the change globally for every grid with this:

Ext.override(Ext.grid.View, { enableTextSelection: true });
童话 2024-12-10 02:41:59

以最 Exty 的方式组合其中几个答案...创建网格时,在网格视图中将enableTextSelection 设置为 true。

var grid = new Ext.grid.GridPanel({
   viewConfig: {
      enableTextSelection: true
   },
});

Combining several of these answers in the most Exty way.... Set enableTextSelection to true in the grid's view when creating the grid.

var grid = new Ext.grid.GridPanel({
   viewConfig: {
      enableTextSelection: true
   },
});
暮光沉寂 2024-12-10 02:41:59

您可以像这样添加它,使用列的渲染器

columns: [
    {
        header: "",
        dataIndex: "id",
        renderer: function (value, metaData, record, rowIndex, colIndex, store) {
            return this.self.tpl.applyTemplate(record.data);
        },
        flex: 1
    }
],
statics: {
    tpl: new Ext.XTemplate(
        '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
            '<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
        '</td>')
}

You can add it like this, using renderer for the column

columns: [
    {
        header: "",
        dataIndex: "id",
        renderer: function (value, metaData, record, rowIndex, colIndex, store) {
            return this.self.tpl.applyTemplate(record.data);
        },
        flex: 1
    }
],
statics: {
    tpl: new Ext.XTemplate(
        '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
            '<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
        '</td>')
}
毁梦 2024-12-10 02:41:59

我只是想补充一下 Triquis 的答案:
使用 ExtJS 4.1.0,您还可以启用树面板的选择:

Ext.override(Ext.view.Table, { enableTextSelection: true }); // Treepanels

Ext.override(Ext.grid.View,  { enableTextSelection: true }); // Grids

将此代码放在 Ext.onReady() 函数早期或应用程序早期的某个位置。

(抱歉,我无法对 Triqui 的答案发表评论,因为我还没有所需的声誉。)

I just would like to add to Triquis answer:
With ExtJS 4.1.0 you can enable the selection for treepanels as well:

Ext.override(Ext.view.Table, { enableTextSelection: true }); // Treepanels

Ext.override(Ext.grid.View,  { enableTextSelection: true }); // Grids

Place this code somewhere early in your Ext.onReady() function or early in your application.

(Sorry, I am not able to post a comment to the answer of Triqui as I don't have the required reputation yet.)

你对谁都笑 2024-12-10 02:41:59

另一种方法可能是在使用新的 templatecolumn 时将类分配给任意 html 元素。这是我刚刚在将应用程序移植到 extjs4 时编写的列定义中的单个列。

{   
    text: "Notes",
    dataIndex: 'notes',
    width: 300,    
    xtype: 'templatecolumn',  
    tpl: [
    '<span class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}"',
    'style="{style}" tabIndex="0" {cellAttr}>',
    '<span class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{notes}',
    '</span></span>' ],
    editor: {xtype:'textfield'} 
}

An alternate way to do it might be to assign the classes to arbitrary html elements while using the new templatecolumn. Here's a single column in a column definition I just wrote while porting an app to extjs4.

{   
    text: "Notes",
    dataIndex: 'notes',
    width: 300,    
    xtype: 'templatecolumn',  
    tpl: [
    '<span class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}"',
    'style="{style}" tabIndex="0" {cellAttr}>',
    '<span class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{notes}',
    '</span></span>' ],
    editor: {xtype:'textfield'} 
}
柏林苍穹下 2024-12-10 02:41:59

对于旧版本的 EXTJS,不支持启用文本选择。以下将通过向网格单元分配新模板来工作。这适用于 EXTJS 3.4.0

   var grid = new Ext.grid.GridPanel({
   viewConfig: {
      templates: {
         cell: new Ext.Template(
            '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id}\
                        x-selectable {css}" style="{style}"\
                        tabIndex="0" {cellAttr}>',\
            '<div class="x-grid3-cell-inner x-grid3-col-{id}"\
                        {attr}>{value}</div>',\
            '</td>'
         )
      }
   },
});

For older version of EXTJS that doesnot support enable text selection. Following will work by assigning a new template to the grid cell. This works for EXTJS 3.4.0

   var grid = new Ext.grid.GridPanel({
   viewConfig: {
      templates: {
         cell: new Ext.Template(
            '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id}\
                        x-selectable {css}" style="{style}"\
                        tabIndex="0" {cellAttr}>',\
            '<div class="x-grid3-cell-inner x-grid3-col-{id}"\
                        {attr}>{value}</div>',\
            '</td>'
         )
      }
   },
});
披肩女神 2024-12-10 02:41:59

您可以使用这几行代码启用网格视图的选择,
它在 EXTJS 3.4 中运行良好,具有 IE 11 和兼容模式

   if (!Ext.grid.GridView.prototype.templates) {
Ext.grid.GridView.prototype.templates = {};
}
Ext.grid.GridView.prototype.templates.cell = new Ext.Template(
'<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
'<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
'</td>'
);

you can enable the selection for grid view with those few lines of code ,
it works very well in EXTJS 3.4 , with IE 11 and compatibility mode

   if (!Ext.grid.GridView.prototype.templates) {
Ext.grid.GridView.prototype.templates = {};
}
Ext.grid.GridView.prototype.templates.cell = new Ext.Template(
'<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
'<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
'</td>'
);
咿呀咿呀哟 2024-12-10 02:41:59

值 true 不起作用,请按以下方式使用:

userSelectable: {
body元素:'文本'
},

Value true is not working, Please use as following :

userSelectable: {
bodyElement: 'text'
},

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