From ad1bf950d8a5ed5b4207455aa175f54d87814e4c Mon Sep 17 00:00:00 2001 From: afduarte Date: Fri, 2 Dec 2016 16:27:35 +0000 Subject: [PATCH 1/4] Added functionality to select the query key for a get request,limit the number of results shown, change the key in the results json for bothe the id and the label --- dist/jquery.bootcomplete.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/dist/jquery.bootcomplete.js b/dist/jquery.bootcomplete.js index 3e7b34a..4406a4e 100644 --- a/dist/jquery.bootcomplete.js +++ b/dist/jquery.bootcomplete.js @@ -1,4 +1,4 @@ -/* +//* * @name jQuery.bootcomplete * @projectDescription Lightweight AJAX autocomplete for Bootstrap 3 * @author Rick Somers | http://getwebhelp.com/bootcomplete @@ -19,7 +19,11 @@ idFieldName : $(this).attr('name')+"_id", minLength : 3, dataParams : {}, - formParams : {} + formParams : {}, + resultId : 'id', + resultLabel : 'label', + queryKey : 'query', + limit : -1 } var settings = $.extend( {}, defaults, options ); @@ -56,14 +60,16 @@ arr[k]=$(v).val() }) var dyFormParams = $.extend({}, arr ); - var Data = $.extend({query: $(this).val()}, settings.dataParams, dyFormParams); + var DataObj = {}; + DataObj[settings.queryKey] = $(this).val(); + var Data = $.extend(DataObj, settings.dataParams, dyFormParams); if(!Data.query){ $(this).next('.'+settings.menuClass).html('') $(this).next('.'+settings.menuClass).hide() } - if(Data.query.length >= settings.minLength){ + if(Data[settings.queryKey].length >= settings.minLength){ if(xhr && xhr.readyState != 4){ xhr.abort(); @@ -77,7 +83,8 @@ success: function( json ) { var results = '' $.each( json, function(i, j) { - results += ''+j.label+'' + if(settings.limit === i) return false; + results += ''+j[settings.resultLabel]+'' }); $(that).next('.'+settings.menuClass).html(results) @@ -108,4 +115,4 @@ return this; }; -}( jQuery )); +}( jQuery )); \ No newline at end of file From aa2f4dc335c8f919168f56d5ab5ec07e8cbd7747 Mon Sep 17 00:00:00 2001 From: afduarte Date: Fri, 2 Dec 2016 16:29:21 +0000 Subject: [PATCH 2/4] Changed the top comment somehow, fixed now --- dist/jquery.bootcomplete.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/jquery.bootcomplete.js b/dist/jquery.bootcomplete.js index 4406a4e..c7de561 100644 --- a/dist/jquery.bootcomplete.js +++ b/dist/jquery.bootcomplete.js @@ -1,4 +1,4 @@ -//* +/* * @name jQuery.bootcomplete * @projectDescription Lightweight AJAX autocomplete for Bootstrap 3 * @author Rick Somers | http://getwebhelp.com/bootcomplete From 5ebbfc41045d5825f02b97ffacbf0b8c7335ab77 Mon Sep 17 00:00:00 2001 From: afduarte Date: Fri, 2 Dec 2016 16:34:43 +0000 Subject: [PATCH 3/4] Changed the if to handle the new query key configurability --- dist/jquery.bootcomplete.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/jquery.bootcomplete.js b/dist/jquery.bootcomplete.js index c7de561..6167831 100644 --- a/dist/jquery.bootcomplete.js +++ b/dist/jquery.bootcomplete.js @@ -64,7 +64,7 @@ DataObj[settings.queryKey] = $(this).val(); var Data = $.extend(DataObj, settings.dataParams, dyFormParams); - if(!Data.query){ + if(!Data[settings.queryKey]){ $(this).next('.'+settings.menuClass).html('') $(this).next('.'+settings.menuClass).hide() } From 6a99f009f8bcbaf3037e98c2a39202ff6e464307 Mon Sep 17 00:00:00 2001 From: afduarte Date: Wed, 21 Dec 2016 10:55:56 +0000 Subject: [PATCH 4/4] Added handling for dom elements created after the code runs initially (by hooking into the body element instead of the input's and cascading down) --- dist/jquery.bootcomplete.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dist/jquery.bootcomplete.js b/dist/jquery.bootcomplete.js index 6167831..eaea424 100644 --- a/dist/jquery.bootcomplete.js +++ b/dist/jquery.bootcomplete.js @@ -9,7 +9,6 @@ (function ( $ ) { $.fn.bootcomplete = function(options) { - var defaults = { url : "/search.php", method : 'get', @@ -17,6 +16,7 @@ menuClass : "bc-menu", idField : true, idFieldName : $(this).attr('name')+"_id", + inputSelector : 'input[name="' + $(this).attr('name') + '"]', minLength : 3, dataParams : {}, formParams : {}, @@ -39,9 +39,9 @@ } } $('
').insertAfter($(this)) - - $(this).on("keyup", searchQuery); - $(this).on("focusout", hideThat) + + $('body').on("keyup",settings.inputSelector, searchQuery); + $('body').on("focusout",settings.inputSelector, hideThat); var xhr; var that = $(this) @@ -54,7 +54,6 @@ } function searchQuery(){ - var arr = []; $.each(settings.formParams,function(k,v){ arr[k]=$(v).val() @@ -115,4 +114,4 @@ return this; }; -}( jQuery )); \ No newline at end of file +}( jQuery ));