Filterrific gem-根据关联更新过滤器

时间:2018-12-14 13:35:18

标签: ruby-on-rails-4 filterrific

在我的应用程序中,我有CountryRegionCity

class Country < ActiveRecord::Base
  has_many :cities
  has_many :regions


class Region < ActiveRecord::Base
  belongs_to :country
  has_many :cities

class City < ActiveRecord::Base
  belongs_to :country
  belongs_to :region

这是我的模型,其中装有过滤器

scope :with_country_id, lambda { |country_ids|
  where(:country_id => [*country_ids])
}
delegate :name, to: :country, prefix: true

scope :with_region_id, lambda { |region_ids|
  where(:region_id => [*region_ids])
}
delegate :name, to: :region, prefix: true

scope :with_city_id, lambda { |city_ids|
  where(:city_id => [*city_ids])
}
delegate :name, to: :city, prefix: true

过滤器本身可以正常工作,但是我能否进行过滤,所以当用户选择country,我的with_region_idwith_city_id时,还可以根据他们的关联进行更新吗?

例如,我有国家/地区:美国,英国,当用户选择 UK 时,with_region_id会更新,并且仅显示属于的区域英国

感谢您的帮助,并表示感谢!

1 个答案:

答案 0 :(得分:0)

我使用动态菜单来解决此问题:

我更改了:

= f.collection_select :with_country_id,@filterrific.select_options[:with_country_id]
= f.collection_select :with_region_id,@filterrific.select_options[:with_region_id]

收件人:

= f.collection_select :with_country_id, Country.order(:name), :id, :name, {}, { class: 'class'}
= f.grouped_collection_select :with_region_id, Country.order(:name), :regions, :name, :id, :name, include_blank: true

JS

(function() {
jQuery(function() {
    var regions;

    regions = $('#filterrific_with_region_id').html();

    if($('#filterrific_with_country_id option').is(':selected')){
        var country, escaped_country, options;
        country = $('#filterrific_with_country_id :selected').text();
        escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
        options = $(regions).filter("optgroup[label='" + escaped_country + "']").html();

        if (options) {
            $('#filterrific_with_region_id').html('<option value="">(Select one)</option>'+options);

        } else {
            $('#filterrific_with_region_id').html('<option value="">Please Select a Country</option>');

        }

        return $('#filterrific_with_country_id').change(function() {
            var country, escaped_country, options;
            country = $('#filterrific_with_country_id :selected').text();
            escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
            console.log(escaped_country);
            options = $(regions).filter("optgroup[label='" + escaped_country + "']").html();
            if (options) {
                $('#filterrific_with_region_id').html('<option value="">(Select one)</option>'+options);

            } else {
                $('#filterrific_with_region_id').html('<option value="">Please Select a Country</option>');

            }
        });


    } else {
        console.warn('NOT SELECTED');

        return $('#filterrific_with_country_id').change(function() {
            var country, escaped_country, options;
            country = $('#filterrific_with_country_id :selected').text();
            escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
            console.log(escaped_country);
            options = $(regions).filter("optgroup[label='" + escaped_country + "']").html();
            if (options) {
                $('#filterrific_with_region_id').html('<option value="">(Select one)</option>'+options);

            } else {
                $('#filterrific_with_region_id').html('<option value="">Please Select a Country</option>');

            }
        });
    }

});