How to use http://loudev.com/ (multiselect.js) in PHP, or how to select items from a select Multiple

Asked

Viewed 100 times

0

<table name="tbListagem" id="tbListagem" class="table table-striped table-bordered">
   <thead>
      <tr class="info">
         <th>Código </th>
         <th>Nome </th>
         <th>Cotas Coloridas  </th>
         <th>Cotas Preto/Branco </th>
         <th>Política </th>
         <th>Situação </th>
         <th>Operações </th>
      </tr>
   </thead>
   <tbody>
      <?php foreach($result as $row){ ?>
      <tr>
         <td> <?=$row->ID ?> </td>
         <td> <?=$row->NOME?> </td>
         <td> <?=$row->QTDECOTASC?> </td>
         <td> <?=$row->QTDECOTASP?> </td>
         <td> <?=$row->POLNOME?> </td>
         <td> <?=$row->SITNOME?> </td>
         <td>
            <button title="Editar" id="btnEditar" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalFormEditaGru"
               data-macao="editarGru"
               data-mid="<?=$row->ID ?>"
               data-mnome="<?=$row->NOME ?>"
               data-mdescricao="<?=$row->DESCRICAO ?>"
               data-mpolitica="<?=$row->POLITICA ?>"
               data-msituacao="<?=$row->SITUACAO ?>"
               <?php
                  $Impressoras = array();
                  foreach($resultGrupoImpressora as $GruImp){
                    if ($GruImp->ID_GRUPO == $row->ID){
                         $Impressoras[] = $GruImp->ID_IMPRESSORA; ---Se printar $Impressoras RETORNARÁ algo assim Array( [0] => 1 [1] => 2 [2] => 3 )
                    }
                  }
                  ?>
               data-mimpressoras="<?=$Impressoras?>">
            <span class="glyphicon glyphicon-pencil"></span>
            </button>
            <?php print_r($Impressoras);?> ---printando(dependendo da posição) RETORNARÁ algo assim Array( [0] => 1 [1] => 2 [2] => 3 )
         </td>
      </tr>
      <?php } ?>
   </tbody>
</table>
<div class="modal fade" id="modalFormEditaGru" name="modalFormEditaGru" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
   <div class="modal-dialog" role="document">
      <div class="modal-content">
         <div class="modal-header">
            <h4 class="modal-title" id="exampleModalLabel">Titulo</h4>
         </div>
         <div class="modal-body">
            <form id="frEditaGru" name="frEditaGru" role="form" method="post" accept-charset="utf-8">
               <label style="color: #aa1111">Campos Obrigatórios</label>
               <input type="hidden" name="edId" id="edId"/>
               <input type="hidden" name="edAcao" id="edAcao" value=""/>
               <div class="form-group">
                  <label for="edNome" class="control-label">Nome </label>
                  <input type="text" class="form-control" id="edNome" name="edNome" placeholder="exemplo" autofocus required>
               </div>
               <div class="row">
                  <div class="form-group col-md-6">
                     <label for="edDescricao" class="control-label">Descrição </label>
                     <input type="text" class="form-control" id="edDescricao" name="edDescricao" placeholder="exemplo" required>
                  </div>
                  <div class="form-group col-md-6">
                     <label for="chkPolitica">Política</label>
                     <select id="chkPolitica" name="chkPolitica" class="form-control" required>
                        <option selected value="1">Sem Política</option>
                        <option value="2">A</option>
                        <option value="3">B</option>
                        <option value="4">C</option>
                        <option value="5">D</option>
                     </select>
                  </div>
                  <div class="form-group col-md-6">
                     <label for="chkSituacao">Situação</label>
                     <select id="chkSituacao" name="chkSituacao" class="form-control" required>
                        <option selected value="1">Ativo</option>
                        <option value="2">Inativo</option>
                     </select>
                  </div>
                  ---AQUI ESTA O CAMPO MULTIPLO
                  <div class="form-group  col-md-6">
                     <label for="chkImp">Impressora(s):</label>
                     <select multiple="multiple" id="chkImp" name="chkImp[]" class="form-control" required>
                        <?php foreach($resultImpressoras as $row){ ?>
                        <option value=" <?=$row->ID ?> "> <?=$row->NOME?></option>
                        <?php } ?>
                     </select>
                  </div>
                  ---AQUI ESTA O CAMPO MULTIPLO
               </div>
               <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                  <button type="submit" class="btn btn-primary" name="btnSalvar" id="btnSalvar" value="Salvar">Salvar</button>
               </div>
            </form>
         </div>
      </div>
   </div>
</div>

$(document).ready(function(){
    $('#modalFormEditaGru').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var Acao        = button.data('macao');
        var Id          = button.data('mid');
        var Nome        = button.data('mnome');
        var Descricao   = button.data('mdescricao');
        var Politica    = button.data('mpolitica');
        var Situacao    = button.data('msituacao');
        var Impressoras = button.data('mimpressoras');
        var modal = $(this);
        modal.find('.modal-title').text('Editando grupo: '+Id+' - '+Nome);
        modal.find('#edAcao').val(Acao);
        modal.find('#edId').val(Id);
        modal.find('#edNome').val(Nome);
        modal.find('#edDescricao').val(Descricao);
        modal.find('#chkPolitica').val(Politica);
        modal.find('#chkSituacao').val(Situacao);
        $('#chkImp').multiSelect('select', Impressoras);   ---AQUI O PROBLEMA, não traz as impressoras do array selecionadas
    });
});

My problem is that I can not leave as selected the items of Multiple select in my form from the $Printers array, just list all printers without any selected!

Thank you!

  • To be selected, you must put the "Selected" attribute in the option. That is, make a check of this list and comparing with the data previously saved in the database. In the documentation there is an example

  • Yes, I’ve tried doing an in_array in php but in this case it doesn’t work, because I have to list all the selected items for each group when opening the "frEditaGru" form. For this reason I use: $('#chkImp'). multiselect('select', Printers); but it is not working for some reason

  • I don’t understand where the $results variable comes from

1 answer

1


Here is a simple example to bring the selected options.

echo '<select multiple="multiple" id="chkImp" name="chkImp[]" class="form-control" required>'.PHP_EOL;

foreach($impressoras as $value):
    $isSelected = in_array($value,$impressorasCadastradas) ? selected : '';
    echo "<option value=\"$value\" in_array($value,$impressorasCadastradas) ? selected : '' Impressora id: $value>".PHP_EOL;
endforeach;
echo '</select>';

Browser other questions tagged

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