You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DispatchWeb/Content/hplus/js/plugins/suggest/bootstrap-suggest.min.js

7 lines
8.1 KiB
JavaScript

3 years ago
/*
Bootstrap_Search_Suggest - v0.0.1
Description: 这是一个基于 bootstrap 按钮式下拉菜单组件的搜索建议插件必须使用于按钮式下拉菜单组件上
Author: lizhiwen#meizu.com
Update: 2015-06-23 22:17:53
*/
!function(a){a.fn.bsSuggest=function(a){return"string"==typeof a&&b[a]?b[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?void 0:b.init.apply(this,arguments)};var b={init:function(b){function c(a,b){var c,d,e;return-1===b.indexId&&!b.idField||b.multiWord?a:(c=a.css("background-color").replace(/ /g,"").split(",",3).join(","),d="rgba(255,255,255,0.1)",e=b.inputWarnColor||"rgba(255,255,0,0.1)",!a.val()||a.attr("data-id")?a.css("background",d):(-1===e.indexOf(c)&&(a.trigger("onUnsetSelectValue"),a.css("background",e)),a))}function d(a,b){var c,d,e=a.parent().find("tbody tr."+r.listHoverCSS);e.length>0&&(c=(e.index()+3)*e.height(),d=Number(b.css("max-height").replace("px","")),b.scrollTop(c>d||b.scrollTop()>d?c-d:0))}function e(a,b){a=a||$dropdownMenu,b=b||r,a.find("tr."+b.listHoverCSS).removeClass(b.listHoverCSS)}function f(b){var c=a(b),d=c.parent(".input-group").find("ul.dropdown-menu"),e=c.data("bsSuggest");return 0===d.length?!1:e?!1:(c.data("bsSuggest",{target:b,options:r}),!0)}function g(b,c,d,e){var f,g,h,l,m,n,o,q={value:[]};if(b=b||"",e=e||r,e.url)m=-1!==e.url.indexOf("?")?"&":"?",n=e.jsonp?[e.url+b,m,e.jsonp,"=?"].join(""):e.url+b,a.ajax({type:"GET",url:n,dataType:"json",timeout:3e3}).done(function(a){d(c,a,e),c.trigger("onDataRequestSuccess",a),"firstByUrl"===r.getDataMethod&&(r.data=a,r.url=null)}).fail(p);else{if(f=e.data,g=i(f))if(b){for(o=f.value.length,h=0;o>h;h++)for(l in f.value[h])if(a.trim(f.value[h][l])&&(k(l,e)||j(l,e))&&(-1!==f.value[h][l].toString().indexOf(b)||-1!==b.indexOf(f.value[h][l]))){q.value.push(f.value[h]);break}}else q=f;d(c,q,e)}}function h(a){return validData=i(a)}function i(a){var b=!0;for(var c in a)if("value"===c){b=!1;break}return b?(p("返回数据格式错误!"),!1):0===a.value.length?!1:a}function j(b,c){return c=c||r,a.isArray(c.effectiveFields)&&c.effectiveFields.length>0&&-1===a.inArray(b,c.effectiveFields)?!1:!0}function k(b,c){return-1!==a.inArray(b,c.searchFields)?!0:!1}function l(a,b,c){var d,e,f,g,i,k,l,n=a.parent().find("ul.dropdown-menu"),o=0,p=['<table class="table table-condensed">'];if(c=c||r,b=h(b),b===!1||0===(d=b.value.length))return n.empty().hide(),a;if(c.showHeader){g="<thead><tr>";for(f in b.value[0])j(f)!==!1&&(g+=0===o?"<th>"+(c.effectiveFieldsAlias[f]||f)+"("+d+")</th>":"<th>"+(c.effectiveFieldsAlias[f]||f)+"</th>",o++);g+="</tr></thead>",p.push(g)}for(p.push("<tbody>"),e=0;d>e;e++){o=0,i="",k=b.value[e][c.idField]||"",l=b.value[e][c.keyField]||"";for(f in b.value[e])l||c.indexKey!==o||(l=b.value[e][f]),k||c.indexId!==o||(k=b.value[e][f]),o++,j(f)!==!1&&(i+='<td data-name="'+f+'">'+b.value[e][f]+"</td>");i='<tr data-index="'+e+'" data-id="'+k+'" data-key="'+l+'">'+i+"</tr>",p.push(i)}return p.push("</tbody></table>"),n.html(p.join("")).show(),m(a,n,c),n.css("max-height")&&Number(n.css("max-height").replace("px",""))<Number(n.find("table:eq(0)").css("height").replace("px",""))&&Number(n.css("min-width").replace("px",""))<Number(n.css("width").replace("px",""))?n.css("padding-right","20px").find("table:eq(0)").css("margin-bottom","20px"):n.css("padding-right",0).find("table:eq(0)").css("margin-bottom",0),a}function m(b,d,f){d=d||$dropdownMenu,f=f||r,d.find("tbody tr").each(function(){a(this).off("mouseenter").on("mouseenter",function(){e(d,f),a(this).addClass(f.listHoverCSS)}).off("mousedown").on("mousedown",function(){o(b,n(a(this)),f),c(b,f)})})}function n(a){var b={};return b.id=a.attr("data-id"),b.key=a.attr("data-key"),b}function o(a,b,c){var d,e=b||{},f=e.id||"",g=e.key||"";c&&c.multiWord?(d=a.val().split(c.separator||" "),d[d.length-1]=g,a.val(d.join(c.separator||" ")).focus()):a.attr("data-id",f).focus().val(g),a.trigger("onSetSelectValue",e)}function p(a,b){console.log(a),b&&console.log(b)}var q=this,r=a.extend({url:null,jsonp:null,data:{},getDataMethod:"firstByUrl",indexId:0,indexKey:0,idField:"",keyField:"",effectiveFields:[],effectiveFieldsAlias:{},searchFields:[],showHeader:!1,showBtn:!0,allowNoKeyword:!0,multiWord:!1,separator:",",processData:h,getData:g,autoMinWidth:!1,listAlign:"left",inputWarnColor:"rgba(255,0,0,.1