Air*_*ram 14 symfony sonata-admin
我有一个Vehicle类型的元素列表,我用Sonata Admin显示这些元素.我允许通过"状态"字段过滤这些元素,但是我想要,当显示列表时,只显示活动车辆,如果有人想看到不活动的车辆,则使用过滤器并选择非活动状态.我想知道是否有人知道如何使用Sonata Admin默认为过滤元素列表应用过滤器.
这是我的代码:
public function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('status')
;
}
protected function configureDatagridFilters(DatagridMapper $mapper)
{
$mapper
->add('name')
->add('status')
;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何选项可以添加到configureDatagridFilters()中的状态字段以实现此目标?其他选择?
提前致谢.
Alt*_*PHP 21
你必须覆盖$datagridValues属性如下(status > 0如果它是一个整数):
/**
* 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'.
);
Run Code Online (Sandbox Code Playgroud)
source:在列表视图中配置默认页面和排序
Qui*_*che 10
您也可以使用此方法
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();
}
Run Code Online (Sandbox Code Playgroud)
使用上述两种方法都会破坏过滤器"重置"行为,因为我们总是强制过滤器按默认值过滤.对我来说,我认为最好的方法是使用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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13830 次 |
| 最近记录: |