Hola. Llevo días dándole y no consigo clarificar el tema.
Tengo una vista con un formulario para entrar búsqueda arriba, y un paginador a continuación. Simplemente quiero que me actualice el paginador, según el resultado de la búsqueda. La búsqueda la lanzo a una función "cerca". La búsqueda la hace bien, pero no la puedo renderizar en la misma vista, sino en otra similar llamada cerca.ctp
Supongo que hace falta un requerimiento Ajax en el controlador, y un JS en la vista.
Alguien me puede orientar en enforcar el tema? Muchas gracias.
Las funciones del controlador son:
public function index()
{
$this->paginate = [
'contain' => ['Departaments'],
'sortWhitelist' => [
'Departaments.nom','titol', 'curs', 'tutor','autor','nota','claus'],
'limit' => 5,
'order' => [
'Departaments.nom' => 'ASC'
]
];
$this->set('tdrs', $this->paginate());
}
public function cerca()
{
$this->autoRender = false;
//if ($this->request->is(['patch', 'post', 'put','get'])) {
if ($this->request->is('ajax')) {
$cerca = $this->request->data['claus'];
$results = $this->Tdrs->find('all', array(
'conditions' => array('Tdrs.claus LIKE ' => '%' . $cerca . '%')
));
$this->paginate = [
'contain' => ['Departaments'],
'sortWhitelist' => [
'Departaments.nom','titol', 'curs', 'tutor','autor','nota','claus'],
'conditions' => array('Tdrs.claus LIKE ' => '%' . $cerca. '%'),
'limit' => 5,
'order' => [
'Departaments.nom' => 'ASC'
]
];
$this->set('tdrs', $this->paginate());
}
}
La vista, resumida: index.ctp
<?php use Cake\Routing\Router;
use Cake\View\Helper\UrlHelper; ?>
<h1>Llistat de TdRs</h1>
<table>
<tr>
<th>
<?php echo $this->Form->create('Tdrs');
echo $this->Form->input('claus'); ?>
</th>
<th>
<?php echo $this->Form->button(__('<span class = "glyphicon glyphicon-search "></span> Cerca'),['class'=>'btn btn-primary']);
echo $this->Form->end; ?>
</th>
</tr>
</table>
<BR>
<div>
<table class="table table-striped">
<tr>
<th><?= $this->Paginator->sort('Departaments.nom', 'Departament') ?></th>
<th><?= $this->Paginator->sort('curs', 'Curs') ?></th>
<th><?= $this->Paginator->sort('titol', 'Títol') ?></th>
<th><?= $this->Paginator->sort('autor', 'Autor/a') ?></th>
<th><?= $this->Paginator->sort('tutor', 'Tutor/a') ?></th>
<th><?= $this->Paginator->sort('nota', 'Qualificació') ?></th>
<th><?= $this->Paginator->sort('claus', 'Claus') ?></th>
<th>Veure</th>
<th>Modificar</th>
<th>Esborrar</th>
</tr>
<?php foreach ($tdrs as $recipe): ?>
<tr>
<td><?= h($recipe->departament->nom) ?> </td>
<td><?= h($recipe->curs) ?> </td>
<td><?= h($recipe->titol) ?> </td>
<td><?= h($recipe->autor) ?> </td>
<td><?= h($recipe->tutor) ?> </td>
<td><?= h($recipe->nota) ?> </td>
<td><?= h($recipe->claus) ?> </td>
<td><?= $this->Html->link('<span class = "glyphicon glyphicon-download-alt "></span> Veure', '/' . $recipe->fitxer_dir . '' . $recipe->fitxer, ['class' => 'btn btn-link', 'role' => 'button', 'escape' => false]); ?></td>
<td><?= $this->Html->link('Modificar', ['action' => 'edit', $recipe -> id]) ?> </td>
<td><?= $this->Form->postLink(
'Esborrar',
['action' => 'delete', $recipe->id],
['confirm' => 'Estàs segur/a?'])
?> </td>
</tr>
<?php endforeach; ?>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->prev('< ' . __('Anterior')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('Posterior') . ' >') ?>
</ul>
<p><?= $this->Paginator->first('< Principi')?></p>
</div>
<?php echo $this->Paginator->counter(
'Pàgina {{page}} de {{pages}}, mostrant {{current}} registres de
{{count}} en total, començant en el {{start}}, finalitzant en el {{end}}'
);
?>
</div>