Dropdown with "default" value and "all" option

Asked

Viewed 132 times

2

Using Cakephp in my project, I created a dropdown to use to search the database by values and strings.

I need to create an "all" option to search for all results and to be the "default" option of this dropdown. How can I do that?

Code:

<?php echo $this->Form->create('RegistroHorario', array('class' => 'form-horizontal bucket-form', 'autocomplete' => 'off')); ?>  
<div class="col-lg-3">  
<?php echo $this->Form->select('RegistroHorario.cliente_id', $clientes, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>  
</div><div class="col-lg-3">  
<?php echo $this->Form->select('RegistroHorario.user_id', $usuarios, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>   
</div>  
<input type="submit" class="btn btn-success" value="Buscar" />  
<?php echo $this->Form->end(); ?>

1 answer

2


In the controller, you check if the method is POST. When not, you set the value directly in the property datafrom the Cakephp controller.

Behold:

<?php
    if ($this->Request->is(array('post', 'put'))) {
        // Operação para salvar os dados no banco
    } else {
        $this->data['RegistroHorario']['campo_select'] = "valor igual ao quer que seja o Default";
    }

That view on ONLY ENGLISH.

Updating

As there is also the need to add "all" to select, I believe that if a variable of type is passed Array to fill in the option of select, then it would be enough for you to do so:

$this->Form->select('RegistroHorario.usuario_id', array('valor' => 'Todos') + $usuarios)
  • But in the dropdown appear the information of the bank, I wanted to show the field "All" too, and that it came as default

  • 1

    I updated the answer. I think this should solve the problem.

  • 1

    It worked, I got it, thanks. I tried to score as useful, but I need 15 reputation points :/ when I hit again try to mark it.

Browser other questions tagged

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