多个节点调用select2组件,每个节点都初始化一次,这样增加很多没用的代码,编程繁琐,封装起来集成调用,看起来更加明了简洁
html部分
<div class="form-group">
<label class="col-sm-3 control-label">类型</label>
<div class="col-sm-9">
<input type="text" class="form-control select2" id='chaptertype'>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >安监机构</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="chapterunit">
</div>
</div>
js脚本
//类型
this.select2Search({
type:'flow-type',
dom:'chaptertype',
tip:'请选择类型',
method:'getItemFieldsData',
data:{"field":"type"},
id:'id',
name:'name'
});
// 安监机构 下拉框初始化
this.select2Search({
type:'flow-unit',
dom:'chapterunit',
tip:'请选择安监机构',
method:'getunits',
id:'id',
data:{"unitName":this.proxy(function(){
return this.term;
})},
name:'name'
});
/**
* @ title select2查询通用盒子
* dom 传入节点标识
* tip 传入提示的显示信息
* method 查询方法【data的传入参数,根据项目需求填写】
* text显示的文本
* id 标识的ID值
* type 查询类型
* data ajax需要传入特殊参数
* */
select2Search:function(params){
$("#"+params.dom).select2({
placeholder: params.tip,
ajax:{
url: $.u.config.constant.smsqueryserver,
type: "post",
dataType: "json",
data: this.proxy(function(term, page){
this.term = term;
return $.extend({
"tokenid":$.cookie("tokenid"),
"method":params.method,
},params.data);
}),
results:function(data,page){
if(data.success){
return {results:$.map(data.data,function(item,idx){
return item;
})};
}else{
$.u.alert.error(data.reason, 1000 * 3);
}
}
},
formatResult: function(item){
return item[params.name];
},
formatSelection: function(item){
return item[params.name];
}
});
}
备注:需要模糊搜索时候,term变量,需要定义js全局变量调用