优化dropdown的reload相关代码新增reloadData方法支持更新数据和内容

pull/1206/head
sunxiaobin89 2023-03-10 10:06:22 +08:00
parent b3d95f4e55
commit 78d9a24312
1 changed files with 34 additions and 11 deletions

View File

@ -48,6 +48,9 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
,reload: function(options){ ,reload: function(options){
that.reload.call(that, options); that.reload.call(that, options);
} }
,reloadData: function(options){
dropdown.reloadData(id, options);
}
,close: function () { ,close: function () {
that.remove() that.remove()
} }
@ -83,14 +86,14 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
}; };
//重载实例 //重载实例
Class.prototype.reload = function(options){ Class.prototype.reload = function(options, type){
var that = this; var that = this;
that.config = $.extend({}, that.config, options); that.config = $.extend({}, that.config, options);
that.init(true); that.init(true, type);
}; };
//初始化准备 //初始化准备
Class.prototype.init = function(rerender){ Class.prototype.init = function(rerender, type){
var that = this var that = this
,options = that.config ,options = that.config
,elem = options.elem = $(options.elem); ,elem = options.elem = $(options.elem);
@ -110,18 +113,18 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
var newThat = thisModule.getThis(elem.data(MOD_INDEX)); var newThat = thisModule.getThis(elem.data(MOD_INDEX));
if(!newThat) return; if(!newThat) return;
return newThat.reload(options); return newThat.reload(options, type);
} }
//初始化 id 参数 //初始化 id 参数
options.id = ('id' in options) ? options.id : that.index; options.id = ('id' in options) ? options.id : that.index;
if(options.show) that.render(rerender); //初始即显示 if(options.show) that.render(rerender, type); //初始即显示
that.events(); //事件 that.events(); //事件
}; };
//渲染 //渲染
Class.prototype.render = function(rerender){ Class.prototype.render = function(rerender, type){
var that = this var that = this
,options = that.config ,options = that.config
,elemBody = $('body') ,elemBody = $('body')
@ -177,7 +180,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
,'-': 'layui-menu-item-divider' ,'-': 'layui-menu-item-divider'
}; };
if(isChild || type){ if(isChild || type){
return ' class="'+ className[type] + + '"'; return ' class="'+ className[type] +'"';
} }
return item.disabled ? ' class="'+ STR_DISABLED +'"' : ''; return item.disabled ? ' class="'+ STR_DISABLED +'"' : '';
}() +'>' }() +'>'
@ -241,7 +244,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
//记录模板对象 //记录模板对象
that.elemView = $('.' + STR_ELEM + '[lay-id="' + options.id + '"]'); that.elemView = $('.' + STR_ELEM + '[lay-id="' + options.id + '"]');
if (that.elemView.length) { if ('reloadData' === type && that.elemView.length) {
that.elemView.html(options.content || getDefaultView()); that.elemView.html(options.content || getDefaultView());
} else { } else {
that.elemView = $(TPL_MAIN).attr('lay-id', options.id); that.elemView = $(TPL_MAIN).attr('lay-id', options.id);
@ -523,14 +526,34 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
}(); }();
//重载实例 //重载实例
dropdown.reload = function(id, options){ dropdown.reload = function(id, options, type){
var that = thisModule.getThis(id); var that = thisModule.getThis(id);
if(!that) return this; if(!that) return this;
that.reload(options); that.reload(options, type);
return thisModule.call(that); return thisModule.call(that);
}; };
// 仅重载数据
dropdown.reloadData = function(){
var args = $.extend([], arguments);
args[2] = 'reloadData';
// 重载时,与数据相关的参数
var dataParams = new RegExp('^('+ [
'data', 'templet', 'content'
].join('|') + ')$');
// 过滤与数据无关的参数
layui.each(args[1], function (key, value) {
if(!dataParams.test(key)){
delete args[1][key];
}
});
return dropdown.reload.apply(null, args);
};
//核心入口 //核心入口
dropdown.render = function(options){ dropdown.render = function(options){
var inst = new Class(options); var inst = new Class(options);