新增 table 事件返回的 `obj.setRowChecked()` 公共方法,用于设置当前选中行状态

pull/1194/head
贤心 2 years ago
parent 62e6d682b4
commit 3c1a9a41bc

@ -1837,8 +1837,8 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
// 分页栏操作事件 // 分页栏操作事件
that.layPagebar.on('click', '*[lay-event]', function(e){ that.layPagebar.on('click', '*[lay-event]', function(e){
var othis = $(this) var othis = $(this);
,events = othis.attr('lay-event'); var events = othis.attr('lay-event');
layui.event.call(this, MOD_NAME, 'pagebar('+ filter +')', $.extend({ layui.event.call(this, MOD_NAME, 'pagebar('+ filter +')', $.extend({
event: events event: events
@ -1993,24 +1993,26 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
var data = table.cache[that.key] || []; var data = table.cache[that.key] || [];
data = data[index] || {}; data = data[index] || {};
return $.extend({ // 事件返回的公共成员
var obj = {
tr: tr //行元素 tr: tr //行元素
,config: options ,config: options
,data: table.clearCacheKey(data) //当前行数据 ,data: table.clearCacheKey(data) //当前行数据
,index: index
,del: function(){ //删除行数据 ,del: function(){ //删除行数据
table.cache[that.key][index] = []; table.cache[that.key][index] = [];
tr.remove(); tr.remove();
that.scrollPatch(); that.scrollPatch();
} }
,update: function(fields, related){ //修改行数据 ,update: function(fields, related){ // 修改行数据
fields = fields || {}; fields = fields || {};
layui.each(fields, function(key, value){ layui.each(fields, function(key, value){
var td = tr.children('td[data-field="'+ key +'"]'); var td = tr.children('td[data-field="'+ key +'"]');
var cell = td.children(ELEM_CELL); //获取当前修改的列 var cell = td.children(ELEM_CELL); //获取当前修改的列
// 更新缓存中的数据 // 更新缓存中的数据
if(key in data) data[key] = value; if(key in data) data[key] = obj.data[key] = value;
// 更新相应列视图 // 更新相应列视图
that.eachCols(function(i, item3){ that.eachCols(function(i, item3){
@ -2043,7 +2045,14 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
that.renderForm(); that.renderForm();
} }
}, sets); // 设置行选中状态
,setRowChecked: function(checked){
that.setThisRowChecked(index, true);
}
// 获取当前列
};
return $.extend(obj, sets);
}; };
// 复选框选择(替代元素的 click 事件) // 复选框选择(替代元素的 click 事件)
@ -2080,23 +2089,15 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
); );
}); });
//单选框选择 // 单选框选择
that.elem.on('click', 'input[lay-type="layTableRadio"]+', function(){ that.elem.on('click', 'input[lay-type="layTableRadio"]+', function(){
var radio = $(this).prev() var radio = $(this).prev();
,checked = radio[0].checked var checked = radio[0].checked;
,thisData = table.cache[that.key] var index = radio.parents('tr').eq(0).data('index');
,index = radio.parents('tr').eq(0).data('index');
// 单选框选中状态
//重置数据单选属性 that.setThisRowChecked(index, true);
layui.each(thisData, function(i, item){ // 事件
if(index === i){
item[options.checkName] = true;
} else {
delete item[options.checkName];
}
});
that.setThisRowChecked(index);
layui.event.call(this, MOD_NAME, 'radio('+ filter +')', commonMember.call(this, { layui.event.call(this, MOD_NAME, 'radio('+ filter +')', commonMember.call(this, {
checked: checked checked: checked
})); }));
@ -2104,13 +2105,13 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
// 行事件 // 行事件
that.layBody.on('mouseenter', 'tr', function(){ //鼠标移入行 that.layBody.on('mouseenter', 'tr', function(){ //鼠标移入行
var othis = $(this) var othis = $(this);
,index = othis.index(); var index = othis.index();
if(othis.data('off')) return; //不触发事件 if(othis.data('off')) return; //不触发事件
that.layBody.find('tr:eq('+ index +')').addClass(ELEM_HOVER) that.layBody.find('tr:eq('+ index +')').addClass(ELEM_HOVER)
}).on('mouseleave', 'tr', function(){ //鼠标移出行 }).on('mouseleave', 'tr', function(){ //鼠标移出行
var othis = $(this) var othis = $(this);
,index = othis.index(); var index = othis.index();
if(othis.data('off')) return; //不触发事件 if(othis.data('off')) return; //不触发事件
that.layBody.find('tr:eq('+ index +')').removeClass(ELEM_HOVER) that.layBody.find('tr:eq('+ index +')').removeClass(ELEM_HOVER)
}).on('click', 'tr', function(){ //单击行 }).on('click', 'tr', function(){ //单击行
@ -2124,8 +2125,8 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
var othis = $(this); var othis = $(this);
if(othis.data('off')) return; //不触发事件 if(othis.data('off')) return; //不触发事件
layui.event.call(this, layui.event.call(this,
MOD_NAME, eventType + '('+ filter +')' MOD_NAME, eventType + '('+ filter +')',
,commonMember.call(othis.children('td')[0]) commonMember.call(othis.children('td')[0])
); );
}; };
@ -2176,11 +2177,11 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
//事件回调的参数对象 //事件回调的参数对象
var params = commonMember.call(td[0], { var params = commonMember.call(td[0], {
value: value value: value,
,field: field field: field,
,oldValue: data[field] // 编辑前的值 oldValue: data[field], // 编辑前的值
,td: td td: td,
,reedit: function(){ // 重新编辑 reedit: function(){ // 重新编辑
setTimeout(function(){ setTimeout(function(){
// 重新渲染为编辑状态 // 重新渲染为编辑状态
renderGridEdit(params.td); renderGridEdit(params.td);
@ -2330,9 +2331,11 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
settings = settings || {}; settings = settings || {};
var that = this; var that = this;
var inst = null; var inst = null;
var elemTable = filter var elemTable = typeof filter === 'object' ? filter : (
? $('table[lay-filter="'+ filter +'"]') typeof filter === 'string'
: $(ELEM + '[lay-data], '+ ELEM + '[lay-options]'); ? $('table[lay-filter="'+ filter +'"]')
: $(ELEM + '[lay-data], '+ ELEM + '[lay-options]')
);
var errorTips = 'Table element property lay-data configuration item has a syntax error: '; var errorTips = 'Table element property lay-data configuration item has a syntax error: ';
//遍历数据表格 //遍历数据表格

Loading…
Cancel
Save