bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實例
用select2插件,實現(xiàn)以下這個選擇框:

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:'請選擇',//默認文字提示
tags: true,//允許手動添加
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里來返回數(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有一個為0,會導(dǎo)致這個選項無法選取,因為在select2中id=0有特殊意義
以上這篇bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- BootstrapTable refresh 方法使用實例簡單介紹
- Bootstrap table中toolbar新增條件查詢及refresh參數(shù)使用方法
- BootStrap Table后臺分頁時前臺刪除最后一頁所有數(shù)據(jù)refresh刷新后無數(shù)據(jù)問題
- Bootstrap的Refresh Icon也spin起來
- 使用vue框架 Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來
- Bootstrap進度條與AJAX后端數(shù)據(jù)傳遞結(jié)合使用實例詳解
- bootstrap jquery dataTable 異步ajax刷新表格數(shù)據(jù)的實現(xiàn)方法
- 使用Bootstrap Tabs選項卡Ajax加載數(shù)據(jù)實現(xiàn)
- DataTables+BootStrap組合使用Ajax來獲取數(shù)據(jù)并且動態(tài)加載dom的方法(排序,過濾,分頁等)
- bootstrapTable+ajax加載數(shù)據(jù) refresh更新數(shù)據(jù)
相關(guān)文章
瀑布流的實現(xiàn)方式(原生js+jquery+css3)
這篇文章主要為大家詳細介紹了原生js+jquery+css3實現(xiàn)瀑布流的相關(guān)代碼,三種實現(xiàn)瀑布流的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
微信小程序methods中定義的方法互相調(diào)用的實例代碼
這篇文章主要介紹了微信小程序methods中定義的方法互相調(diào)用的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
微信小程序注冊60s倒計時功能 使用JS實現(xiàn)注冊60s倒計時功能
這篇文章主要介紹了微信小程序注冊60s倒計時功能,以及使用JS實現(xiàn)注冊60s倒計時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
關(guān)于JS中match() 和 exec() 返回值和屬性的測試
這篇文章主要介紹了關(guān)于JS中match() 和 exec() 返回值和屬性的測試 的相關(guān)資料,需要的朋友可以參考下2016-03-03

