refactor(table): 优化行单双击事件机制,避免与单元格编辑等事件冲突 (#2064)

* chore(table): 优化行事件,新增返回 `event` 对象,可用于阻止事件冒泡

* docs: 更新文档

* refactor: 重新优化不触发行事件的机制
pull/2094/head
贤心 2024-07-09 14:22:49 +08:00 committed by GitHub
parent 47728370e8
commit 4c6c133a12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 28 deletions

View File

@ -768,18 +768,25 @@ table.render({
});
// 行单击事件
table.on('row(test)', function(obj){
table.on('row(test)', function(obj) {
var data = obj.data; // 得到当前行数据
var dataCache = obj.dataCache; // 得到当前行缓存数据,包含特定字段 --- 2.8.8+
var index = obj.index; // 得到当前行索引
var tr = obj.tr; // 得到当前行 <tr> 元素的 jQuery 对象
var options = obj.config; // 获取当前表格基础属性配置项
console.log(obj); // 查看对象所有成员
var e = obj.e; // 当前的 jQuery 事件对象 --- 2.9.14+
console.log('onrow', obj); // 查看返回对象的所有成员
// obj.del() // 删除当前行
// obj.update(fields, related); // 修改行数据
// obj.setRowChecked(opts); // 设置行选中状态
});
// 行双击事件
table.on('rowDouble(test)', function(obj) {
console.log('onrowDouble', obj); // 查看返回对象的所有成员 - 同 row 事件
});
```
<h3 id="on-rowContextmenu" lay-pid="table.on" class="ws-anchor ws-bold">行右键菜单事件 <sup>2.8+</sup></h3>

View File

@ -195,7 +195,7 @@ layui.use(['table', 'dropdown'], function(){
}
],
// escape: false,
editTrigger: 'dblclick',
// editTrigger: 'dblclick',
// cellMaxWidth: 320
// cellExpandedWidth: 160, // 单元格默认展开后的宽度
// cellExpandedStyle: 'tips', // 单元格默认展开风格
@ -555,18 +555,18 @@ layui.use(['table', 'dropdown'], function(){
});
// 行单击事件
table.on('row(test)', function(obj){
console.log(obj);
// layer.closeAll('tips');
table.on('row(test)', function(obj) {
console.log('onrow', obj); // 查看返回的对象成员
// 单击行设置选中
obj.setRowChecked({
// type: 'radio'
});
// layer.msg('row 事件')
});
// 行双击事件
table.on('rowDouble(test)', function(obj){
console.log(obj);
table.on('rowDouble(test)', function(obj) {
console.log('onrowDouble', obj); // 查看返回的对象成员
});
// 单元格编辑事件

View File

@ -2497,31 +2497,39 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
if(othis.data('off')) return; // 不触发事件
that.layBody.find('tr:eq('+ index +')').removeClass(ELEM_HOVER)
}).on('click', 'tr', function(e){ // 单击行
// 不支持行单击事件的元素
var UNROW = [
'.layui-form-checkbox',
'.layui-form-switch',
'.layui-form-radio',
'[lay-unrow]'
].join(',');
if( $(e.target).is(UNROW) || $(e.target).closest(UNROW)[0]){
return;
}
setRowEvent.call(this, 'row');
}).on('dblclick', 'tr', function(){ // 双击行
setRowEvent.call(this, 'rowDouble');
setRowEvent.call(this, 'row', e);
}).on('dblclick', 'tr', function(e){ // 双击行
setRowEvent.call(this, 'rowDouble', e);
}).on('contextmenu', 'tr', function(e){ // 菜单
if (!options.defaultContextmenu) e.preventDefault();
setRowEvent.call(this, 'rowContextmenu');
setRowEvent.call(this, 'rowContextmenu', e);
});
// 创建行单击、双击、菜单事件
var setRowEvent = function(eventType){
var setRowEvent = function(eventType, e){
var othis = $(this);
if(othis.data('off')) return; //不触发事件
layui.event.call(this,
if(othis.data('off')) return; // 不触发事件
// 不触发「行单/双击事件」的子元素
if (eventType !== 'rowContextmenu') {
var UNROW = [
'.layui-form-checkbox',
'.layui-form-switch',
'.layui-form-radio',
'[lay-unrow]'
].join(',');
if($(e.target).is(UNROW) || $(e.target).closest(UNROW)[0]){
return;
}
}
layui.event.call(
this,
MOD_NAME, eventType + '('+ filter +')',
commonMember.call(othis.children('td')[0])
commonMember.call(othis.children('td')[0], {
e: e
})
);
};
@ -2606,7 +2614,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
// 表格主体单元格触发编辑的事件
that.layBody.on(options.editTrigger, 'td', function(e){
renderGridEdit(this, e)
renderGridEdit(this, e);
}).on('mouseenter', 'td', function(){
showGridExpandIcon.call(this)
}).on('mouseleave', 'td', function(){