extjs表格文本启用选择复制功能具体实现
更新时间:2013年10月11日 17:54:27 作者:
extjs提供了方便的表格组件grid供使用,但是默认情况下表格中的文本是不能被选中的,自然也是无法复制的,下面就为大家介绍下选择复制功能如何启用,感兴趣的朋友可以了解下
extjs提供了方便的表格组件grid供使用,但是默认情况下表格中的文本是不能被选中的,自然也是无法复制的。
而选择复制文本的需要也是很平常的,于是我们就需要自己动手来解决这个问题,实现extjs的grid文本选择复制功能。
说明一点,文中所列出的代码片断都是在当前ext 4.0.2a版本下的,其它版本未做测试,请自行斟酌。
首先自定义一下样式,来覆盖默认的css样式:
<style type="text/css">
.x-selectable, .x-selectable * {
-moz-user-select: text!important;
-khtml-user-select: text!important;
}
</style>
复写extjs的table类,阻止鼠标选择文本的就是这个unselectable
/**
* override the table class
*/
Ext.override(Ext.view.Table, {
afterRender : function() {
var me = this;
me.callParent();
me.mon(me.el, {
scroll : me.fireBodyScroll,
scope : me
});
if (!me.featuresMC && (me.featuresMC.findIndex('ftype', 'unselectable') >= 0)) {
me.el.unselectable();
}
me.attachEventsForFeatures();
}
});
然后再自定义一个feature,启用文本选择功能,通过替换取消unselectable样式,同时增加x-selectable样式
/**
* define the select feature
*/
Ext.define('Myext.grid.SelectFeature', {
extend : 'Ext.grid.feature.Feature',
alias : 'feature.selectable',
mutateMetaRowTpl : function(metaRowTpl) {
var i, ln = metaRowTpl.length;
for (i = 0; i < ln; i++) {
tpl = metaRowTpl[i];
tpl = tpl.replace(/x-grid-row/, 'x-grid-row x-selectable');
tpl = tpl.replace(/x-grid-cell-inner x-unselectable/g, 'x-grid-cell-inner');
tpl = tpl.replace(/unselectable="on"/g, '');
metaRowTpl[i] = tpl;
};
}
});
现在可以声明一个selectFeature了
var selectFeature = Ext.create('Myext.grid.SelectFeature');
需要启用文本选择的表格,在创建时添加这个feature就可以了
Ext.create('Ext.grid.Panel', {
title : 'grid example',
store : gridStore, // define before
width : 600,
height : 300,
features : [selectFeature],
columns : [{
text:'name',
dataIndex:'name'
}]
// other code
}
而选择复制文本的需要也是很平常的,于是我们就需要自己动手来解决这个问题,实现extjs的grid文本选择复制功能。
说明一点,文中所列出的代码片断都是在当前ext 4.0.2a版本下的,其它版本未做测试,请自行斟酌。
首先自定义一下样式,来覆盖默认的css样式:
复制代码 代码如下:
<style type="text/css">
.x-selectable, .x-selectable * {
-moz-user-select: text!important;
-khtml-user-select: text!important;
}
</style>
复写extjs的table类,阻止鼠标选择文本的就是这个unselectable
复制代码 代码如下:
/**
* override the table class
*/
Ext.override(Ext.view.Table, {
afterRender : function() {
var me = this;
me.callParent();
me.mon(me.el, {
scroll : me.fireBodyScroll,
scope : me
});
if (!me.featuresMC && (me.featuresMC.findIndex('ftype', 'unselectable') >= 0)) {
me.el.unselectable();
}
me.attachEventsForFeatures();
}
});
然后再自定义一个feature,启用文本选择功能,通过替换取消unselectable样式,同时增加x-selectable样式
复制代码 代码如下:
/**
* define the select feature
*/
Ext.define('Myext.grid.SelectFeature', {
extend : 'Ext.grid.feature.Feature',
alias : 'feature.selectable',
mutateMetaRowTpl : function(metaRowTpl) {
var i, ln = metaRowTpl.length;
for (i = 0; i < ln; i++) {
tpl = metaRowTpl[i];
tpl = tpl.replace(/x-grid-row/, 'x-grid-row x-selectable');
tpl = tpl.replace(/x-grid-cell-inner x-unselectable/g, 'x-grid-cell-inner');
tpl = tpl.replace(/unselectable="on"/g, '');
metaRowTpl[i] = tpl;
};
}
});
现在可以声明一个selectFeature了
var selectFeature = Ext.create('Myext.grid.SelectFeature');
需要启用文本选择的表格,在创建时添加这个feature就可以了
复制代码 代码如下:
Ext.create('Ext.grid.Panel', {
title : 'grid example',
store : gridStore, // define before
width : 600,
height : 300,
features : [selectFeature],
columns : [{
text:'name',
dataIndex:'name'
}]
// other code
}
相关文章
Extjs中ComboBoxTree实现的下拉框树效果(自写)
最近涉及到的一个项目中,需要实现ComboBoxTree的效果,由于在Extjs中是没有这种效果,所以看看别人的资料自己写了一个,感兴趣的朋友可以参考下哈2013-05-05
Extjs4 消息框去掉关闭按钮(类似Ext.Msg.alert)
类似Ext.Msg.alert();但没有关闭按钮,由于Extjs4消息框中的关闭按钮,没有执行回调函数,点击关闭按钮后,直接关闭窗口,接下来为大家详细介绍下去掉关闭按钮2013-04-04
EXTjs4.0的store的findRecord的BUG演示代码
EXTjs4.0 的store的findRecord的BUG:当判断ID=1的时候,遇到1开头的ID的时候,这个时候就判断出问题了,有类似问题的朋友可以了解下哈2013-06-06
extjs 列表框(multiselect)的动态添加列表项的方法
最近公司一个项目,因为要使用div模拟的窗口,因为久闻extjs的大名,因此就想在项目中使用一下.首先下载了multiselect的extjs3.0 demo.看到的代码这里我就不粘贴了.2009-07-07
Extjs中的GridPanel隐藏列会显示在menuDisabled中解决方法
在Extjs中的GridPanel会有这样的情况,隐藏列会显示在menuDisabled中,但是这个一般没有什么用处,只是用于后台取值的作用,感兴趣的朋友可以了解下啊,希望本文对你有所帮助2013-01-01
ExtJs 学习笔记 Ext.Panle Ext.TabPanel Ext.Viewport
ExtJs 学习笔记基础篇 面板的使用(Ext.Panle、Ext.TabPanel、Ext.Viewport)2008-12-12


最新评论