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>
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>
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
## 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: The effect is as follows: [Related video recommendations: The above is the detailed content of Using bootstrap-select control. For more information, please follow other related articles on the PHP Chinese website! <script type="text/javascript">
$().ready(function(){
//键入字符触发事件:动态获得后台传入select选项数据
//请求的url
var selectNameUrl = $("#select-manager").attr("data-selectNameUrl");
//选择得到搜索栏input,松开按键后触发事件
$("#select-manager").prev().find('.bs-searchbox').find('input').keyup(function () {
//键入的值
var inputManagerName =$('#addForm .open input').val(); //判定键入的值不为空,才调用ajax
if(inputManagerName != ''){
$.ajax({
type: 'Get',
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('refresh');
}
}
})
}else
//如果输入的字符为空,清除之前option标签
$("#select-manager").empty();
$("#select-manager").selectpicker('refresh');
});
}); </script>