diff --git a/README.md b/README.md index 53833b9..5060144 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,22 @@ To have the first item be automatically focused on when the autocomplete menu is Now your autocomplete code is unobtrusive, Rails style. +#### Client-side config + +To configure the behaviour if no matches are found, you can set the following options: + + jQuery.railsAutocomplete.options.showNoMatches //default true + jQuery.railsAutocomplete.options.noMatchesLabel //default 'no existing match' + +These will change the behaviour globally. To set them on a single input field use: + + f.autocomplete_field :brand_names, autocomplete_brand_name_products_path, + 'data-showNoMatches' => false + #or + f.autocomplete_field :brand_names, autocomplete_brand_name_products_path, + 'data-noMatchesLabel' => 'no brands found' + + ### Getting the object id If you need to use the id of the selected object, you can use the *id_element* attribute too: diff --git a/lib/assets/javascripts/autocomplete-rails-uncompressed.js b/lib/assets/javascripts/autocomplete-rails-uncompressed.js index abc42ee..ede6cc0 100644 --- a/lib/assets/javascripts/autocomplete-rails-uncompressed.js +++ b/lib/assets/javascripts/autocomplete-rails-uncompressed.js @@ -35,6 +35,10 @@ var _e = e; this.init(_e); }; + jQuery.railsAutocomplete.options = { + showNoMatches: true, + noMatchesLabel: 'no existing match' + } jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = { railsAutocomplete: '0.0.1' @@ -67,9 +71,17 @@ }); } jQuery.getJSON( jQuery(e).attr('data-autocomplete'), params, function() { - if(arguments[0].length === 0) { + var options = {}; + jQuery.extend(options, jQuery.railsAutocomplete.options); + jQuery.each(options, function(key, value) { + if(options.hasOwnProperty(key)) { + var attrVal = jQuery(e).attr('data-' + key); + options[key] = attrVal ? attrVal : value; + } + }); + if(arguments[0].length == 0 && options.showNoMatches) { arguments[0] = []; - arguments[0][0] = { id: "", label: "no existing match" }; + arguments[0][0] = { id: "", label: options.noMatchesLabel }; } jQuery(arguments[0]).each(function(i, el) { var obj = {}; diff --git a/lib/assets/javascripts/autocomplete-rails.js b/lib/assets/javascripts/autocomplete-rails.js index f0cac3d..f942d19 100644 --- a/lib/assets/javascripts/autocomplete-rails.js +++ b/lib/assets/javascripts/autocomplete-rails.js @@ -1 +1 @@ -!function(t){t.fn.railsAutocomplete=function(){var e=function(){this.railsAutoCompleter||(this.railsAutoCompleter=new t.railsAutocomplete(this))};return void 0!==t.fn.on?t(document).on("focus",this.selector,e):this.live("focus",e)},t.railsAutocomplete=function(t){var e=t;this.init(e)},t.railsAutocomplete.fn=t.railsAutocomplete.prototype={railsAutocomplete:"0.0.1"},t.railsAutocomplete.fn.extend=t.railsAutocomplete.extend=t.extend,t.railsAutocomplete.fn.extend({init:function(e){function a(t){return t.split(e.delimiter)}function i(t){return a(t).pop().replace(/^\s+/,"")}e.delimiter=t(e).attr("data-delimiter")||null,e.min_length=t(e).attr("min-length")||2,e.append_to=t(e).attr("data-append-to")||null,e.autoFocus=t(e).attr("data-auto-focus")||!1,t(e).autocomplete({appendTo:e.append_to,autoFocus:e.autoFocus,delay:t(e).attr("delay")||0,source:function(a,n){var r=this.element[0],o={term:i(a.term)};t(e).attr("data-autocomplete-fields")&&t.each(t.parseJSON(t(e).attr("data-autocomplete-fields")),function(e,a){o[e]=t(a).val()}),t.getJSON(t(e).attr("data-autocomplete"),o,function(){0===arguments[0].length&&(arguments[0]=[],arguments[0][0]={id:"",label:"no existing match"}),t(arguments[0]).each(function(a,i){var n={};n[i.id]=i,t(e).data(n)}),n.apply(null,arguments),t(r).trigger("railsAutocomplete.source",arguments)})},change:function(e,a){if(t(this).is("[data-id-element]")&&""!==t(t(this).attr("data-id-element")).val()&&(t(t(this).attr("data-id-element")).val(a.item?a.item.id:""),t(this).attr("data-update-elements"))){var i=t.parseJSON(t(this).attr("data-update-elements")),n=a.item?t(this).data(a.item.id.toString()):{};if(i&&""===t(i.id).val())return;for(var r in i){var o=t(i[r]);o.is(":checkbox")?null!=n[r]&&o.prop("checked",n[r]):o.val(a.item?n[r]:"")}}},search:function(){var t=i(this.value);return t.length