14

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.

Here is my code:

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('status')
    ;
}

protected function configureDatagridFilters(DatagridMapper $mapper)
 {
     $mapper
         ->add('name')
         ->add('status')
     ;
 }

Is there any option that can be added to the status field in configureDatagridFilters() to achieve this goal? Other options?

Thanks in advance.

4

5 回答 5

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'.
   );

来源:在列表视图中配置默认​​页面和排序

于 2013-04-25T11:41:20.240 回答
11

你也可以使用这个方法

    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();
    }
于 2013-05-03T14:49:48.907 回答
7

使用上述两种建议的方法都会破坏过滤器的“重置”行为,因为我们总是强制过滤器按默认值过滤。对我来说,我认为最好的方法是使用 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();
}
于 2016-05-19T13:54:57.887 回答
1

另一种方法是使用 createQuery 和 getPersistentParameters 来强制执行不可见过滤器。这种方法最好有完全可定制的过滤器。在这里查看我的文章:

http://www.theodo.fr/blog/2016/09/sonata-for-symfony-hide-your-filters/

于 2016-09-30T14:24:11.857 回答
0

从 sonata-admin 4.0 开始,该函数getFilterParameters()被标记为 final 并且 $datagridValues 不再存在。

所以你需要重写这个configureDefaultFilterValues()函数

protected function configureDefaultFilterValues(array &$filterValues): void
{
    $filterValues['foo'] = [
        'type'  => ContainsOperatorType::TYPE_CONTAINS,
        'value' => 'bar',
    ];
}

更多详细信息:https ://symfony.com/bundles/SonataAdminBundle/current/reference/action_list.html#default-filters

于 2022-02-28T09:26:42.790 回答