Set a selectpicker bootstrap

1

Assign a value to selectpicker. I tried to do it like the documentation but it has not worked for me, the point is that I get the options directly from the DB and I want to be able to add an additional option to the records. that is to say that it is like a <option value="0">Seleccione una categoría...</option> .

$('#idcategoria').selectpicker('val', 'Seleccione la categoría...');

If I put it this way it appears in the first option "Nothing selected", and as I mentioned before I want to appear: Select the category

HTML

<select id="idcategoria" name="idcategoria" class="form-control selectpicker" data-live-search="true" required>
</select>
    
asked by Anderviver 04.05.2018 в 16:17
source

2 answers

1

I solved it in the control file, like this:

$rspta = $rutas->select();//Carga variable con funcion select de categoría
echo '<option value="0">seleccione...</option>';
while ($reg = $rspta->fetch_object())//mientras exista respuesta
{
    echo '<option value=' . $reg->idRuta . '>' . $reg->nombreRuta .'</option>';
}
    
answered by 18.09.2018 в 17:14
0

If you want to put a non-selectable option, such as a guide, you can put it in the following way, assigning the disable and selected properties:

<select id="idcategoria" name="idcategoria" class="form-control selectpicker" data-live-search="true" required>

<option disabled selected value="0"> Seleccione la categoría... </option>
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>

If you also want it not to be seen, you can use:

<option style="display:none">

and will not be seen when you open the select.

Source: Answer by SO in English

    
answered by 07.05.2018 в 15:14