extjs表格文本啟用選擇複製功能具體實現

來源:互聯網
上載者:User

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
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.