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

Enabling Autocomplete to return different values for lable and value #231

Open
abhimanyuma opened this issue Jun 20, 2013 · 4 comments
Open

Comments

@abhimanyuma
Copy link

I don't see why the json returned always has to have the same value for label and value. A lot of actions would be simplified if I could return a column of my choosing (for e.g id) in the value field and search column in the label field.

def json_for_autocomplete(items, method, extra_data=[])
      items.collect do |item|
        hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)}
        extra_data.each do |datum|
          hash[datum] = item.send(datum)
        end if extra_data
        # TODO: Come back to remove this if clause when test suite is better
        hash
      end
@michaelkirk
Copy link

To clarify, is your use case for selecting an association id by another field on the associated object?

E.g.

create_table :products do
  t.integer :category_id
end
class Product < ActiveRecord::Base
  belongs_to :category
end

create_table :categories do
  t.string :name
end
class Category < ActiveRecord::Base
  has_many :products
end

Then you could assign the products.category_id field by autocompleting on the category name?

@paulwalker
Copy link

This request has nothing to do with associations, I don't understand the confusion here. The problem is that the JSON returned from the autocomplete search has the same value for the "value" field as it does for the "label" field. The "value" field should be that of the object id by default and at least allow an option to choose which field to use.

Normally, users will want to submit the id value of the selected item, not the label value.

@manusajith
Copy link
Member

Although an option to choose the fields is very useful wouldn't changing the default behaviour break the existing applications ?

@bigtunacan
Copy link
Contributor

@paulwalker, I understand this could be a useful feature to have, however not as the default behavior. As @manusajith pointed out, this would break existing applications. If someone would like to add this functionality in a way that maintains backwards compatibility and issue a pull request we are certainly open to that.

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

No branches or pull requests

5 participants