/* * 树形下拉列表 * 江鸿宾(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('