欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實(shí)例

 更新時(shí)間:2018年08月09日 14:24:23   作者:云中不知人  
今天小編就為大家分享一篇bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

用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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論