Browse Source

Add static filter examples

webDEVILopers 10 years ago
parent
commit
3bb9f3067e
1 changed files with 57 additions and 1 deletions
  1. 57 1
      Resources/doc/reference/action_list.rst

+ 57 - 1
Resources/doc/reference/action_list.rst

@@ -260,10 +260,66 @@ To make the filter always visible (even when it is inactive), set the parameter
         ;
     }
 
+Default filters
+^^^^^^^^^^^^^^^
+
+Default filters can be added to the Datagrid Values by overriding the `$datagridValues` property which is also used for default sorting.
+A filter has a `value` and an optional `type`. If no `type` is given the default type `is equal` is used.
+
+.. code-block:: php
+
+    protected $datagridValues = array(
+        '_page' => 1,
+        '_sort_order' => 'ASC',
+        '_sort_by' => 'id', 
+        'foo' => array('value' => 'bar')
+    );
+
+Available types are represented through Classes which can be found here:
+https://github.com/sonata-project/SonataCoreBundle/tree/master/Form/Type
+
+Types like `equal` and `boolean` use constants to assign a choice of `type` to an `integer` for its `value`:
+
+.. code-block:: php
+
+    class EqualType extends AbstractType
+    {
+        const TYPE_IS_EQUAL = 1;
+        const TYPE_IS_NOT_EQUAL = 2;
+    }
+
+The integers are then passed in the URL of the list action e.g. `/admin/user/user/list?filter[enabled][type]=1&filter[enabled][value]=1`.
+This is an example using these constants for an `boolean` type:
+
+.. code-block:: php
+
+use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
+use SonataCoreBundle/blob/master/Form/Type/EqualType.php;
+use SonataCoreBundle/blob/master/Form/Type/BooleanType;
+
+class UserAdmin extends SonataUserAdmin
+{
+    protected $datagridValues = array(
+        'enabled' => array(
+            'type'  => EqualType::TYPE_IS_EQUAL, // => 1
+            'value' => BooleanType::TYPE_YES     // => 1
+        )
+    );
+}
+
+Please note that setting a `false` value on a the `boolean` type will not work since the type expects a `2` `value` as defined in the class constants:
+
+.. code-block:: php
+
+    class BooleanType extends AbstractType
+    {
+        const TYPE_YES = 1;
+        const TYPE_NO = 2;
+    }
+    
 To do:
 
 - basic filter configuration and options
-- how to set default filter values
 - targeting submodel fields using dot-separated notation
 - advanced filter options (global_search)