zmeul
zmeul

Reputation: 401

bootstrap multiselect keep selection on change

I have a dropdown box and i am using bootstrap multiselect for checkbox. can you please tell me (or point me to some docs) how can i keep selection after page reload? user checks option 1,3 and push search, page displays search but selected options must be kept. thx.

bootstrap multiselect version: 0.9.8

select option

<select name="people[]" id="multiselect" multiple="multiple">
<option value ="Val 1" >    Val 1</option>
<option value ="Val 2" >    Val 2</option>
<option value ="Val 3" >    Val 3</option>
</select>

JavaScript

 <script type="text/javascript">
      $(document).ready(function() {
          $('#people').multiselect(
              {
                  enableFiltering: true,
                  filterBehavior: 'both',
                  maxHeight: 200,
                  buttonWidth: '400px',
                  enableCaseInsensitiveFiltering: true,
                  nSelectedText: ' - selected values'
              }
          );
      });
 </script>

Upvotes: 0

Views: 8925

Answers (1)

zmeul
zmeul

Reputation: 401

I managed to resolve like this.

I created an array where I keep all the options.

<?php 
$optag = array(1=>'Option 1', 2=>'Option 2', 3=>'Option 3');
$reqag = $_REQUEST['people'];
?>

and the select

<select name="agent[]" id="multiselect" multiple="multiple">
  <?php foreach ( $optag as $i=>$opt ) : ?>
    <option value="<?php echo $opt ?>"<?php if(isset($reqag) && in_array($opt, $reqag)) echo '   selected'; ?>><?php echo $opt ?></option>
  <?php endforeach; ?>
</select>

JavaScript part

<script type="text/javascript">
  $(document).ready(function() {
      $('#people').multiselect(
          {
              enableFiltering: true,
              filterBehavior: 'both',
              maxHeight: 200,
              buttonWidth: '400px',
              enableCaseInsensitiveFiltering: true,
              nSelectedText: ' - selected values'
          }
      );
  });
 </script>

Upvotes: 0

Related Questions