i'd wrap standard jquery ui autocomplete jquery plugin multi-value autocomplete. works good, except 1 small thing. i'd show suggestions on focus. how should modify code?
(function ($) { $.fn.myautocomplete = function (options) { var defaults = { source: null, minlength: 0, separator: ", " }; var o = {}; $.extend(o, defaults, options); var split = function(val) { return val.split(/,\s*/); }; var extractlast = function(term) { return split(term).pop(); }; if (o.source == null) return this; return this.each(function () { $(this).autocomplete({ source: function(request, response) { $.ajax({ url: o.source, data: { term: extractlast(request.term) }, success: function(data) { response($.map(data, function(item) { return { label: item.name }; })); } }); }, search: function() { return extractlast(this.value).length > o.minlength; }, focus: function() { //$(this).trigger('keydown.autocomplete'); $(this).data("autocomplete").search($(this).val()); //return false; }, select: function(event, ui) { var terms = split(this.value); terms.pop(); terms.push(ui.item.label); terms.push(""); this.value = terms.join(o.separator); return false; } }); }); }; })(jquery);
i've tried see inside focus handler.
this helped me:
var showsuggestions = function() { var searchstring = extractlast(this.value); if (searchstring.trim().length == 0) { searchstring = "[getall]"; } $(this).autocomplete("search", searchstring); }; $(this).focus(showsuggestions).click(showsuggestions);
and on sever checking if [getall] consider empty string
Comments
Post a Comment