简体   繁体   中英

Predefined parameters for Sonata list search

I need to set several predefined options for showing list of entities.

Sonata code:

class SubscriptionAdmin extends Admin {
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('created_at')
            ->add('status', 'doctrine_orm_choice', [], 'choice', ['choices' = ['active'=>'ecmo.enum.user.active', 'inactive'=>'ecmo.enum.user.active']
        ;
}

How i can set, that, by default, sonata show only active users, sorted by created_at ?

You could do this, setting the default values for the $datagridValues variable as below

class SubscriptionAdmin extends Admin
{   

   /**
    * Default Datagrid values
    *
    * @var array
    */
   protected $datagridValues = array (
           'status' => array ('type' => 1, 'value' => 1), // field status with value 1
           '_page' => 1, // Display the first page (default = 1)
           '_sort_order' => 'ASC', // Descendant ordering (default = 'ASC')
           '_sort_by' => 'name' // name of the ordered field (default = the model id field, if any)
      // the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
   );
}

Where 'status' should be substituted by the field that you want and you should specify the desired value, and 'type' value corresponds with the following:

1: =
2: >=
3: >
4: <=
5: <

That's to filter an integer field. In my case, I've used this without specifying type, and the value 1 is set as default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM