Browse Source

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

pull/1229/head
贤心 2 years ago
parent
commit
0ca0fddca3
  1. 32
      src/modules/dropdown.js

32
src/modules/dropdown.js

@ -132,34 +132,40 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
// 渲染 // 渲染
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';
} }

Loading…
Cancel
Save