【问题标题】:Symfony2 - Give a default filter in a list of elements of Sonata AdminSymfony2 - 在 Sonata Admin 的元素列表中提供默认过滤器
【发布时间】:2013-04-19 06:22:29
【问题描述】:

我有一个 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')
     ;
 }

是否有任何选项可以添加到 configureDatagridFilters() 中的状态字段以实现此目标?其他选择?

提前致谢。

【问题讨论】:

    标签: symfony sonata-admin


    【解决方案1】:

    你必须重写$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'.
       );
    

    来源:Configure the default page and ordering in the list view

    【讨论】:

    • 非常感谢佩西!!我一直在互联网上寻找信息几个小时。我无法为您的答案投票,因为我没有足够的声誉,但我会尽快为您的答案投票。
    • 不客气!我在github.com/sonata-project/SonataAdminBundle/blob/master/… 找到了配置信息
    • 你先生,你让我头疼了!
    • 我不明白type是什么意思。你能解释一下吗?
    • 这里指的是过滤器的操作符,看行尾的注释 ;-) 值为“2”的类型表示“大于”
    【解决方案2】:

    你也可以用这个方法

        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();
        }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 这行不通。该值在我的过滤器中,但未应用过滤器。如果我点击Filter 没有任何反应,但是当我重置过滤器然后选择值时,它确实有效
      • 在我的情况下,我必须删除 'type' => EqualType::TYPE_IS_EQUAL
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-05-11
        • 2013-08-16
        • 1970-01-01
        • 2022-01-01
        • 2010-10-25
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多