layui.define(['layer','dtree'],function(exports){ //提示:模块也可以依赖其它模块,如:layui.define('layer', callback); var layer = layui.layer; var dtree = layui.dtree; var opts={ "title":'选择员工', "department_url": "/api/index/get_department_tree", "employee_url": "/api/index/get_employee", "type":1, "ids":[], "names":[], "dids":[], "departments":[], "fixedid":0, "callback": function(){} }; obj = { render:function(){ var me=this,letterTem=''; for(var i=0;i<26;i++){ letterTem+=''+String.fromCharCode(65+i)+''; } var tpl='
\
\
\
\
⇐ 点击左边部门筛选员工,或者点击下面字母筛选
'+letterTem+'
\
\
已选择全选
\
'+me.initSelect()+'
\
'; return tpl; }, initSelect:function(){ var me=this,select_tags=''; if(me.settings.type == 0){ select_tags+=''+me.settings.names+''; } else{ for(var a=0;a'; } else{ select_tags+=''+me.settings.names[a]+''; } } } return select_tags; }, init: function (options) { this.settings = $.extend({}, opts, options); var me=this; layer.open({ type:1, title:me.settings.title, area:['800px','500px'], content:me.render(), success:function(obj,idx){ var dataList=[]; $.ajax({ url:me.settings.department_url, type:'get', success:function(res){ dtree.render({ elem: "#employeetree", data: res.trees, type: "all", initLevel: "1", icon: "2" //修改二级图标样式 }); dtree.on("node('employeetree')" ,function(obj){ $('#letterBtn').find('span').removeClass('on'); $.ajax({ url:me.settings.employee_url, type:'get', data:{did:obj.param.nodeId}, success:function(res){ dataList=res.data; if(dataList.length>2 && me.settings.type == 1){ $('.layui-tags-all').show(); } else{ $('.layui-tags-all').hide(); } if(dataList.length>0){ var tagsItem=''; for(var i=0; i'+dataList[i].nickname+''; } $('#employee').html(tagsItem); }else{ $('#employee').html('
暂无员工
'); } } }) }); $('#letterBtn').on("click" ,'span',function(){ var code=$(this).data('code'); $(this).addClass('on').siblings().removeClass('on'); $.ajax({ url:me.settings.employee_url, type:'get', data:{id:1}, success:function(res){ dataList=res.data; var letterData=[]; if(dataList.length>0){ var tagsItem=''; for(var i=0; i'+dataList[i].nickname+''; letterData.push(dataList[i]); } } dataList=letterData; if(dataList.length>2 && me.settings.type == 1){ $('.layui-tags-all').show(); } else{ $('.layui-tags-all').hide(); } if(tagsItem==''){ tagsItem='
暂无员工
'; } $('#employee').html(tagsItem); }else{ $('#employee').html('
暂无员工
'); } } }) }); } }); if(me.settings.type == 1){ $('.layui-tags-all').on('click',function(){ var sIds = me.settings.ids.join(','); var sNames = me.settings.names.join(','); var sDids = me.settings.dids.join(','); var sDepartments = me.settings.departments.join(','); for(var a=0; a=0){ return; } if(me.settings.type == 0){ layer.close(idx); me.settings.callback(item_id,item_name,item_did,item_department); } else{ var sIds = me.settings.ids.join(',')+','+item_id; var sNames = me.settings.names.join(',')+','+item_name; var sDids = me.settings.dids.join(',')+','+item_did; var sDepartments = me.settings.departments.join(',')+','+item_department; if(me.settings.ids.length==0){ sIds=item_id; sNames=item_name; sDids=item_did; sDepartments=item_department; } me.settings.ids=sIds.split(','); me.settings.names=sNames.split(','); me.settings.dids=sDids.split(','); me.settings.departments=sDepartments.split(','); $('#selectTags').html(me.initSelect()); } }); $('#selectTags').on('click','.layui-tags-close',function(){ var index=$(this).data('index'); var newIds=[],newNames=[],newDids=[],newDepartments=[]; for(var i=0;i0){ ids_array=ids.split(','); names_array=names.split(','); } obj.init({ ids:ids_array, names:names_array, type:1, callback:function(ids,names,dids,departments){ that.val(names.join(',')); that.next().val(ids.join(',')); } }); }); //输出接口 exports('employeepicker', obj); });