Recargar paginador después de una búsqueda

40 views
Skip to first unread message

Lluís Palacín

unread,
Jan 30, 2016, 11:23:58 AM1/30/16
to CakePHP en Español
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.
Tambíen he probado con el Plugin para Ajax Deuromark, pero aún me aclaro menos.  https://github.com/dereuromark/cakephp-ajax
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>



cel...@pilotoautomatico.mx

unread,
Feb 12, 2016, 4:27:36 AM2/12/16
to CakePHP en Español
Hola en una misma función puedes hacer la busqueda y mostrar el resultado paginado. Igual cuando des a la pagina siguiente debes mandar por la url el filtro. Eso fue lo más dificil para mi. Justo hasta el principio de la página pones
 <?php if(isset($this->data)){$paginator->options(array('url' => $this->data));} ?>

Espero que ya hayas solucionado tu problema! Saludos!!

Lluís Palacín

unread,
Apr 20, 2016, 2:55:29 AM4/20/16
to CakePHP en Español
Muchas gracias. Lo probaré en cuanto pueda.
Ya te diré algo.
Reply all
Reply to author
Forward
0 new messages