简体   繁体   English

如何在ActiveAdmin中的自定义过滤器中搜索子字符串?

[英]How to search for a substring in a custom filter in ActiveAdmin?

In the past: 以往:

A model User has a string filed first_name . 模型User的字符串为first_name If I write filter :first_name in ActiveAdmin model it would display a drop-down besides the input field with the options Contains , Equals , Starts with , Ends with . 如果我在ActiveAdmin模型中编写filter :first_name ,它将在输入字段旁边显示一个下拉列表,其中Contains选项ContainsEqualsStarts withEnds with So for examle if I choose Contains and write ik , it would find, for example Mike. 因此,例如,如果我选择Contains并编写ik ,它将找到,例如Mike。

Now: 现在:

If I use a custom filter I just get a large input, spanning across two columns, which only does exact search. 如果使用自定义过滤器,我将得到一个很大的输入,跨越两列,仅进行精确搜索。 I define my filter like so: 我这样定义过滤器:

  1. Create a scope in the model. 在模型中创建一个范围。
  2. Whitelist it with ransackable_scopes . 使用ransackable_scopes将其列入白名单。
  3. Add it to the admin model like so: filter :my_filter, as: :string . 像这样将其添加到管理模型中: filter :my_filter, as: :string

How do I get the functionality to seach for substrings? 如何获取功能来查找子字符串?

To search for substrings you must use the Ransack Search Predicate Syntax ie if you want search a substring on field first_name you must configure this way: 要搜索子字符串,您必须使用Ransack搜索谓词语法,即,如果要在字段first_name上搜索子字符串,则必须采用以下方式进行配置:

filter :first_name_cont, as: :string, label: "Name"

You can find more information here 您可以在这里找到更多信息

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM