开发者

rails3-jquery-autocomplete Populate drop down on initial focus

开发者 https://www.devze.com 2023-04-04 19:38 出处:网络
I\'m trying to have the drop down list of items from autocomplete automatically populate on focus with all entries from a particular table when there is nothing in the autocomplete text field. Everyth

I'm trying to have the drop down list of items from autocomplete automatically populate on focus with all entries from a particular table when there is nothing in the autocomplete text field. Everything works perfectly with it except I need this one piece of functionality.

I've tried:

$('#activity_name').focus(function() {
  if (!$(this).val() || $(this).val() == '') {
    $(this).val('%');
    $(this).trigger("focus");
  }
})

Where #activity_name is the autocomplete_field_tag. You can tell from the value that I'm trying to get all the items in the table to show when the field is initially focused on.

I've altered the autocomplete_rails.js file to use 1+ letters instead of 2+ letters so it should be querying with just the % character (which I'm able to confirm by just typing % into the text field).

Here's the code I changed in autocomplete_rails.js:

search: function() {
  var a = c(this.value);
  if (a.length < 1) return ! 1
},
开发者_Python百科

Any insight would be appreciated.


Start in defining the source.

You can actually refactor my workaround.

                    $("input#xiruki").autocomplete(
                    {
                        source: <%= raw Category.select("distinct description as value, code as name, id as ID").where("description ILIKE ?", "%#{params[:term]}%").to_json %>,
                        minLength: 0,
                        select: function (event, data) {
                            $(this).autocomplete('disable');
                        console.log(data.item);

                        }
                    })
                    .blur(function(){
                        $(this).autocomplete('enable');
                    })
                    .focus(function () {
                        $(this).autocomplete('search', '');
                    });

You can change the ILIKE term to LIKE because ILIKE is meant for PG only.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号