(function( $ ) { $.widget( "ui.combobox", { options: { editable: false, ajax: false,// 是否异步请求数据 datatablelogicid: "",// 表id childtarget: "",// 子checkbox控件 textcolumn: "",// 显示列 autoload : true, // 自动加载 valuecolumn: "",// 存储列 type: "",// 类型(singleselect:单级下拉列表,multiselect:多级下拉列表) property: {},// 查询条件应为{key:value}格式的字符串 id: "",// input 的id button: true, onselect: function(){},// 选中事件 afteraddoptions: function(){}// 添加options后事件 }, _create: function() { var input, that = this, select = this.element.hide(), wrapper = this.wrapper = $( "
" ) .addclass( "ui-combobox" ) .attr("style", select.attr("style")) .show() .insertafter( select ); // 判断是否异步请求 if(this.options.ajax && this.options.autoload){ addoptions(select, that, {}); } var selected = select.children( ":selected" ), value = selected.val() ? selected.text() : ""; function removeifinvalid(element) { var value = $( element ).val(), matcher = new regexp( "^" + $.ui.autocomplete.escaperegex( value ) + "$", "i" ), valid = false; select.children( "option" ).each(function() { if ( $( this ).text().match( matcher ) ) { this.selected = valid = true; return false; } }); if ( !valid ) { // 如果当前文本框为可编辑状态 if(that.options.editable){ var firstoption = select.children( "option" )[0]; $(firstoption).attr("value", value); firstoption.selected = true return false; } // remove invalid value, as it didn't match anything $( element ) .val( "" ) .attr( "title", value + " 未搜索到匹配项" ); select.val( "" ); input.data( "autocomplete" ).term = ""; return false; } } input = $( "" ) .appendto( wrapper ) .val( value ) .css("height", select.height()) .attr("id", this.options.id) .attr( "title", "" ) .addclass( "ui-state-default ui-combobox-input" ) .addclass(select.attr("class")) .autocomplete({ delay: 0, minlength: 0, source: function( request, response ) { var matcher = new regexp( $.ui.autocomplete.escaperegex(request.term), "i" ); response( select.children( "option" ).map(function() { var text = $( this ).text(); if ( this.value && ( !request.term || matcher.test(text) ) ) return { label: text.replace( new regexp( "(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escaperegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi" ), "$1" ), value: text, option: this }; }) ); }, select: function( event, ui ) { ui.item.option.selected = true; if($(ui.item.option).data("bindeddata") != null){ that.options.onselect($(ui.item.option).data("bindeddata")); }else{ that.options.onselect({ value:$(ui.item.option).val(), text: $(ui.item.option).text() }); } if(that.options.type == "multiselect"){ // 多选情况刷新子集 if(that.options.childtarget){ that.options.childtarget.combobox("refresh", {id: $(ui.item.option).attr("id")}); } } that._trigger( "selected", event, { item: ui.item.option }); }, change: function( event, ui ) { if ( !ui.item ) return removeifinvalid( this ); } }) .addclass( "ui-widget ui-widget-content ui-corner-left" ); input.data( "autocomplete" )._renderitem = function( ul, item ) { return $( "
  • " ) .data( "item.autocomplete", item ) .append( "
    " + item.label + "
    " ) .appendto( ul ); }; if(that.options.button){ input.css({ width: "90%", "line-height": input.height() + "px" }); $( "" ) .attr( "tabindex", -1 ) .attr( "title", "显示所有" ) .css("height", select.height()) .appendto( wrapper ) .button({ icons: { primary: "ui-icon-triangle-1-s" }, text: false }) .removeclass( "ui-corner-all" ) .addclass( "ui-corner-right ui-combobox-toggle" ) .click(function() { // close if already visible if ( input.autocomplete( "widget" ).is( ":visible" ) ) { input.autocomplete( "close" ); removeifinvalid( input ); return; } // work around a bug (likely same cause as #5265) $( this ).blur(); // pass empty string as value to search for, displaying all results input.autocomplete( "search", "" ); input.focus(); }); }else{ input.css({ width: "100%", "line-height": input.height() + "px" }); } this.element.data("input", input); }, setval: function(value) { this.element.val(value); var input = this.element.data("input"); input.val(this.element.children(":selected").text()); }, refresh: function(data) { // 刷新 var select = this.element; // 判断是否异步请求 if(this.options.ajax){ $(select).empty(); // 文本框 var input = this.element.data("input"); input.val(""); addoptions(select, this, {selectpid: data.id}); } }, destroy: function() { this.wrapper.remove(); this.element.show(); $.widget.prototype.destroy.call( this ); } }); // 在异步请求的情况下使用ajax添加option function addoptions(element, that, property){ // 添加一个空的option $("") .appendto(element); $.ajax({ url: contextrootpath + "/release/searchoptionsajax.xhtml", type: "post", datatype: "json", async:false, data: { "datatablelogicid": that.options.datatablelogicid, "property": json.stringify($.extend(that.options.property, property)), "formlogicid": that.options.formlogicid, "type": that.options.type, "infopathlogicid": that.options.infopathlogicid }, success: function(datas){ if(datas.error){ alert(datas.msg); return; } $.each(datas, function(k, data){ $("") .data("bindeddata",data) .appendto(element); }); element.val(that.element.attr("defaultvalue")); that.options.afteraddoptions.call(that.element, datas); } }); } })( jquery );