Solution:
In% of% I make a filter for each Controller
, in the case of the example below I will only do 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 I initialize the variables that will be used in Checkbox
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;
}
}
}
Next, if the filters are not empty I add each filter in the% conditions variable foreach
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 paginate 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 is the alias $conditions
and the alias Ose
, I do not make their relationship here, since it has already been done in User
.
After I hedge 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 Model
we do the following:
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 View
below where when we click on the desired Snippet
, the value is added in the text field sampled in Checkbox
, but in fact it will be included in the Snippet
field above, as it is done in input
.
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" />