ruby - 根据与 Filterrific 的关联更新过滤器
问题描述
在我的应用程序中,我有Country
, Region
& City
。
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_id
&with_city_id
也会根据他们的关联进行更新?
例如,当用户选择UK时,我有国家: USA, UK,得到更新并且只显示属于UK的地区。with_region_id
解决方案
我使用动态菜单来解决这个问题:
我变了:
= 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>');
}
});
}
});
推荐阅读
- prolog - Prolog 中按值结果传递如何工作的示例
- oracle - 我对 oracle sql developer 有一个问题,包括一个 with 函数不起作用
- java - 按名称比较两个对象列表并获取不同对象的列表
- google-app-engine - 在哪里以及如何应用用户权限并删除使任何用户都能够访问 GCP 的配置?
- angular - font-awesome 图标在 angular 10+ 中不起作用
- javascript - 使用 ReactMemo 渲染异步值
- python - Python:与非 Python 用户共享交互式绘图(离线)
- java - 我如何在 listview android 中使用我的 JsonArray?
- powerbi - DAX Power BI - 如何计算表中出现超过 5 次的 ID
- r - 如何使用 ifelse 对一系列变量进行分组