时间:2021-07-01 10:21:17 帮助过:32人阅读
<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
}