Home > Web Front-end > JS Tutorial > body text

Using bootstrap-select control

PHPz
Release: 2018-10-15 15:42:35
Original
5629 people have browsed it

This control relies on the Bootstrap front-end framework;

For specific usage of this control, please see the above link;

One of the uses of this control is to search for the options of the select tag to obtain accurate options. After all, there may be many options;

Now I want to dynamically obtain the option data incoming from the background by typing keywords in the search bar, instead of reading all the data at once and then searching;

The specific implementation is as follows:

1. Introduce relevant files:

<head>
     <!-- 引入 Bootstrap样式 -->
      <link href="${pageContext.request.contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">
     <!-- bootstrap-select样式 -->
      <link href="${pageContext.request.contextPath}/resources/css/select/bootstrap-select.min.css" rel="stylesheet">

     <!-- jQuery (Bootstrap 依赖于 jQuery) -->
      <script src="${pageContext.request.contextPath}/resources/js/jquery-3.0.0.min.js"></script>

     <!-- Bootstrap -->
       <script src="${pageContext.request.contextPath}/resources/js/bootstrap.min.js"></script>

     <!-- Bootstrap-select 控件 -->
       <script src="${pageContext.request.contextPath}/resources/js/select/bootstrap-select.min.js"></script>
       <script src="${pageContext.request.contextPath}/resources/js/select/defaults-zh_CN.min.js"></script>

</head>
Copy after login

  2. Select tag

<form id="addForm" action="${pageContext.request.contextPath}/admin/authority/manager/saveAdd" method="post">
   <p class="form-group">    
       <p class="input-group">     
          <span class="input-group-addon" >管理员</span>
           <!--触发bootstrap-select需添加class="selectpicker" 搜索功能需添加data-live-search="true" -->
          <select data-size="6" class="form-control selectpicker show-tick" data-live-search="true" title="请输入管理员"  id="select-manager" name="managerName"                data-selectNameUrl="${pageContext.request.contextPath}/admin/authority/managerRole/getManagerByName" >    
          </select>
       </p>
   </p>
   <p class="form-group">    
       <p class="input-group">         
          <span class="input-group-addon" >角    色</span>
          <select  class="form-control selectpicker show-tick" data-size="6" data-live-search="true"  id="select-role"  name="roleName" title="请选择角色" > 
             <c:forEach var="selectRole" items="${selectRoles}" varStatus="statu">
                <option    value ="${selectRole.id}"  >${selectRole.roleName}</option>
             </c:forEach>
          </select>
       </p>
   </p>
</form>
Copy after login

Until this step , under the administrator's select tag, I did not add any tags, so the options are empty

Rendering:

## ​

What I want to achieve now is to trigger an event as soon as a character is typed in the search bar: dynamically obtain background data to add Tag;

## The problem we encounter now is that the search bar is generated by the bootstrap-select control, and we cannot know in advance how to select the search bar to trigger the event.

The solution is to call the developer mode of chrome and find the label generated by the bootstrap-select control, as shown below:

## Knowing the search bar related labels and attributes generated by bootstrap-select, you can select the Search bar trigger event;

3. Trigger event:

  <script type="text/javascript">
         
         $().ready(function(){          
               //键入字符触发事件:动态获得后台传入select选项数据 
              
              //请求的url
              var selectNameUrl = $("#select-manager").attr("data-selectNameUrl");              
              //选择得到搜索栏input,松开按键后触发事件
              $("#select-manager").prev().find(&#39;.bs-searchbox&#39;).find(&#39;input&#39;).keyup(function () {                   
                  //键入的值
                  var inputManagerName =$(&#39;#addForm .open input&#39;).val();                  //判定键入的值不为空,才调用ajax
                  if(inputManagerName != &#39;&#39;){
                      $.ajax({
                            type: &#39;Get&#39;,
                            url: selectNameUrl,
                            data: {                     //传递到后台的值                                managerName: inputManagerName
                            },
                            dataType: "Json",
                            success: function (Selectmanagers) {                                //清除select标签下旧的option签,根据新获得的数据重新添加option标签
                                $("#select-manager").empty();                                if (Selectmanagers != null) {               
                                    $.each(Selectmanagers, function (i,Selectmanager) {
                                        $("#select-manager").append(" <option value=\"" + Selectmanager.id + "\">" + Selectmanager.managerName + "</option>");
                                    })                                    //必不可少的刷新
                                    $("#select-manager").selectpicker(&#39;refresh&#39;);
                                   
                                }
                            }
                      })
                  }else 
                      //如果输入的字符为空,清除之前option标签
                      $("#select-manager").empty();
                      $("#select-manager").selectpicker(&#39;refresh&#39;);  
              });     
         });  </script>
Copy after login

The effect is as follows:

[Related video recommendations:

Bootstrap tutorial

The above is the detailed content of Using bootstrap-select control. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template