/* * 下拉列表缓存 * 江鸿宾(QQ33080907) * 最近修改:2020.12.03 * 本插件只用于作者参与的项目,未经许可请勿转载 */ (function ($) { $.fn.selectcache = function (options) { var defaults = { server: '', index: false, tree: false, cache: true, chosen: true, notop: false, delay: 0 } var opts = $.extend(defaults, options) var $this = $(this) if ($this.length === 0) { return } else if ($this.length > 1) { alert('jquery.selectcache.js: the DOM\'s length > 1') } else if ($this.prop('tagName').toLowerCase() !== 'select') { alert('jquery.selectcache.js: only bind to select') } else if (!opts.url) { alert('jquery.selectcache.js: missing options (url)') } else { var deferreds = [] setTimeout(function () { if (typeof store === 'undefined') { deferreds.push($.ajax('/Scripts/store2.min.js', { dataType: 'script', cache: true })) } if (opts.tree && typeof ($.fn.jTreeSelect) !== 'function') { deferreds.push($.ajax('/Scripts/jquery.jtree.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 = 'selectcache-' + opts.url.substring(1).replace(/\/|\?|=|&/g, '-') var list = store(key) if (opts.cache && list && list.length) { makeselect(list) } else { $.getJSON((opts.server || Global.Host) + opts.url, function (json) { if (json.code !== 0) { alert(json.message) } else { list = json.data makeselect(list) store(key, list) } }) } }) }, opts.delay * 1000) } return this // 生成下拉列表 function makeselect(list) { var value = $this.data('value') var html = '' if (opts.tree) { $.each(list, function (i, item) { html += '' }) $this.append(html).jTreeSelect() if (opts.notop) { $this.children().first().remove() } } else { $.each(list, function (i, item) { html += '' }) $this.append(html) } if (opts.chosen && typeof ($.fn.chosen) === 'function') { $this.width(function (i, w) { return w + 12 }).chosen({ disable_search_threshold: 10, no_results_text: '没有结果匹配:', search_contains: true, placeholder_text_multiple: '按住[Ctrl]可以一次性多选,或多次选择' }) } } } })(jQuery)