问题
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