I have the index template, there I have a select of the users to filter the grid with the user selected, but when I click on the button to execute the query it doesn’t do anything.
This is the code for index action;
public function index()
{
$this->paginate = [
'contain' => ['Users'],
];
if ($this->request->is('post') && !empty($this->request->getData('user_id'))) {
$observations = $this->paginate($this->Observations->find('all')->where(['user_id' => $this->request->getData('user_id')]));
}else{
$observations = $this->paginate($this->Observations->find('all'));
}
$users = $this->Observations->Users->find('list', ['limit' => 200]);
$this->set(compact('observations', 'users'));
}
And this is the code for index template:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Observation[]|\Cake\Collection\CollectionInterface $observations
*/
?>
<div class="observations index content">
<?= $this->Html->link(__('Agregar Observación'), ['action' => 'add'], ['class' => 'button float-right']) ?>
<div class="observations form content">
<?= $this->Form->create($observations) ?>
<fieldset>
<legend><?= __('Filtro') ?></legend>
<?php
echo $this->Form->control('user_id', ['label' => 'Usuario', 'options' => $users, 'empty' => 'Seleccione un usuario']);
?>
</fieldset>
<?= $this->Form->button(__('Consultar')) ?>
<?= $this->Form->end() ?>
</div>
<h3><?= __('Observations') ?></h3>
<div class="table-responsive">
<table>
<thead>
<tr>
<th><?= $this->Paginator->sort('user_id', 'Usuario') ?></th>
<th><?= $this->Paginator->sort('observation', 'Observación') ?></th>
<th><?= $this->Paginator->sort('created', 'Creado') ?></th>
<th><?= $this->Paginator->sort('modified', 'Modificado') ?></th>
<th class="actions"><?= __('Acciones') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($observations as $observation): ?>
<tr>
<td><?= $observation->has('user') ? $this->Html->link($observation->user->username, ['controller' => 'Users', 'action' => 'view', $observation->user->id]) : '' ?></td>
<td><?= h($observation->observation) ?></td>
<td><?= h($observation->created) ?></td>
<td><?= h($observation->modified) ?></td>
<td class="actions">
<?= $this->Html->link(__('Ver'), ['action' => 'view', $observation->id]) ?>
<?= $this->Form->postLink(__('Eliminar'), ['action' => 'delete', $observation->id], ['confirm' => __('¿Desea eliminar esta observación?', $observation->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('Primera')) ?>
<?= $this->Paginator->prev('< ' . __('Anterior')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('Siguiente') . ' >') ?>
<?= $this->Paginator->last(__('Última') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(__('Página {{page}} de {{pages}}, mostrando {{current}} registro(s) de {{count}} total')) ?></p>
</div>
</div>