extjs表格文本啟用選擇復(fù)制功能具體實現(xiàn)
更新時間:2013年10月11日 17:54:27 作者:
extjs提供了方便的表格組件grid供使用,但是默認(rèn)情況下表格中的文本是不能被選中的,自然也是無法復(fù)制的,下面就為大家介紹下選擇復(fù)制功能如何啟用,感興趣的朋友可以了解下
extjs提供了方便的表格組件grid供使用,但是默認(rèn)情況下表格中的文本是不能被選中的,自然也是無法復(fù)制的。
而選擇復(fù)制文本的需要也是很平常的,于是我們就需要自己動手來解決這個問題,實現(xiàn)extjs的grid文本選擇復(fù)制功能。
說明一點,文中所列出的代碼片斷都是在當(dāng)前ext 4.0.2a版本下的,其它版本未做測試,請自行斟酌。
首先自定義一下樣式,來覆蓋默認(rèn)的css樣式:
<style type="text/css">
.x-selectable, .x-selectable * {
-moz-user-select: text!important;
-khtml-user-select: text!important;
}
</style>
復(fù)寫extjs的table類,阻止鼠標(biāo)選擇文本的就是這個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;
};
}
});
現(xiàn)在可以聲明一個selectFeature了
var selectFeature = Ext.create('Myext.grid.SelectFeature');
需要啟用文本選擇的表格,在創(chuàng)建時添加這個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
}
而選擇復(fù)制文本的需要也是很平常的,于是我們就需要自己動手來解決這個問題,實現(xiàn)extjs的grid文本選擇復(fù)制功能。
說明一點,文中所列出的代碼片斷都是在當(dāng)前ext 4.0.2a版本下的,其它版本未做測試,請自行斟酌。
首先自定義一下樣式,來覆蓋默認(rèn)的css樣式:
復(fù)制代碼 代碼如下:
<style type="text/css">
.x-selectable, .x-selectable * {
-moz-user-select: text!important;
-khtml-user-select: text!important;
}
</style>
復(fù)寫extjs的table類,阻止鼠標(biāo)選擇文本的就是這個unselectable
復(fù)制代碼 代碼如下:
/**
* 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樣式
復(fù)制代碼 代碼如下:
/**
* 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;
};
}
});
現(xiàn)在可以聲明一個selectFeature了
var selectFeature = Ext.create('Myext.grid.SelectFeature');
需要啟用文本選擇的表格,在創(chuàng)建時添加這個feature就可以了
復(fù)制代碼 代碼如下:
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
}
相關(guān)文章
Extjs中ComboBoxTree實現(xiàn)的下拉框樹效果(自寫)
最近涉及到的一個項目中,需要實現(xiàn)ComboBoxTree的效果,由于在Extjs中是沒有這種效果,所以看看別人的資料自己寫了一個,感興趣的朋友可以參考下哈2013-05-05
ExtJs默認(rèn)的字體大小改變的幾種方法(自己整理)
本文列出網(wǎng)上收集的幾種方法,希望對大家有用,并且做了下瀏覽器兼容,感興趣的朋友可以參考下哈2013-04-04
extjs ColumnChart設(shè)置不同的顏色實現(xiàn)代碼
extjs為ColumnChart設(shè)置不同的顏色想必有很多朋友還是比較陌生的吧,接下來為大家詳細(xì)介紹下具體設(shè)置代碼,感興趣的朋友可以參考下哈2013-05-05
Extjs4 消息框去掉關(guān)閉按鈕(類似Ext.Msg.alert)
類似Ext.Msg.alert();但沒有關(guān)閉按鈕,由于Extjs4消息框中的關(guān)閉按鈕,沒有執(zhí)行回調(diào)函數(shù),點擊關(guān)閉按鈕后,直接關(guān)閉窗口,接下來為大家詳細(xì)介紹下去掉關(guān)閉按鈕2013-04-04
EXTjs4.0的store的findRecord的BUG演示代碼
EXTjs4.0 的store的findRecord的BUG:當(dāng)判斷ID=1的時候,遇到1開頭的ID的時候,這個時候就判斷出問題了,有類似問題的朋友可以了解下哈2013-06-06
extjs 列表框(multiselect)的動態(tài)添加列表項的方法
最近公司一個項目,因為要使用div模擬的窗口,因為久聞extjs的大名,因此就想在項目中使用一下.首先下載了multiselect的extjs3.0 demo.看到的代碼這里我就不粘貼了.2009-07-07
Extjs中的GridPanel隱藏列會顯示在menuDisabled中解決方法
在Extjs中的GridPanel會有這樣的情況,隱藏列會顯示在menuDisabled中,但是這個一般沒有什么用處,只是用于后臺取值的作用,感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01
ExtJs 學(xué)習(xí)筆記 Ext.Panle Ext.TabPanel Ext.Viewport
ExtJs 學(xué)習(xí)筆記基礎(chǔ)篇 面板的使用(Ext.Panle、Ext.TabPanel、Ext.Viewport)2008-12-12

