Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

initial search multiple values for select filters #871

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Datatable/Filter/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sg\DatatablesBundle\Datatable\Filter;

use Sg\DatatablesBundle\Datatable\Extension\Select;
use Sg\DatatablesBundle\Datatable\OptionsTrait;

use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -124,7 +125,14 @@ public function configureOptions(OptionsResolver $resolver)

$resolver->setAllowedTypes('search_type', 'string');
$resolver->setAllowedTypes('search_column', array('null', 'string'));
$resolver->setAllowedTypes('initial_search', array('null', 'string'));

$allowedInitialSearchTypes = ['null', 'string'];
if($this instanceof Select2Filter or $this instanceof Select) {
// Add ability to set initial search as array only for Select & Select2 filters
$allowedInitialSearchTypes[] = 'array';
}

$resolver->setAllowedTypes('initial_search', $allowedInitialSearchTypes);
$resolver->setAllowedTypes('classes', array('null', 'string'));
$resolver->setAllowedTypes('cancel_button', 'bool');
$resolver->setAllowedTypes('placeholder', 'bool');
Expand Down Expand Up @@ -382,7 +390,7 @@ protected function getExpression(Composite $expr, QueryBuilder $qb, $searchType,
case 'bigint':
case 'smallint':
case 'boolean':
if ($searchValue == (string) (int) $searchValue) {
if ( $searchValue == (string) (int) $searchValue ) {
$searchValue = (int) $searchValue;
} else {
$incompatibleTypeOfField = true;
Expand Down
17 changes: 13 additions & 4 deletions Resources/views/datatable/initial_search.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#}
#}

"searchCols": [
{% for column in sg_datatables_view.columnBuilder.columns %}
{% if column.filter.initialSearch is defined and column.filter.initialSearch|length > 0 %}{"search" : "{{ column.filter.initialSearch }}"}{% else %}null{% endif %},
{% endfor %}
{% for column in sg_datatables_view.columnBuilder.columns %}
{% if column.filter.initialSearch is defined %}
{% if column.filter.initialSearch is iterable %}
{"search" : "{% for fi in column.filter.initialSearch %}{{ fi }},{% endfor %}"},
{% elseif column.filter.initialSearch|length > 0 %}
{"search" : "{{ column.filter.initialSearch }}"},
{% else %}
null,
{% endif %}
{% endif %}
{% endfor %}
]
10 changes: 9 additions & 1 deletion Resources/views/filter/select.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@

{% set filter_select_select_options %}
{% for key, name in column.filter.selectOptions %}
{% if column.filter.initialSearch is not same as(null) and column.filter.initialSearch is same as(key) %}
{% if column.filter.initialSearch is iterable %}
{% set selected = '' %}
{% for fi in column.filter.initialSearch %}
{% if fi is not same as(null) and fi is same as(key) %}
{% set selected = 'selected="selected"' %}
{% endif %}
{% endfor %}
<option value="{{ key }}" {{ selected }}>{{ name }}</option>
{% elseif column.filter.initialSearch is not same as(null) and column.filter.initialSearch is same as(key) %}
<option value="{{ key }}" selected="selected">{{ name }}</option>
{% else %}
<option value="{{ key }}">{{ name }}</option>
Expand Down