/* * 树形下拉列表 * 江鸿宾(QQ33080907) * 最近修改:2021.02.15 * 本插件只用于作者参与的项目,未经许可请勿转载 */ (function ($) { $.fn.treeselect = function (options) { var defaults = { server: '', cache: true, notop: false } var opts = $.extend(defaults, options) var $this = $(this) var amui = typeof AMUI === 'object' if ($this.length === 0) { return } else if ($this.length > 1) { alert('jquery.treeselect.js: the DOM\'s length > 1') } else if ($this.prop('tagName').toLowerCase() !== 'input') { alert('jquery.treeselect.js: only bind to input') } else if (!opts.url) { alert('jquery.treeselect.js: missing options (url)') } else { var deferreds = [] if (typeof ($.fn.zTree) !== 'function') { deferreds.push($.ajax('/Scripts/ztree/js/jquery.ztree.core.min.js', { dataType: 'script', cache: true })) } if (opts.server === '' && typeof Global === 'undefined') { deferreds.push($.ajax('/Scripts/global.js', { dataType: 'script', cache: true })) } $.when.apply(null, deferreds).done(function () { var key = 'treeselect-' + opts.url.substring(1).replace(/\/|\?|=|&/g, '-') var list = JSON.parse(localStorage.getItem(key)) if (opts.cache && list && list.length) { dealDom() makeselect(list) } else { $.getJSON((opts.server || Global.Host) + opts.url, { c: 'ztree' }, function (json) { if (json.code !== 0) { alert(json.message) } else { list = json.data dealDom() makeselect(list) localStorage.setItem(key, JSON.stringify(list)) } }) } }) } return this // 组合CSS和DOM function dealDom() { // 追加ztree插件所用CSS var style = '.ztree * {' + 'font-size: 14px;' + '}' + '.ztree {' + 'display: none;' + 'position: absolute;' + 'border: 1px solid #aaa;' + 'border-radius: 5px;' + 'background: #fff;' + 'box-shadow:0 1px 3px #ccc;' + 'z-index:3;' + 'overflow-y:auto;' + '}' + '.ztree li span.button.switch.level0 {' + 'visibility: hidden;' + 'width: 1px;' + '}' + '.ztree li ul.level0 {' + 'padding: 0;' + 'background: none;' + '}' + '.treeselect {' + 'position:relative;' + 'display:inline-block;' + 'vertical-align:middle;' + '}' if (!amui) { style += '.treeselect-tree-path-str {' + 'border: 1px solid #aaa;' + 'border-radius: 5px;' + 'background: linear-gradient(#fff 20%, #f6f6f6 50%, #eee 52%, #f4f4f4 100%);' + 'height: 23px;' + 'color: #333;' + 'font-size: 14px;' + 'cursor: pointer;' + 'line-height:23px;' + 'padding: 0 6px;' + 'vertical-align:middle;' + '}' + '.treeselect-tree-path-str i {' + 'font-style: normal;' + 'color: #999;' + 'margin-left: 5px;' + 'font-size: 18px;' + '}' } $('head') .append('') .append('') $this .hide() .prop('readonly', true) .wrap('
') // 必须定义ID,否则渲染中出现undefined .after('') if (amui) { $this.after('
请选择:
') } else { $this.after('
请选择:
') } $this.next('div.treeselect-tree-path-str').on('click', function () { var btn = $(this) var tree = $this.siblings('ul.ztree') if (tree.is(':hidden')) { var winHeight = $(window).height() var position = btn.position() position.top += btn.outerHeight() + 1 tree.css(position).css('max-height', winHeight - tree.offset().top - 110).slideDown() amui ? btn.children('i').attr('class', 'am-icon-caret-up') : btn.children('i').text('▴') $(document).on('mousedown', function (e) { // 点击外部隐藏(修正点击滚动条消失的BUG) if (!($(e.target).is('ul.ztree') || $(e.target).parents('ul.ztree').length > 0)) { tree.fadeOut('fast') amui ? btn.children('i').attr('class', 'am-icon-caret-down') : btn.children('i').text('▾') $(document).off('mousedown') } }) } else { tree.fadeOut('fast') amui ? btn.children('i').attr('class', 'am-icon-caret-down') : btn.children('i').text('▾') $(document).off('mousedown') } }) } // 生成下拉列表 function makeselect(list) { var btn = $this.next('div.treeselect-tree-path-str') var ul = $this.siblings('ul.ztree') var parent = list.find(item => item.pId === 0) if ($this.val() === '') { $this.val(parent.id) } btn.children('span').text(parent.name) // 构建树 var tree = $.fn.zTree.init(ul, { data: { simpleData: { enable: true } }, callback: { onClick: function (event, treeId, treeNode) { $this.val(treeNode.id) setPathStr(tree) ul.fadeOut('fast') amui ? btn.children('i').attr('class', 'am-icon-caret-down') : btn.children('i').text('▾') $(document).off('mousedown') } } }, list) // 初始值 tree.selectNode(tree.getNodeByParam('id', $this.val())) setPathStr(tree) } // 设置路径字符串 function setPathStr(tree) { var btn = $this.next('div.treeselect-tree-path-str') var node = tree.getSelectedNodes() if (node.length) { var path = node[0].getPath() var pathStr = '' if (path.length === 1) { pathStr = path[0].name } else { for (let i = 1; i < path.length; i++) { pathStr += (i === 1 ? '' : '/') + path[i].name } } btn.children('span').html(pathStr) } } } })(jQuery)