railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.9k stars 2.26k forks source link

Changing available values for autocomplete input issue. #2457

Closed SuperMasterBlasterLaser closed 3 years ago

SuperMasterBlasterLaser commented 9 years ago

Hello.

When model has belongs_to association, in create and edit action there will be combo box that helps to associate other model to this. I have found out that field for assigning models are more complicated than I thought.

According to source code of this field:

  1. It has hidden <select> tag that keeps all avilable values.
  2. Users sees <input> tag which has javascript for autocomplete
  3. When users clicks to drop-down button or types to input, it creates dynamically <ul> tag with available elements.
  4. When users choose one of options some kind of javascript method changes <select> tag's value.

I think this field uses JQueryUI's autocomplete library. It loads ALL available models that could be associated.

However, this gives new problem. For example:

If I have models Shop, Item and Category. They have these associations:

  1. Shop has_and_belongs_to_many Categories
  2. Shop has_many Items
  3. Category has_many Items

When Users creates his own Shop he associates multiple Categories. Then Users tries to create Item and he need to choose Shop and Category. Combobox shows ALL available Categories for Item creation.

I need to show only categories that has selected Shop. imagine if there are 1000 Categories and Shop has only 3 of them.

I have created my own ui.js javascript file in assets/javascripts/rails_admin/custom/ directory. Created AJAX functions that will change values of <select> tag by value of other <select> tag. However, it DOES NOT change available values for autcomplete.

I think script for autocomplete preloads all data when page is loaded and works independently from values of <select> tags.

Is it possible to reaload avaliable data for autocomplete input?

mshibuya commented 3 years ago

https://github.com/sferik/rails_admin/wiki/Associations-scoping will help.

SuperMasterBlasterLaser commented 3 years ago

@mshibuya LOL XD. After six (!) years I receive an answer.