[英]Custom filter in ActiveAdmin
How do I implement custom filtering in ActiveAdmin? 如何在ActiveAdmin中实现自定义过滤? It should be based on ActiveRecord conditions.
它应基于ActiveRecord条件。
Eg I have 2 models: Product and Category 例如,我有2个型号:产品和类别
class Product < ActiveRecord::Base
belongs_to :category
scope :in_category, ->(category_id) { where(category_id: Category.find(category_id).descendants.pluck(:id)) }
end
class Category < ActiveRecord::Base
has_many :products
acts_as_nested_set
end
ActiveAdmin.register Product do
filter :category
end
Categories are hierarchical. 类别是分层的。
When I enter category in filter I should see all products of that category and it's descendant categories (in_category scope in Product model). 当我在过滤器中输入类别时,我应该看到该类别的所有产品及其后代类别(产品模型中的in_category范围)。
Now ActiveAdmin uses ransack instead of metasearch and oldest approaches does not work. 现在,ActiveAdmin使用ransack代替元搜索,并且最旧的方法不起作用。
Solution for my example. 我的示例的解决方案。
In Product model: 在产品型号中:
ransacker :containing_category,
:formatter => ->(v) { ids = Category.find(v).self_and_descendants.pluck(:id);
ids.present? ? ids : nil } do |product|
product.table[:category_id]
end
Filter: 过滤:
filter :containing_category_in,
as: :select,
label: 'Category',
collection: Category.all
Here is article how to create a custom column on index page in ActiveAdmin and add custom filter based on Ransack: 以下是本文介绍如何在ActiveAdmin中的索引页上创建自定义列以及如何基于Ransack添加自定义过滤器:
http://codeonhill.com/activeadmin-custom-column-and-its-filter/ http://codeonhill.com/activeadmin-custom-column-and-its-filter/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.