Displaying filter from another view

Asked

Viewed 191 times

3

I have a View where customer data is shown, and at the click of a button, the user is redirected to a page that shows the moves of several customers, right on this page contains some filters. So what I need is that when this client clicks on the button and is redirected to the page with the filters, that the items related to it are filtered.

For example:

<center><strong>--Simulando--</strong></center><br>
<i>Ao clicar no botão, da `View`, abrirá a página `Nfse`, pertencente à outra controller.</i>
<hr>
<br><br>
<strong><i>VIEW:</i></strong> <br><br>

<table border='1'>
  <tr>
    <th>Cliente</th>
    <th>Cnpj</th>
    <th>Nfes emitidas</th>
  </tr>
  <tr>
    <td>XX SERVICOS</td>
    <td>00000000000100</td>
    <td><center><button>Nfes</button></center></td>
  </tr>
</table>
<br><br>
<i> Ao clicar no botão <button>Nfes</button> será redirecionado para ...</i>
<br><br>

<strong><i>Nfes:</i></strong><br><br>

<input type='text' placeholder='nr.nfe'>
<input type='text' placeholder='cnpj emitente' value='00000000000100'> <small><small>#Este campo deverá vir preenchido#</small></small>
<input type='text' placeholder='dt emissao'>
<input type='button' value='Filtrar'>

<br><br>
<i>Trazendo como resultado esses dados simulados!</i>
<br><br>
<table border='1'>
  <tr>
    <th>Nr.nfe</th>
    <th>cnpj emitente</th>
    <th>dt emissao</th>
  </tr>
  <tr>
    <td>11324</td>
    <td>00000000000100</td>
    <td>31/01/2017</td>
  </tr>
  <tr>
    <td>11323</td>
    <td>00000000000100</td>
    <td>28/01/2017</td>
  </tr>
  <tr>
    <td>11322</td>
    <td>00000000000100</td>
    <td>15/01/2017</td>
  </tr>
</table>

In the case of two controller different, I’m not able to find a solution for this, could collaborate with some idea or correction?

2 answers

3

From what I understand, you don’t know the helpers. They facilitate code implementation, and do everything for you, passing php data and assembling the html structure with this data. I’m sorry if I misinterpreted.

If you haven’t tried, you can make a link redirecting to the view of the other controller, passing some parameters referring to the client that should be filtered.

$html->link(/* o botão */, 
    ['controller' => /* o controller */, 
    'view' => /* a view */, 
     $parametro1, 
     $parametro2]);

The controller and view is where you want to redirect the client and if you have more parameters, just put more, separated by commas. Remember that these are the parameters that are available in your controller’s Function. I put it in a generic way because I don’t know exactly how your structure is.

1


I can collaborate with the following idea:

In his View replace the element <button>Nfes</button> by the Helper FormHelper::postLink. Which has the following format:

Formhelper::postLink( string $title, Mixed $url = null, array $options = array () );

Creates an HTML link, but accesses the URL using the POST method. Requires Javascript to be enabled in the browser.

This method creates an element <form>. If you want to use this method within an existing form, you must use the inline or block options so that the new form can be processed outside the existing form.[Book Cakephp, 2017, p. 136].

<!-- File: /app/View/ControllerUm/sua-view.ctp -->

<td class="actions">
    <?php 
        echo $this->Form->postLink(
            __('Nfes'),
            //mixed $url = null 
            array(
                'controller' => 'ControllerDois',
                'action' => 'actionOfWork', 
                ['client_id' => $client['Client']['id']]
            ),
            //array $options = array ()
            array(
                'inline' => true,
                'class' => 'demo'
                //'confirm': "Sua mensagem de confirmação, vem aqui se necessário",
            )
        ); 
    ?>
</td>

The button above will do a post by passing the client ID to the specified URL.

<!-- File: /app/Controller/ControllerUn/ControllerDois.php -->

 /**
 * A action no seu ControllerDois que vai receber o post.
 *
 * @return void
 */
 public function actionOfWork() {
    if ($this->request->is('post')) {
        $this->Client->id = $this->request->data['client_id'];
        if (!$this->Client->exists()) {
            throw new NotFoundException(__('Cliente inválido'));
        }

        /** 
         * Aqui você vai usar o seu algoritmo de trabalho da aplicação 
         */

        $dataClientFilter = $this->Client->find('list');
        $this->set(compact('dataClientFilter'));

        /** 
         * Em teoria o CakePHP vai mandar os dados filtrados para serem renderizados 
         * pela View: /app/View/ControllerDois/actionOfWork.ctp. 
         *
         */
    }
 }

If I understand your question correctly,!


Reference:
[Book Cakephp, 2017], Available in: Book Cakephp - Formhelper::postLink. Accessed: 05 Apr, 2017.

  • Perfect !! Exactly what I needed, works perfectly, put the URL that I wanted like this mixed => $url, passing to controller and the action destination, very good !!

  • 1

    @Marcoshenzel, OK! Thank you so much for the reward! But the most important thing is that it was useful for you! It feels really good! Success!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.