Onema
Onema

Reputation: 7582

Symfony - Set Sonata Admin filter operator to "is equal to" as default option

In sonata admin I have a working filter in my admin class:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('client.name');
}

The filter will default to match "contains" (LIKE operator) like this:

enter image description here

I would like the filter to use "is equals to" (= operator) as the default option instead of "contains":

enter image description here

Unfortunately I couldn't find questions or answers related to this in SO, google, and the Sonata docs 9.4 filters is currently not documented.

How can I do this?

Upvotes: 2

Views: 1408

Answers (3)

Francis Gonzales
Francis Gonzales

Reputation: 495

I did it with JQuery:

////This function is used for settings all the filter
////in the default views
$("select").find("option").each(function() {
    var ArrayClasses = $(this).parent().attr('class').split(' ');
    var IsFilter = false;
    $.each(ArrayClasses, function(i, value){
        if (value === 'sonata-filter-option')
            IsFilter = true;
    });
    if ($(this).val() === '3' && IsFilter) {
        var HtmlStr = "<span class='select2-chosen'>is iqual to</span>" +
                      "<abbr class='select2-search-choice-close'></abbr>" +
                      "<span class='select2-arrow'><b></b></span></a>";
        $(this).attr("selected", "selected");
        var WrapId = $(this).parent().prev().attr('id');
        $("#" + WrapId + " a.select2-choice").html(HtmlStr);
    }
});

Upvotes: 1

Jithin
Jithin

Reputation: 2604

Post after a long time. You can override default filter options by overriding the variable $datagridValues

protected $datagridValues = array(
        'email' => array(
            'type' => 3,
            'value' => ''
        )
    );

'type' => 3 means its equal to

Upvotes: 0

Jithin
Jithin

Reputation: 2604

You can override default filter options by overriding the variable $datagridValues

protected $datagridValues = array(
        'email' => array(
            'type' => 3,
            'value' => ''
        )
    );

'type' => 3 means its equal to

Upvotes: 1

Related Questions