优化 dropdown 的 `templet` 属性,以支持函数写法

pull/1229/head
贤心 2023-04-22 17:24:55 +08:00
parent e3152bda21
commit 0ca0fddca3
1 changed files with 25 additions and 19 deletions

View File

@ -130,36 +130,42 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
that.events(); // 事件 that.events(); // 事件
}; };
//渲染 // 渲染
Class.prototype.render = function(rerender, type){ Class.prototype.render = function(rerender, type){
var that = this var that = this;
,options = that.config var options = that.config;
,elemBody = $('body') var elemBody = $('body');
//默认菜单内容 // 默认菜单内容
,getDefaultView = function(){ var getDefaultView = function(){
var elemUl = $('<ul class="layui-menu layui-dropdown-menu"></ul>'); var elemUl = $('<ul class="layui-menu layui-dropdown-menu"></ul>');
if(options.data.length > 0 ){ if(options.data.length > 0 ){
eachItemView(elemUl, options.data) eachItemView(elemUl, options.data)
} else { } else {
elemUl.html('<li class="layui-menu-item-none">no menu</li>'); elemUl.html('<li class="layui-menu-item-none">No data</li>');
} }
return elemUl; return elemUl;
} };
//遍历菜单项 // 遍历菜单项
,eachItemView = function(views, data){ var eachItemView = function(views, data){
//var views = []; // var views = [];
layui.each(data, function(index, item){ layui.each(data, function(index, item){
//是否存在子级 // 是否存在子级
var isChild = item.child && item.child.length > 0 var isChild = item.child && item.child.length > 0;
,isSpreadItem = ('isSpreadItem' in item) ? item.isSpreadItem : options.isSpreadItem var isSpreadItem = ('isSpreadItem' in item) ? item.isSpreadItem : options.isSpreadItem
,title = item.templet var title = function(title){
? laytpl(item.templet).render(item) var templet = item.templet || options.templet;
: (options.templet ? laytpl(options.templet).render(item) : item.title) if(templet){
title = typeof templet === 'function'
? templet(item)
: laytpl(templet).render(item);
}
return title;
}(item.title);
//初始类型 // 初始类型
,type = function(){ var type = function(){
if(isChild){ if(isChild){
item.type = item.type || 'parent'; item.type = item.type || 'parent';
} }