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

…衆ロ難τιáo~ 提交于 2019-12-11 08:21:26

问题


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:

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

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?


回答1:


I did it with JQuery:

//This function is used for setting all the filters
//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);
    }
});



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/21540098/symfony-set-sonata-admin-filter-operator-to-is-equal-to-as-default-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!