---
title: "ExtJS Grid 可以选择文字方法"
author: "youanyyou"
date: "2010-10-27T10:31:29Z"
tags: []
categories: ["Web前端"]
slug: "extjs-grid"
draft: false
lastmod: "2010-10-27T10:31:29Z"
id: 795261
url: "https://itlol.iteye.com/blog/795261"
---

**How to select text in the grid (with the mouse) so that it can be copied to the clipboard**

 

1.相关帖子:[http://extjs.com/forum/showthread.php?p=154426#post154426](http://extjs.com/forum/showthread.php?p=154426#post154426)

 

2.下面是[Condor](http://extjs.com/forum/member.php?u=343) 使用的方法:

- 首先添加CSS:

Html代码 [![复制代码](http://atian25.iteye.com/images/icon_copy.gif)](http://atian25.iteye.com/blog/428946 "复制代码")

1. **&lt;style** type="text/css"**&gt;**
2. .x-selectable, .x-selectable * {
3. -moz-user-select: text!important;
4. -khtml-user-select: text!important;
5. }
6. **&lt;/style&gt;**

```
<style type="text/css">
	.x-selectable, .x-selectable * {
		-moz-user-select: text!important;
		-khtml-user-select: text!important;
	}
</style>
```

- 然后在grid的配置中使用该css

 

Js代码 [![复制代码](http://atian25.iteye.com/images/icon_copy.gif)](http://atian25.iteye.com/blog/428946 "复制代码")

01. **var** grid = **new** Ext.grid.GridPanel({
02. viewConfig: {
03. templates: {
04. cell: **new** Ext.Template(
05. '&lt;td **class**="x-grid3-col x-grid3-cell x-grid3-td-{id} &lt;SPAN style="COLOR: #808000"&gt;x-selectable&lt;/SPAN&gt;
06. {css}" style="{style}"                         tabIndex="0" {cellAttr}&gt;',
07. '&lt;div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}&gt;{value}&lt;/div&gt;',
08. '&lt;/td&gt;'
09. )
10. }
11. },
12. ...
13. });

```
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>'
         )
      }
   },
   ...
});
```

 

3.如果你想把它设置为GRID的默认属性,可以使用以下代码:

Js代码 [![复制代码](http://atian25.iteye.com/images/icon_copy.gif)](http://atian25.iteye.com/blog/428946 "复制代码")

1. **if** (!Ext.grid.GridView.prototype.templates) {
2. Ext.grid.GridView.prototype.templates = {};
3. }
4. Ext.grid.GridView.prototype.templates.cell = **new** Ext.Template(
5. '&lt;td **class**="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}"
6. style="{style}" tabIndex="0" {cellAttr}&gt;',
7. '&lt;div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}&gt;{value}&lt;/div&gt;',
8. '&lt;/td&gt;'
9. );

```
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>'
);
```

 译者注:紧跟ext-all.js的后面

 

4.如果你也想让表头文字可选,则可以相应的修改hcell模板;

5.如果你使用的是分组Grid,则需要把以上的模板定义放入到GroupingView的配置中去,而不是上文的viewConfig

 

 

译注:

 

1.看下源码:

Js代码 [![复制代码](http://atian25.iteye.com/images/icon_copy.gif)](http://atian25.iteye.com/blog/428946 "复制代码")

1. //GridView 288行
2. **if**(!ts.cell){
3. ts.cell = **new** Ext.Template(
4. '&lt;td class="x-grid3-col x-grid3-cell x-grid3-td-{id} {css}" style="{style}" tabIndex="0" {cellAttr}&gt;',
5. '&lt;div **class**="x-grid3-cell-inner x-grid3-col-{id}" &lt;SPAN style="COLOR: #808000"&gt;unselectable="on"&lt;/SPAN&gt;
6. {attr}&gt;{value}&lt;/div&gt;',
7. '&lt;/td&gt;'
8. );
9. }

```
//GridView 288行
if(!ts.cell){
  ts.cell = new Ext.Template(
    '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} {css}" style="{style}" tabIndex="0" {cellAttr}>',
      '<div class="x-grid3-cell-inner x-grid3-col-{id}" unselectable="on"
 {attr}>{value}</div>',
    '</td>'
  );
}
```

 可知,以上的修改就是把unselectable去掉,并且在td的class里面多加了我们的x-selectable

 

 

2.进一步的,如果需要只针对某些列可选:

    2.1在该列的renderer(value,meta)里面,添加一句meta.selectable=true

    2.2再编辑下cell的模板,在里面判断{selectable?'someCssClass':''}

 

[深圳人才网](http://www.szrcwz.com/) [深圳招聘网](http://www.szrcwz.com/) [深圳人才招聘网](http://www.szrcwz.com/) [深圳人才大市场](http://www.szrcwz.com/) 

企业、个人免费注册，获取想要的 深圳 软件工程师招聘信息 月薪最低3000-8000，更有高端猎头职位！ 

www.szrcwz.com                                                                                                                                     [](http://www.google.com/url?ct=abg&q=https%3A%2F%2Fwww.google.com%2Fadsense%2Fsupport%2Fbin%2Frequest.py%3Fcontact%3Dabg_afc%26url%3Dhttp%3A%2F%2Fyouanyyou.iteye.com%2Fblog%2F772874%26hl%3Dzh-CN%26client%3Dca-pub-4914818119943102%26adU%3Dwww.TFMChina.com%26adT%3D%25E4%25B8%2580%25E5%25A4%25A9%25E8%25A7%258110%25E4%25B8%25AASeo%25E4%25BC%2598%25E5%258C%2596%25E5%25A4%25A7%25E5%25B8%2588%26adU%3Dwww.gxb138.com%26adT%3D%25E6%259C%2589%25E6%2595%2588%25E9%2599%258D%25E4%25BD%258E%25E8%25A1%2580%25E7%25B3%2596%2B%25E5%2591%258A%25E5%2588%25AB%25E7%25B3%2596%25E5%25B0%25BF%25E7%2597%2585%25E8%25B6%25B3%26adU%3Dwww.seo1680.com%26adT%3D%25E6%25B5%2581%25E9%2587%258F%25E6%259A%25B4%25E6%25B6%25A8seo%25E8%25BD%25AF%25E4%25BB%25B6--%25E6%258F%2590%25E5%258D%2587%25E6%258E%2592%25E5%2590%258D%26gl%3DCN&usg=AFQjCNHgQa8JEDhLJLmlCMlxSIb_9KgJnQ)      

                  ![Google 提供的广告](http://static.googleadsserving.cn/pagead/abglogo/abg-zh-CN-100c-000000.png)