工欲善其事,必先利其器css
對於不少非專業前端開發來講寫頁面是很是痛苦的,藉助框架或插件每每可以達到事半功倍的效果,本系列文章會介紹我在運維繫統開發過程當中用到的那些順手的前端插件,若是你是想寫XX管理系統的學生、或是須要獨自作Dashboard的後端工程師、亦或是像我這樣半吊子的開發加運維,那麼這個系列的文章你必定不要錯過前端
Bootstrap Dual Listbox是一款基於Bootstrap的雙向select選擇框控件,做爲對multiple select的擴展,使用起來很是簡單,功能也更強大jquery
項目Github地址:https://github.com/istvan-ujjmeszaros/bootstrap-duallistboxgit
須要用到的JS和CSS文件位於項目代碼下的dist目錄中,須要將這個目錄中的對應文件放入你的項目裏,這一步不贅述github
<!-- 加載bootstrap --> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <!-- 加載bootstrap-dualllistbox --> <link rel="stylesheet" href="static/bootstrap-duallistbox/bootstrap-duallistbox.css"> <script src="static/bootstrap-duallistbox/jquery.bootstrap-duallistbox.js"></script>
<select class="form-control" multiple="multiple" name="groups" size="10"> <option value="1">GroupA</option> <option selected value="2">GroupB</option> <option value="3">GroupC</option> <option value="4">GroupD</option> <option selected value="5">GroupE</option> <option value="6">GroupF</option> <option value="7">GroupG</option> </select> <script> var selectorx = $('select[name="groups"]').bootstrapDualListbox(); </script>
很是簡單,到這裏已經能夠正常使用這個控件了,更多的花樣接着往下看bootstrap
整個界面爲英文顯示,有默認提示,若是你想將提示改成中文或添加自定義的提示內容,那麼能夠經過以下配置後端
var selectorx = $('select[name="groups"]').bootstrapDualListbox({ nonSelectedListLabel: '未選擇的組', selectedListLabel: '已選擇的組', filterTextClear: '展現全部', filterPlaceHolder: '過濾搜索', moveSelectedLabel: "添加", moveAllLabel: '添加全部', removeSelectedLabel: "移除", removeAllLabel: '移除全部', infoText: '共{0}個組', infoTextFiltered: '搜索到{0}個組 ,共{1}個組', infoTextEmpty: '列表爲空', });
以上配置都比較簡單,對照中文就能知曉意思,不作過多解釋,另外有幾個支持的參數說明以下:app
infoText: 除了設置字符串外還可設置爲false
,當設置爲false
時可隱藏這段信息框架
showFilterInputs: 默認爲true,是否顯示filter過濾框運維
moveOnSelect: 默認爲true,點擊便會變動選項到對應的選擇框內,若是設置爲false則會在出現moveSelected
的箭頭須要點擊箭頭或者雙擊選項後才能變動選項到對應的選擇框
nonSelectedFilter: 未選中的默認過濾規則,能夠配置爲OPS-COFFEE-A
則未選中的框內只會顯示OPS-COFFEE-A
selectedFilter: 已選中的默認規則,與noSelectedFilter
相似
獲取已選擇的值
selectorx.val()
獲取select插件對象
selectorx.bootstrapDualListbox('getContainer')
刷新插件元素用戶界面
selectorx.bootstrapDualListbox('refresh');
刪除bootstrap-duallistbox插件,恢復select原樣
selectorx.bootstrapDualListbox('destroy');
動態添加select的option
selectorx.append('<option value="9" selected>ops-coffee.cn</option>'); selectorx.bootstrapDualListbox('refresh');
注意:上文中的全部selectorx都爲加載duallistbox時實例化的select對象
爲了方便你們學習,我寫了個完整的demo,你能夠在線查看效果或下載代碼應用到本身的項目中
在線Demo地址:https://demo.ops-coffee.cn/duallistbox/
Github源碼地址:https://github.com/ops-coffee/demo/tree/master/duallistbox