easyui下拉框过滤优化
项目中有个需求:编辑combobox的输入域会自动检索匹配项,当没有任何匹配项时,将combobox重置为初始状态。
处理方式:重写输入域的blur事件,判断当前值是否为加载的数据集的子集,如果不是则重置。
脚本如下:
$.fn.combobox.defaults.inputEvents.blur = function(e){ var target = e.data.target; var opts = $(target).combobox('options'); if (opts.reversed || opts.limitToList){ if (opts.blurTimer){ clearTimeout(opts.blurTimer); } opts.blurTimer = setTimeout(function(){ var existing = $(target).parent().length; if (existing){ if (opts.reversed){ $(target).combobox('setValues', $(target).combobox('getValues')); } else if (opts.limitToList){ doEnter(target); } opts.blurTimer = null; } },50); } /* 过滤没有匹配项时,重置为初始状态 */ var datas = $(target).combobox('getData'); if (datas) { var has = false; datas.map(function(data){ var text = data[opts.textField].trim(), val = data[opts.valueField], ctxt = $(target).combobox('getText').trim(), cVal = $(target).combobox('getValue'); if(text == ctxt && val == cVal) has = true; }); if(!has) $(target).combobox('reset'); } }