123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- layui.define(['jquery', 'layer', 'util', 'uParas', 'admin', 'zTree'], function (exports) {
- var $ = layui.$,
- uParas = layui.uParas,
- admin = layui.admin;
- var _g = {
- _init: function (setting) {
- _g._init(setting.url,
- setting.idKey,
- setting.pIdKey,
- setting.dataName,
- setting.dataId,
- setting.ifPid,
- setting.ul,
- setting.insTb,
- setting.menuContent_,
- setting.inputName,
- setting.inputId
- )
- },
- /**
- *
- * @param url 访问地址
- * @param ul ul 渲染地址
- * @param idKey 子主键
- * @param pIdKey 夫关系
- * @param dataName 数据name
- * @param dataId 数据id
- * @param ifPid 是否选择父id (默认选择)
- * @param insTb 外部参数
- * @param menuContent 样式
- * @param inputName
- * @param inputId
- * @private
- */
- _init: function (url, idKey, pIdKey, dataName, dataId, ifPid, ul, insTb, menuContent, inputName, inputId, defVal) {
- var dn, di, name, id;
- dn = dataName;
- di = dataId;
- if (ul == undefined) {
- ul = 'wzTree';
- }
- if (inputName == undefined) {
- name = dataName;
- } else {
- name = inputName;
- }
- if (inputId == undefined) {
- id = di;
- } else {
- id = inputId;
- }
- if (ifPid == undefined || ifPid === 0) {
- ifPid = 0;
- } else {
- ifPid = 1;
- }
- if (menuContent == undefined) {
- menuContent = "menuContent";
- }
- var obj = new Object();
- if (ifPid === 0) {
- obj.onClick = onClick;
- } else {
- obj.onClick = onClick;
- obj.beforeClick = zTreeBeforeClick
- }
- var setting = {
- check: {
- enable: false,
- chkboxType: {"Y": "", "N": ""}
- },
- view: {
- dblClickExpand: false
- },
- data: {
- simpleData: {
- enable: true,
- idKey: idKey,
- pIdKey: pIdKey,
- rootPId: null
- },
- key: {
- name: dataName
- }
- },
- callback: obj
- };
- function zTreeBeforeClick(treeId, treeNode, clickFlag) {
- return !treeNode.isParent;
- };
- /**
- * ztree 文本下拉
- */
- $.getJSON(uParas.baseUrl + url, {}, function (res) {
- if (res.data.length > 0) {
- $.fn.zTree.init($('#' + ul + ''), setting, res.data);
- $.fn.zTree.getZTreeObj(ul).expandAll(true);
- if (defVal !== undefined && defVal !== null && defVal !== "") {
- $.each(res.data, function (i, field) {
- if (field[dataId] == defVal) {
- $("#" + name + "").val(field[dataName]);
- $("#" + id + "").val(field[dataId]);
- return false;
- } else if (field[dataId] === defVal) {
- $("#" + name + "").val(field[dataName]);
- $("#" + id + "").val(field[dataId]);
- return false;
- }
- });
- }
- layer.closeAll('loading');
- }
- });
- function onClick(event, treeId, treeNode, clickFlag) {
- var zTree = $.fn.zTree.getZTreeObj(ul),
- nodes = zTree.getSelectedNodes(),
- v = "", idlist = "";
- for (var i = 0, l = nodes.length; i < l; i++) {
- v += nodes[i][dn] + ",";
- idlist += nodes[i][di] + ",";
- }
- if (v.length > 0) {
- v = v.substring(0, v.length - 1);
- idlist = idlist.substring(0, idlist.length - 1);
- }
- //隐藏 赋值
- var cityObj = $("#" + name + "");
- if (cityObj.val() == v) {
- $("#" + id + "").focus();
- $("#" + id + "").attr("value", "");
- $("#" + id + "").blur();
- $("#" + name + "").val('');
- } else {
- $("#" + name + "").val(v);
- $("#" + id + "").focus();
- $("#" + id + "").attr("value", idlist);
- $("#" + id + "").blur();
- }
- //文本赋值
- var groupId = idlist;
- if (groupId != "-1") {
- var Obj = new Object();
- Obj.gId = groupId;
- Obj.groupId = groupId;
- Obj.cId = treeNode.companyOrOrgId;
- if (insTb != null && insTb != undefined) {
- insTb.reload({where: Obj});
- }
- }
- hideMenu();
- }
- function hideMenu() {
- $("#" + menuContent + "").fadeOut("fast");
- $("body").unbind("mousedown", onBodyDown);
- }
- $("#" + name + "").click(function () {
- $("#" + menuContent + "").slideDown("fast");
- $("body").bind("mousedown", onBodyDown);
- });
- function onBodyDown(event) {
- if (!(event.target.id === name || event.target.id === "" + menuContent + "" || $(event.target).parents("#" + menuContent + "").length > 0)) {
- hideMenu();
- }
- }
- }, _initx: function (url, ul,onClick) {
- var setting = {
- data: {
- simpleData: {
- enable: true,
- }
- }, callback: {
- // beforeClick: beforeClick,
- onClick: onClick
- }
- };
- $.getJSON(uParas.baseUrl + url, {}, function (res) {
- if (res.data.length > 0) {
- $.fn.zTree.init($('#' + ul + ''), setting, res.data);
- $.fn.zTree.getZTreeObj(ul).expandAll(true);
- layer.closeAll('loading');
- }
- });
- // function beforeClick(treeId, treeNode, clickFlag) {
- // className = (className === "dark" ? "" : "dark");
- // showLog("[ " + getTime() + " beforeClick ] " + treeNode.name);
- // return (treeNode.click != false);
- // }
- // function onClick(event, treeId, treeNode, clickFlag) {
- // console.log(1);
- // showLog("[ " + getTime() + " onClick ] clickFlag = " + clickFlag + " (" + (clickFlag === 1 ? "single selected" : (clickFlag === 0 ? "<b>cancel selected</b>" : "<b>multi selected</b>")) + ")");
- // }
- }
- }
- exports("_zTree", _g);
- });
|