Symfony2-在Sonata Admin的元素列表中提供默认筛选器
我有一个Vehicle类型的元素列表,我用Sonata Admin显示了这些元素。我允许通过“状态”字段过滤这些元素,但我希望在显示列表时,仅显示活动车辆,如果有人想查看非活动车辆,使用过滤器并选择非活动状态。我想知道是否有人知道在默认情况下使用Sonata Admin为元素列表应用过滤器的方法 这是我的密码:Symfony2-在Sonata Admin的元素列表中提供默认筛选器,symfony,sonata-admin,Symfony,Sonata Admin,我有一个Vehicle类型的元素列表,我用Sonata Admin显示了这些元素。我允许通过“状态”字段过滤这些元素,但我希望在显示列表时,仅显示活动车辆,如果有人想查看非活动车辆,使用过滤器并选择非活动状态。我想知道是否有人知道在默认情况下使用Sonata Admin为元素列表应用过滤器的方法 这是我的密码: public function configureListFields(ListMapper $listMapper) { $listMapper ->ad
public function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('status')
;
}
protected function configureDatagridFilters(DatagridMapper $mapper)
{
$mapper
->add('name')
->add('status')
;
}
是否有任何选项可以添加到configureDatagridFilters()中的状态字段以实现此目标?其他选择
提前感谢。您必须重写
$datagridValues
属性,如下所示(对于状态>0
,如果它是整数):
来源:您也可以使用此方法
public function getFilterParameters()
{
$this->datagridValues = array_merge(
array(
'status' => array (
'type' => 1,
'value' => 0
),
// exemple with date range
'updatedAt' => array(
'type' => 1,
'value' => array(
'start' => array(
'day' => date('j'),
'month' => date('m'),
'year' => date('Y')
),
'end' => array(
'day' => date('j'),
'month' => date('m'),
'year' => date('Y')
)
),
)
),
$this->datagridValues
);
return parent::getFilterParameters();
}
使用上述两种方法将打破过滤器的“重置”行为,因为我们总是强制过滤器按默认值进行过滤。对我来说,我认为最好的方法是使用getFilterParameters函数(因为我们可以在其中添加逻辑,而不是静态地添加值),并检查用户是否单击了“重置按钮”
另一种方法是使用createQuery和getPersistentParameters强制执行不可见过滤器。这种方法最好使用完全可定制的过滤器。请看我的文章:
非常感谢您的光临!!我在网上找了好几个小时的信息。我不能投票支持你的答案,因为我没有足够的声誉,但我会尽快投票支持你的答案。不客气!我在你那里找到了配置信息,先生,你帮我省去了一点麻烦!我不明白
类型
是什么意思。你能解释一下吗?这是指filter的运算符,请看第行末尾的注释;-)输入值为“2”表示“大于”,这不起作用。该值在我的筛选器中,但未应用筛选器。如果我点击过滤器,什么都不会发生,但是当我重置过滤器,然后选择值时,它确实起作用了。在我的例子中,我必须删除'type'=>EqualType::type_IS_EQUAL
public function getFilterParameters()
{
$this->datagridValues = array_merge(
array(
'status' => array (
'type' => 1,
'value' => 0
),
// exemple with date range
'updatedAt' => array(
'type' => 1,
'value' => array(
'start' => array(
'day' => date('j'),
'month' => date('m'),
'year' => date('Y')
),
'end' => array(
'day' => date('j'),
'month' => date('m'),
'year' => date('Y')
)
),
)
),
$this->datagridValues
);
return parent::getFilterParameters();
}
/**
* {@inheritdoc}
*/
public function getFilterParameters()
{
// build the values array
if ($this->hasRequest()) {
$reset = $this->request->query->get('filters') === 'reset';
if (!$reset) {
$this->datagridValues = array_merge(array(
'status' => array (
'type' => 1,
'value' => 0
),
),
$this->datagridValues
);
}
}
return parent::getFilterParameters();
}