2011-09-01 69 views
25

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

我已经试过此网格配置:

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中的排模板如下,但我不知道如何在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>' 
    ) 
} 

任何su ggestions非常感谢。

回答

4

您可以添加像这样,通过渲染器列

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>') 
} 
+0

真棒,谢谢选择。 虽然我怀疑,这是一个迹象,我试图使用网格的东西,它不是为了 - 我使用纯粹的网格显示目的,并没有使用它的任何编辑功能。 我最终只是使用了一个带有XTemplate的面板,该面板渲染出了一张平面简洁的表格。 – keeny

+0

@keeny我知道这是非常古老的,但我经常使用网格显示功能。他们非常适合排序和风格 – WattsInABox

0

另一种方式做到这一点可能是在使用新templatecolumn到类分配给任意的HTML元素。以下是我刚写入的同时将应用程序移植到extjs4的列定义中的单个列。

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

您可以添加enableTextSelection:忠于你viewConfig或全局应用的变化与此每格:

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

我相信“enableTextSelection”仅在4.1.0以后才可用。真的不知道情况是否如此,但如果是这样,这是一条路。 –

1

我只是想增加Triquis回答: 随着ExtJS的4.1.0在Ext.onReady()函数的早期或在您的应用程序的早期某处

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

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

将此代码:您可以启用treepanels选择为好。

(对不起,我不能评论张贴到Triqui的答案,因为我没有必要的声誉呢。)

0

对于旧版本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>' 
     ) 
     } 
    }, 
}); 
17

结合几个这些答案中最Exty方式....创建网格时,网格的视图设置enableTextSelection为true。

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

您可以启用与这些代码几行网格视图, 它的工作原理相当不错,在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>' 
);