sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Default value for filter #556

Closed vazgen closed 12 years ago

vazgen commented 12 years ago

Hello,

Is it possible to set default value for filter? I did not found in documentation as will as from source code analysis.

Thanks.

toooni commented 12 years ago

any news on this?

vazgen commented 12 years ago

no

worenga commented 12 years ago

You can modify the query string for the list action, and override all redirects in the controller

toooni commented 12 years ago

can you explain how you mean "override all redirects in the controller" ? I already modified the createQuery method.. and i get the correct entries in the list now.. but when i do this, there is no filter set...

PoisonousJohn commented 12 years ago

Admin class:

<?php
    public function getFilterParameters()
    {
        $this->datagridValues = array_merge(array(
                'parent' => array(
                    'value' => '33',
                )
            ),
            $this->datagridValues

        );
        return parent::getFilterParameters();
    }

in this example parent is a field name. Also you can add 'type' index along with value. Type value should match to the filter requirements

vazgen commented 12 years ago

thanks

jhon-chavarria commented 12 years ago

great!!

tiagojsag commented 12 years ago

Can you provide an example when using a field with type date, please?

tiagojsag commented 12 years ago

Found a solution. This example filter only shows entries with dates today and after:

public function getFilterParameters()
    {
        $this->datagridValues = array_merge(array(
                'startdate' => array(
                        'type' => 1,
                        'value' => array(
                                'date' => array('year' => date('Y'), 'month' => date('n'), 'day' => date('j')),
                                'time' => array('hour' => 0, 'minute' => 0)
                        ),
                )
        ),
                $this->datagridValues

        );

        return parent::getFilterParameters();
    }
Sydney-o9 commented 11 years ago

The filter doesn't seem to work if I set false for a boolean:

  public function getFilterParameters()
    {
        $this->datagridValues = array_merge(array(
                'isEnabled' => array(
                    'value' => false,
                )
            ),
            $this->datagridValues

        );

        return parent::getFilterParameters();
    }

but it does work if I change value => 2. Maybe I am doing something wrong?

webdevilopers commented 10 years ago

I have also seen this solution:

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

See here: http://stackoverflow.com/questions/16213361/symfony2-give-a-default-filter-in-a-list-of-elements-of-sonata-admin

Though I don't understand what the type is needed for.

pulzarraider commented 10 years ago

@webdevilopers The type is here to specify what comparison will be done with the value, for example: grater than, lower than, contains, not contains.

webdevilopers commented 10 years ago

Thanks @pulzarraider . In which class are the types defined?

pulzarraider commented 10 years ago

You can find them in SonataCoreBundle/Form/Type.

webdevilopers commented 10 years ago

So does the type in my example relate to the constants in the class e.g.:

   /**
    * 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)
   );

relates to https://github.com/sonata-project/SonataCoreBundle/blob/master/Form/Type/EqualType.php

 const TYPE_IS_EQUAL = 1;
const TYPE_IS_NOT_EQUAL = 2;

Then how should the status filter know he should check the equalType?

webdevilopers commented 10 years ago

Does the type in my predefined filter always point to the https://github.com/sonata-project/SonataCoreBundle/blob/master/Form/Type/EqualType.php class so that it only understands:

const TYPE_IS_EQUAL = 1;
const TYPE_IS_NOT_EQUAL = 2;

OR is the class used for the type depending on the type of the Entity attribute (e.g. string, date etc.) @pulzarraider ?

So for insance this could be valid configuration too:


use SonataCoreBundle/blob/master/Form/Type/EqualType.php;
use SonataCoreBundle/blob/master/Form/Type/BooleanType;

   /**
    * Default Datagrid values
    *
    * @var array
    */
   protected $datagridValues = array (
           'simple_string' => array ('type' => EqualType::TYPE_IS_EQUAL, 'value' => 'foo'),
           'simple_choice' => array ('type' => BooleanType::TYPE_YES, 'value' => 'yes'),
           '_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)
   );
johnpez commented 10 years ago

I'm experiencing the same issue as @Sydney-o9

Defaults work fine for boolean field if set to true, but if set to false, it is ignored.

protected $datagridValues = array(
    '_page' => 1,
    '_sort_order' => 'ASC',
    '_sort_by' => 'id', 
    'processed' => array ('type' => 2, 'value' => false),
);
pulzarraider commented 10 years ago

@webdevilopers each filter can have different operators. This is configured with the operator_type option.

webdevilopers commented 9 years ago

@johnpez Just stumbled upon your issue.

When you submi the filters you will recognize that instead of a boolean true / false or 0 / 1 it will submit 1 for true and 2 for false.

These values relate to the class I referenced above: https://github.com/sonata-project/SonataCoreBundle/blob/master/Form/Type/EqualType.php

This should be the official solution:

use SonataCoreBundle/blob/master/Form/Type/EqualType.php;
use SonataCoreBundle/blob/master/Form/Type/BooleanType;

    public function getFilterParameters()
    {
        $this->datagridValues = array_merge(array(
                'enabled' => array (
                    'type'  => EqualType::TYPE_IS_EQUAL,
                    'value' => BooleanType::TYPE_YES
                )
            ), $this->datagridValues);

        // Filter not displayed?
        $this->datagridValues = array_merge(array(
                'locked' => array (
                    'type'  => EqualType::TYPE_IS_EQUAL,
                    'value' => BooleanType::TYPE_NO
                )
            ), $this->datagridValues);

        return parent::getFilterParameters();
    }

Correct, @pulzarraider ? I think these examples should be added to the docs too as suggested in https://github.com/sonata-project/SonataAdminBundle/issues/2529

stetodd commented 9 years ago

When I was trying to get this working I had to do the following:

use Sonata\CoreBundle\Form\Type\EqualType;
use Sonata\CoreBundle\Form\Type\BooleanType;

public function getFilterParameters()
    {
        $parameters = parent::getFilterParameters();

        $parameters['completed'] = array (
            'type' => EqualType::TYPE_IS_EQUAL,
            'value' => BooleanType::TYPE_NO
        );

        return $parameters;
    }

Merging with $this->datagridValues didn't work as expected.

Zuhayer commented 7 years ago

This issue thread helped me. Thanks.

ec-ecss commented 1 year ago

Please how to do that in 2023 ? The getFilterParameters is now final

haivala commented 1 year ago

just a guess but configureFilterParameters?