diff --git a/dist/jquery.bootcomplete.js b/dist/jquery.bootcomplete.js index 3e7b34a..f798694 100644 --- a/dist/jquery.bootcomplete.js +++ b/dist/jquery.bootcomplete.js @@ -6,26 +6,28 @@ * @license MIT License * */ -(function ( $ ) { - - $.fn.bootcomplete = function(options) { - +(function ($) { + $.fn.bootcomplete = function (options) { + var defaults = { - url : "/search.php", - method : 'get', - wrapperClass : "bc-wrapper", - menuClass : "bc-menu", - idField : true, - idFieldName : $(this).attr('name')+"_id", - minLength : 3, - dataParams : {}, - formParams : {} + url: "/search.php", + method: 'get', + wrapperClass: "bc-wrapper", + menuClass: "bc-menu", + idField: true, + idFieldName: $(this).attr('name') + "_id", + minLength: 3, + dataParams: {}, + formParams: {}, + // something like www.site.com/products/_id_to_replace_ + // where _id_to_replace_ will be replaced with "id" of selected item + linkFormatToRedirectOnClick: null } - - var settings = $.extend( {}, defaults, options ); - - $(this).attr('autocomplete','off') - $(this).wrap('
') + + var settings = $.extend({}, defaults, options); + + $(this).attr('autocomplete', 'off') + $(this).wrap('
') if (settings.idField) { if ($(this).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) { //use existing id field @@ -34,8 +36,8 @@ $('').insertBefore($(this)) } } - $('
').insertAfter($(this)) - + $('
').insertAfter($(this)) + $(this).on("keyup", searchQuery); $(this).on("focusout", hideThat) @@ -48,64 +50,68 @@ } $(that).next('.' + settings.menuClass).hide(); } - - function searchQuery(){ - + + function searchQuery() { + var arr = []; - $.each(settings.formParams,function(k,v){ - arr[k]=$(v).val() + $.each(settings.formParams, function (k, v) { + arr[k] = $(v).val() }) - var dyFormParams = $.extend({}, arr ); - var Data = $.extend({query: $(this).val()}, settings.dataParams, dyFormParams); - - if(!Data.query){ - $(this).next('.'+settings.menuClass).html('') - $(this).next('.'+settings.menuClass).hide() + var dyFormParams = $.extend({}, arr); + var Data = $.extend({ query: $(this).val() }, settings.dataParams, dyFormParams); + + if (!Data.query) { + $(this).next('.' + settings.menuClass).html('') + $(this).next('.' + settings.menuClass).hide() } - - if(Data.query.length >= settings.minLength){ - - if(xhr && xhr.readyState != 4){ + + if (Data.query.length >= settings.minLength) { + + if (xhr && xhr.readyState != 4) { xhr.abort(); } - + xhr = $.ajax({ type: settings.method, url: settings.url, data: Data, dataType: "json", - success: function( json ) { + success: function (json) { var results = '' - $.each( json, function(i, j) { - results += ''+j.label+'' + $.each(json, function (i, j) { + results += '' + j.label + '' }); - - $(that).next('.'+settings.menuClass).html(results) - $(that).next('.'+settings.menuClass).children().on("click", selectResult) - $(that).next('.'+settings.menuClass).show() - + + $(that).next('.' + settings.menuClass).html(results) + $(that).next('.' + settings.menuClass).children().on("click", selectResult) + $(that).next('.' + settings.menuClass).show() + } }) } } - - function selectResult(){ - $(that).val($(this).data('label')) - if (settings.idField) { - if ($(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) { - //use existed id field - $(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); - } - else { - //use created id field - $(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); + + function selectResult() { + if (settings.linkFormatToRedirectOnClick === null) { + $(that).val($(this).data('label')) + if (settings.idField) { + if ($(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) { + //use existed id field + $(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); + } + else { + //use created id field + $(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id')); + } } + $(that).next('.' + settings.menuClass).hide(); + } else { + window.location.href = settings.linkFormatToRedirectOnClick.replace('_id_to_replace_', $(this).data('id')); } - $(that).next('.' + settings.menuClass).hide(); return false; } return this; }; - -}( jQuery )); + +}(jQuery));