Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to change the "no existing match" label, and selecting it does nothing #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gfaraj
Copy link

@gfaraj gfaraj commented Mar 18, 2013

The input's value shouldn't be changed to "no existing match" if clicked, since it's not a valid item. I've fixed this.

Also, you can provide a "data-no-matches-label" attribute on the autocomplete input which specifies the text that will be shown if there are no matches.

<%= autocomplete_field_tag :user_name, '', autocomplete_user_name_users_path, :id_element => '#user_id', :data => { :no_matches_label => 'No matches found' } %>

For the future, we could have the label passed as a special hash value parameter.

@jmtoball
Copy link

I like it. It would be nice to have the additional possibility to globally set the label though.

@gfaraj
Copy link
Author

gfaraj commented Mar 19, 2013

I agree. I'll put it on my to-do list. Thanks.

@megalomania1982
Copy link

@gfaraj
How can I disable the message saying "no existing match" when there was no matched record?
Is there any option for that?

Because, I'm not using this input box only for the model related to auto-complete but also the columns in other tables, too.
So even if there was no matching record, I'd like to make my user to execute search.
Once the user see the message that's saying "no existing match", they probably won't proceed the search anymore:(

@gfaraj
Copy link
Author

gfaraj commented Apr 19, 2013

@megalomania1982 you would have to grab my changes since it hasn't been merged yet. No idea who controls that in this repo. If you get my changes, you could probably set the label text to an empty string, and that should be good enough for you.

@megalomania1982
Copy link

@gfaraj Thanks. but how can I grab your version? I have gem 'rails3-jquery-autocomplete', '1.0.7' in Gemfile.
Should I just gem uninstall rails3-jquery-autocomplete and delete all version.
Then bundle install?

@gfaraj
Copy link
Author

gfaraj commented Apr 19, 2013

Nope, since it hasn't been merged to master you can't do that. You would have to manually replace the autocomplete-rails.js file that you have installed in your gem with the version in this pull request.

@megalomania1982
Copy link

@gfaraj Just replacing the whole content of one file??? which file and where is the content that my file has to be?? Sorry for this noob question.

@gfaraj
Copy link
Author

gfaraj commented Apr 19, 2013

The file is autocomplete-rails.js.

Run the following command:
gem which rails3-jquery-autocomplete

It will give you the path to the autocomplete gem you have installed. In its parent path there's assets/javascripts. That's where you need to replace autocomplete-rails.js. For example:
~/.rvm/gems/ruby-1.9.2-p290/gems/rails3-jquery-autocomplete-1.0.11/lib/assets/javascripts

This is the new content:
https://github.com/gfaraj/rails3-jquery-autocomplete/blob/cde7e9477f237ef80321284f9c4b4375d740b77d/lib/assets/javascripts/autocomplete-rails.js

Note: Make a backup of the original js file in case you want to go back to the old version.

@megalomania1982
Copy link

@gfaraj Thanks for detailed explanation. I followed your instruction and I'm pretty sure that I've done perfect.

  1. make copy of original autocomplete-rails.js and paste then rename to autocomplete-rails_backup.js
  2. copy all the content of the source in https://github.com/gfaraj/rails3-jquery-autocomplete/blob/cde7e9477f237ef80321284f9c4b4375d740b77d/lib/assets/javascripts/autocomplete-rails.js , and replace the content of autocomplete-rails.js with it.
  3. /etc/init.d/httpd restart��

But it still pops up the message saying "no existing match"

@gfaraj
Copy link
Author

gfaraj commented Apr 19, 2013

Well, yeah, you have to pass the :no_matches_label parameter to autocomplete_field_tag. See my first comment above. Pass in an empty string to that and see what happens.

@megalomania1982
Copy link

@gfaraj I tried to add :no_matches_label parameter view just like this.
But it it's still getting the same message "no existing match"
Could think of any idea about this? :(

<%= autocomplete_field_tag 'search', params[:search], autocomplete_community_title_communities_path, :class =>'input-small', :placeholder => 'community name or tag', :data => { :no_matches_label => '' } %>

@megalomania1982
Copy link

@gfaraj If possible, could you please check my question?

@gfaraj
Copy link
Author

gfaraj commented Apr 26, 2013

I wonder if it works if you set the matches label to ' ' (one space) instead of an empty string.

@megalomania1982
Copy link

@gfaraj is there any old version it didn't show "no existing match"?

@megalomania1982
Copy link

@gfaraj please check and see it again, It's empty. not one space:)

@dimanlin
Copy link

dimanlin commented May 3, 2013

show "no existing match"
gem 'rails3-jquery-autocomplete', "= 1.0.11"
= f.autocomplete_field :name, autocomplete_developer_name_account_developers_path, :data => { :no_matches_label => "" }

@gfaraj
Copy link
Author

gfaraj commented May 3, 2013

@megalomania1982 I meant, try it with one space instead of an empty string. Does that remove the text?

@dimanlin it hasn't been merged yet, so you can't use it like that. I don't know when it's gonna be merged.

@megalomania1982
Copy link

@gfaraj I tried that. But the same result :(
When do you think they will fix it?

@megalomania1982
Copy link

@gfaraj I'd say it's not because of the update in this gem....
I tried to replace with the whole directory(/usr/local/lib/ruby/gems/1.9.1/gems/rails3-jquery-autocomplete-1.0.7)
with old one, which was not showing "no existing match"

However, the same result... It still shows this message....

I want to know what to fix...

@maxrails
Copy link

Maybe not the best solution, but it works:
in controller with autocomplete add

    def get_autocomplete_items parameters
      resp = super(parameters)
      if resp.blank?
        resp = [OpenStruct.new(id: '', label: '')]
      end
      resp
    end

with this it returns blank object inside array, instead of blank array. It will show nothing inside 'li' tag instead of 'no existing matches', and 'li' tag will be without any classes.

@megalomania1982
Copy link

@maxrails Thanks for an advice. It still says "null" :(
I don't want anything back. It's impossible?

@maxrails
Copy link

strange for me it shows nothing inside but what about css trick - 'li' tag shouldn't contain any classes when nothing inside - so maybe smth like this may help:

ul.ui-autocomplete
  li
    :display none
  li.ui-menu-item
    :display block

@megalomania1982
Copy link

@maxrails Thanks for an advice it didn't work either. Even if there are matching records, it won't show candidate but it shows thin rectangle.

@edsimpson
Copy link
Contributor

This variation on @maxrails solution works for me. Put the following in your ApplicationController.

def get_autocomplete_items(parameters)
    super(parameters).presence || [OpenStruct.new(id: '', parameters[:method].to_s => '')]
end

If you only want to replace the "no existing match" message with your own, then replace the second value in the OpenStruct constructor with a string containing the message you want.

If you want the menu to disappear when there are no matches, run the following after page load or if you're doing AJAX, after the response has rendered.

Coffeescript:

$(autocomplete_field_selector).on 'autocompleteresponse', (event, ui) ->
    $(this).autocomplete('close') if ui.content?[0].id.length == 0

Javascript:

$(autocomplete_field_selector).on('autocompleteresponse', function(event, ui) {
      var content;
      if (((content = ui.content) != null ? content[0].id.length : void 0) === 0) {
        $(this).autocomplete('close');
      }
  });

@megalomania1982
Copy link

@edsimpson Thanks man! It worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants