Symfony2 - 在 Sonata Admin 的元素列表中提供默认过滤器 [英] Symfony2 - Give a default filter in a list of elements of Sonata Admin
问题描述
我有一个 Vehicle 类型的元素列表,我用 Sonata Admin 展示了这些元素.我允许通过状态"字段过滤这些元素,但我希望在显示列表时只显示活动车辆,如果有人想查看非活动车辆,请使用过滤器并选择非活动状态.我想知道是否有人知道默认情况下使用 Sonata Admin 为元素列表应用过滤器的方法.
I have a list of elements of type Vehicle and I show these elements with Sonata Admin. I allow to filter these elements by the "status" field, but I want that, when the list is showed, only the active vehicles are showed, and if somebody wants to see the inactive vehicles, uses the filter and select the inactive status. I would like to know if somebody Knows the way to apply filters by default for a list of elements using Sonata Admin.
这是我的代码:
public function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('status')
;
}
protected function configureDatagridFilters(DatagridMapper $mapper)
{
$mapper
->add('name')
->add('status')
;
}
是否有任何选项可以添加到 configureDatagridFilters() 中的状态字段以实现此目标?其他选择?
Is there any option that can be added to the status field in configureDatagridFilters() to achieve this goal? Other options?
提前致谢.
推荐答案
你必须重写 $datagridValues
属性如下(对于 status > 0
如果它是一个整数) :
You have to override $datagridValues
property as following (for status > 0
if it's an integer) :
/**
* Default Datagrid values
*
* @var array
*/
protected $datagridValues = array (
'status' => array ('type' => 2, 'value' => 0), // type 2 : >
'_page' => 1, // Display the first page (default = 1)
'_sort_order' => 'DESC', // Descendant ordering (default = 'ASC')
'_sort_by' => 'id' // name of the ordered field (default = the model id field, if any)
// the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
);
这篇关于Symfony2 - 在 Sonata Admin 的元素列表中提供默认过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!