Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set default 'add filter' match type #3001

Closed
dgleba opened this issue Mar 8, 2018 · 5 comments
Closed

How to set default 'add filter' match type #3001

dgleba opened this issue Mar 8, 2018 · 5 comments

Comments

@dgleba
Copy link

dgleba commented Mar 8, 2018

I have searched and read a lot but I can't find any hints on how to do this.

When I 'add filter' per the picture below the default match is ... which does nothing. See picture below.

I want to set the default matching to contains. I want contains to be showing and be used with no user interaction. If they want to change it from that they can. It will be the default though rather than ...

Can someone point to docs or tell me how to do that?

image

@ChrissyFlemingVTS
Copy link

We noticed a similar issue. From my internal team:

It used to be, when you added a search parameter in rails admin, it would default the parameter type to "contains."

Starting on the 26th of February (best I can tell), it left the parameter type blank, forcing the user to select one prior to searching:

This is a minor change but it is an extra step for an action that we do upwards of 50 times a day.

Can this be changed back so that it defaults to "contains"?

@mattruzicka
Copy link

Our team would also like to be able to specify a default match type, and not always "contains". So having the default configurable would be great. We may be able to look into adding this behavior, but no promises.

@coorasse
Copy link
Contributor

I solved it by defining the following in a config/initializers/rails_admin/main_helper.rb file:

require 'rails_admin/main_helper'

module RailsAdmin
  module MainHelper
    alias_method :original_ordered_filter_options, :ordered_filter_options

    def ordered_filter_options
      @ordered_filter_options ||= original_ordered_filter_options.tap do |ordered_filter_options|
        ordered_filter_options.each { |options| options[:operator] ||= :like if options[:type] == :string }
      end
    end
  end
end

I basically override the original method and set the operator to :like if is not set already.

@AlexBelov
Copy link

I was able to solve this by adding

$(document).on("click", "#filters a", function (e) {
  e.preventDefault();
  $.filters.append({
    label: $(this).data("field-label"),
    name: $(this).data("field-name"),
    type: $(this).data("field-type"),
    value: $(this).data("field-value"),
    operator: $(this).data("field-type") == 'string' ? 'like' : $(this).data("field-operator"),
    select_options: $(this).data("field-options"),
    required: $(this).data("field-required"),
    index: $.now().toString().slice(6, 11),
    datetimepicker_options: $(this).data("field-datetimepicker-options"),
  });
});

to the app/assets/javascripts/rails_admin/custom/filter-box.js

@codealchemy
Copy link
Contributor

Following #3318 (released in 2.1.0) you can set the default_filter_operator for a given field in your Rails Admin config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants