<input type="text" id="userId" name="userId" required lay-verify="required" class="layui-input">
AjaxK(1, '${pageContext.request.contextPath}/treeUser', {}, function (d) {
generateZTreeSelect('#userId', d, '${XXX.id}');
});
function generateZTreeSelect(element, data, selectedId) {
if (element.charAt(0) === '#') {
var elementName = element.substr(1, element.length);
var selectObj = $(element);
var newObject = selectObj.clone();
newObject.attr('id', elementName + '-name');
newObject.attr('name', elementName + '-name');
newObject.val('');
//自动创建
selectObj.parent().append(newObject);
selectObj.parent().append('<div class="ztree" id="' + elementName + '-zTree"></div>');
//隐藏原本的文本框
selectObj.hide();
var zTreeObj = $('#' + elementName + '-zTree');
var zTreeId = zTreeObj.attr("id");
zTreeObj.attr("style", "max-height:300px;height:auto;overflow:auto;position: absolute;z-index: 1");
zTreeObj.addClass("layui-input layui-hide");
//开始构建树状图
var setting = {
view: {
dblClickExpand: false,
showIcon: false,
showLine: true,
fontCss: {color: "black", "font-weight": "bold", "font-size": "15px"},
selectedMulti: true
},
data: {simpleData: {enable: false, idKey: "id", pIdKey: "pid", rootPId: null}},
callback: {
onClick: function (event, treeId, treeNode) {
var treeObj = $.fn.zTree.getZTreeObj(treeId);
treeObj.expandNode(treeNode);
},
beforeDblClick: function (treeId, treeNode) {
if (treeNode.selected) {
return true;
}
},
onDblClick: function (event, treeId, treeNode) {
selectObj.val(treeNode.id);
newObject.val(treeNode.name);
$.fn.zTree.getZTreeObj(treeId).expandAll(false);
zTreeObj.addClass('layui-hide');
layui.form.render();
}
}
};
$.fn.zTree.init(zTreeObj, setting, data);
var treeObj = $.fn.zTree.getZTreeObj(zTreeId);
//
var node = treeObj.getNodesByParam("id", selectedId, null);
treeObj.cancelSelectedNode();//先取消所有的选中状态
treeObj.selectNode(node, false, false);//将指定ID的节点选中
var nodes = treeObj.getSelectedNodes()[0];
if (nodes.length > 0) {
newObject.val(nodes[0].name);
}
//创建监听新文本框
newObject.on("click", function () {
if (zTreeObj.hasClass("layui-hide")) {
zTreeObj.removeClass("layui-hide");
} else {
zTreeObj.addClass("layui-hide");
$.fn.zTree.getZTreeObj(zTreeId).expandAll(false);
}
});
} else if (element.charAt(0) === '.') {
}
layui.form.render();
}