Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding search functionality in select options using Bootstrap

Tags:

I have been trying to add search functionality in the select form using bootstrap. This is my code:

<!DOCTYPE html> <html lang="en"> <head>   <meta charset="utf-8">   <meta http-equiv="X-UA-Compatible" content="IE=edge">   <meta name="viewport" content="width=device-width, initial-scale=1">      <meta name="description" content="">   <meta name="author" content="">   <link rel="icon" href="http://getbootstrap.com/favicon.ico">    <title>Depots list</title>    <!-- Bootstrap core CSS -->   <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">    <!-- Latest compiled and minified bootstrap-select CSS -->   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->   <link href="http://getbootstrap.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">    <!-- Custom styles for this template -->   <link href="http://getbootstrap.com/examples/starter-template/starter-template.css" rel="stylesheet">   <link href="./css/st.css" rel="stylesheet">   <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->  <!--[if lt IE 9]><script src="http://getbootstrap.com/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->  <script src="http://getbootstrap.com/assets/js/ie-emulation-modes-warning.js"></script>   <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->  <!--[if lt IE 9]>  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>  <![endif]-->  </head>    <body>  <div class="container">    <div class="row">     <div class="col-xs-12">       <div class="box">         <!-- /.box-header -->         <div class="box-body">                   <form>                    <div class="form-group row">                    <label for="" class="col-sm-2 form-control-label">Country</label>                   <div class="col-sm-10">                     <select class="form-control selectpicker" id="select-country" placeholder="" data-live-search="true">                         <options>China</options>                         <options>Malaysia</options>                         <options>Singapore</options>                     </select>                   </div>                 </div>               </form>         </div><!-- /.box-body -->       </div><!-- /.box -->     </div><!-- /.col -->   </div><!-- /.row --> </div><!-- /.container --> 

I have problem with contradiction between form-control and selectpicker classes in select form. My code works only for form-control class.

For selection searches I have used this: https://developer.snapappointments.com/bootstrap-select/examples/#live-search

My question is: How can I use both of the form-control and selectpicker classes in select tag? I have used normal procedure to write the class name with a space. But it seems not to work.

like image 357
ni8mr Avatar asked Mar 15 '16 07:03

ni8mr


People also ask

How to add search in Select option with Bootstrap?

The “Hierarchy Select” is a well developed jQuery plugin for Bootstrap to make select dropdown with search box. It comes with built-in search feature to search from select list. You just need to define a list of words, then this plugin will do everything.

What is the use of bootstrap select?

Bootstrap Select is a form control that shows a collapsable list of different values that can be selected. This can be used for displaying forms or menus to the user. This article shows the methods by which a <select> element can be styled in Bootstrap, using both custom styles and bootstrap-select.


1 Answers

To get a search functionality you must set the data-tokens attribute for each options. Here is your code after adding it. Let me know if you need additional help.

$(function() {    $('.selectpicker').selectpicker();  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" rel="stylesheet" />          <div class="container">      <div class="row">      <div class="col-xs-12">        <div class="box">          <!-- /.box-header -->          <div class="box-body">            <form>              <div class="form-group row">                <label for="" class="col-sm-2 form-control-label">Country</label>                <div class="col-sm-10">                  <select class="form-control selectpicker" id="select-country" data-live-search="true">                  <option data-tokens="china">China</option>    <option data-tokens="malayasia">Malayasia</option>    <option data-tokens="singapore">Singapore</option>                  </select>                  </div>              </div>            </form>          </div>          <!-- /.box-body -->        </div>        <!-- /.box -->      </div>      <!-- /.col -->    </div>    <!-- /.row -->  </div>  <!-- /.container -->
like image 55
Rajshekar Reddy Avatar answered Sep 22 '22 06:09

Rajshekar Reddy