Closed vazgen closed 12 years ago
any news on this?
no
You can modify the query string for the list action, and override all redirects in the controller
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...
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
thanks
great!!
Can you provide an example when using a field with type date, please?
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();
}
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?
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'.
);
Though I don't understand what the type
is needed for.
@webdevilopers The type
is here to specify what comparison will be done with the value
, for example: grater than
, lower than
, contains
, not contains
.
Thanks @pulzarraider . In which class are the types defined?
You can find them in SonataCoreBundle/Form/Type.
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
?
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)
);
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),
);
@webdevilopers each filter can have different operators. This is configured with the operator_type
option.
@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
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.
This issue thread helped me. Thanks.
Please how to do that in 2023 ? The getFilterParameters is now final
just a guess but configureFilterParameters
?
Hello,
Is it possible to set default value for filter? I did not found in documentation as will as from source code analysis.
Thanks.