Solution:
In the Controller
I make a filter for each Checkbox
, in the case of the example below I will do only three:
Controller
$this->Filter->addFilters(
array(
'filter1' => array(
'Filtro1.descricao' => array(
'operator' => 'ILIKE',
'value' => array(
'before' => '%',
'after' => '%'
)
)
)
'filter2' => array(
'Filtro2.descricao' => array(
'operator' => 'ILIKE',
'value' => array(
'before' => '%',
'after' => '%'
)
)
)
'filter3' => array(
'Filtro3.descricao' => array(
'operator' => 'ILIKE',
'value' => array(
'before' => '%',
'after' => '%'
)
)
)
)
);
Then initialize the variables that will be used in the foreach
down below:
$conditions = array();
$vFiltro = array();
$filtro1 = '';
$filtro2 = '';
$filtro3 = '';
foreach ($this->Filter->getConditions() as $chave => $valor) {
if ($valor != '%0%') {
if ($chave == 'Filtro1.descricao ILIKE') {
$filtro1 = $valor;
}
if ($chave == 'Filtro2.descricao ILIKE') {
$filtro2 = $valor;
}
if ($chave == 'Filtro3.descricao ILIKE') {
$filtro3 = $valor;
}
}
}
Then if the filters are not empty I add each filter to the condition variable $conditions
to make the pagination.
if (!empty($filtro1)) {
$conditions[] = "Filtro1.descricao ILIKE '" . $filtro1 . "'";
} elseif (!empty($filtro2)) {
$conditions[] = "Filtro2.descricao ILIKE '" . $filtro2 . "'";
} elseif (!empty($filtro3)) {
$conditions[] = "Filtro3.descricao ILIKE '" . $filtro3 . "'";
} else {
$conditions[] = "";
}
After we make the pagination to show the items we want.
$this->Paginator->settings = array(
'fields' => array('Ose.id', 'user_nome',
'Status.descricao', 'Group.descricao'),
'joins' => array(
array('table' => 'groups',
'alias' => 'Group',
'type' => 'LEFT',
'conditions' => ['User.group_id = Group.id'],
),
array('table' => 'statuses',
'alias' => 'Status',
'type' => 'LEFT',
'conditions' => ['Status.id = Ose.status_id_destino'],
),
array('table' => 'statuses',
'alias' => 'Filtro1',
'type' => 'LEFT',
'conditions' => ['Filtro1.id = Ose.status_id_destino'],
),
array('table' => 'statuses',
'alias' => 'Filtro2',
'type' => 'LEFT',
'conditions' => ['Filtro2.id = Ose.status_id_destino'],
),
array('table' => 'statuses',
'alias' => 'Filtro3',
'type' => 'LEFT',
'conditions' => ['Filtro3.id = Ose.status_id_destino'],
),
'condition' => array($conditions),
'order' => array('Ose.id' => 'asc')
);
Above has the alias Ose
and the alias User
, I don’t do their relationship around here because it’s been done in Model
.
After i Seto the pagination values.
// Aqui é definido quais status serão exibidos
$i = [1, 2, 3];
$this->set(
'ose', $this->paginate(
'Ose', array(
'Ose.ativo' => 't%',
'Ose.status_id_destino' => $i
)
)
);
Then in the View
I’ll tell you what:
View
<?php
echo $this->Search->create();
echo $this->Search->input(
'filter1', array(
'id' => 'filter1',
'type' => 'hidden'
));
echo $this->Search->input(
'filter2', array(
'id' => 'filter2',
'type' => 'hidden'
));
echo $this->Search->input(
'filter3', array(
'id' => 'filter3',
'type' => 'hidden'
));
?>
And we end with the Snippet
below where by clicking on Checkbox
desired value is added in the text field exemplified in Snippet
, but will actually be included in the field input
above, as is done in the CAKEPHP
.
function filtro1(value) {
var filter = document.getElementById('filter1');
if (document.getElementById('1').checked) {
filter.value = value;
}
}
function filtro2(value) {
var filter = document.getElementById('filter2');
if (document.getElementById('2').checked) {
filter.value = value;
}
}
function filtro3(value) {
var filter = document.getElementById('filter3');
if (document.getElementById('3').checked) {
filter.value = value;
}
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js "
type="text/javascript"></script>
<input type="text" name="filter1" id="filter1">
<input type="text" name="filter2" id="filter2">
<input type="text" name="filter3" id="filter3">
<table border='1px' celspacing='0' colspacing='0'>
<tr>
<th>Aguardando embarque</th>
<th>Embarcado</th>
<th>Recebido!</th>
</tr>
<tr>
<td>
<input type="checkbox" name="filter1" onclick="filtro1(value)" id="1" value="Aguardando embarque">
</td>
<td>
<input type="checkbox" name="filter2" onclick="filtro2(value)" id="2" value="Embarcado">
</td>
<td>
<input type="checkbox" name="filter3" onclick="filtro3(value)" id="3" value="Recebido">
</td>
</tr>
</table>
<input type="submit" value="Filtrar" />