bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實(shí)例
用select2插件,實(shí)現(xiàn)以下這個(gè)選擇框:
1、html代碼
<div class="form-group" style='display:none;' id='preParamGroup'> <label for="inputEmail3" class="col-sm-2 control-label">預(yù)定義參數(shù)</label> <div class="col-sm-8"> <select class="js-states form-control" id="preParamDefine" multiple="multiple" style="width: 100%"></select> </div> </div>
2、js代碼
$("#preParamDefine").select2({ //data: data, placeholder:'請選擇',//默認(rèn)文字提示 tags: true,//允許手動(dòng)添加 allowClear: true,//允許清空 ajax: { url: '/jgwork/param_select', type:'GET', dataType: 'json', data: function(){ return {'projectId':$('#projectSel').val()}}, processResults: function (data) { return { results: data.result }; } } })
用ajax從服務(wù)端獲取數(shù)據(jù),在processResult里來返回?cái)?shù)據(jù)
3、服務(wù)端代碼
服務(wù)端返回的數(shù)據(jù)格式如下:
data = [ { 'text': 'enhancement' , 'children':[ { 'id': 1, 'text': 'bug','parent':'enhancement' }, { 'id': 2, 'text': 'duplicate' ,'parent':'enhancement'}, { 'id': 3, 'text': 'invalid' ,'parent':'enhancement'}, { 'id': 4, 'text': 'wontfix' ,'parent':'enhancement'} ] } ]
代碼:
proId = request.GET.get('projectId','') paramList = [param.show_table() for param in paramDefine.objects.filter(proid = proId)] data = [] index = 1 for item in paramList: childList = [] for i in item['paramValue'].split(','): childList.append({ 'id': index, 'text': i, 'param': item['paramName'] }) #生成children字段列表 index += 1 data.append({ 'text': item['paramName'], 'children': childList }) return JsonResponse({'result':data})
這里注意,index不能從0開始,不然生成的id有一個(gè)為0,會(huì)導(dǎo)致這個(gè)選項(xiàng)無法選取,因?yàn)樵趕elect2中id=0有特殊意義
以上這篇bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- BootstrapTable refresh 方法使用實(shí)例簡單介紹
- Bootstrap table中toolbar新增條件查詢及refresh參數(shù)使用方法
- BootStrap Table后臺(tái)分頁時(shí)前臺(tái)刪除最后一頁所有數(shù)據(jù)refresh刷新后無數(shù)據(jù)問題
- Bootstrap的Refresh Icon也spin起來
- 使用vue框架 Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來
- Bootstrap進(jìn)度條與AJAX后端數(shù)據(jù)傳遞結(jié)合使用實(shí)例詳解
- bootstrap jquery dataTable 異步ajax刷新表格數(shù)據(jù)的實(shí)現(xiàn)方法
- 使用Bootstrap Tabs選項(xiàng)卡Ajax加載數(shù)據(jù)實(shí)現(xiàn)
- DataTables+BootStrap組合使用Ajax來獲取數(shù)據(jù)并且動(dòng)態(tài)加載dom的方法(排序,過濾,分頁等)
- bootstrapTable+ajax加載數(shù)據(jù) refresh更新數(shù)據(jù)
相關(guān)文章
瀑布流的實(shí)現(xiàn)方式(原生js+jquery+css3)
這篇文章主要為大家詳細(xì)介紹了原生js+jquery+css3實(shí)現(xiàn)瀑布流的相關(guān)代碼,三種實(shí)現(xiàn)瀑布流的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07微信小程序methods中定義的方法互相調(diào)用的實(shí)例代碼
這篇文章主要介紹了微信小程序methods中定義的方法互相調(diào)用的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08微信小程序注冊60s倒計(jì)時(shí)功能 使用JS實(shí)現(xiàn)注冊60s倒計(jì)時(shí)功能
這篇文章主要介紹了微信小程序注冊60s倒計(jì)時(shí)功能,以及使用JS實(shí)現(xiàn)注冊60s倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08關(guān)于JS中match() 和 exec() 返回值和屬性的測試
這篇文章主要介紹了關(guān)于JS中match() 和 exec() 返回值和屬性的測試 的相關(guān)資料,需要的朋友可以參考下2016-03-03