Last active
October 30, 2020 12:44
-
-
Save GithubMrxia/53369cd03b89ee4e7ef5c3839015c82e to your computer and use it in GitHub Desktop.
封装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.extend({ | |
/** | |
* select2 ajax封装 | |
* @param string url | |
* @param obj transfer 转换响应数据 {id:'',text''} | |
* @param obj addQuery 增加的查询条件 | |
* @param obj params select2配置,可覆盖默认配置 | |
* @param string searchField 查询字段名 | |
* @param int limit 每页显示数量 | |
*/ | |
adminSelect2 : function(url, transfer, searchField = 'search', addQuery = {}, params = {}, limit = 10) { | |
var intiParams = { | |
ajax : { | |
url : url, | |
method : 'post', | |
dataType : 'json', | |
delay : 250, | |
data : function(params) { | |
var query = { | |
limit : limit, | |
page : params.page || 1 | |
} query[searchField] = params.term; | |
query = $.extend(query, addQuery); | |
return query; | |
}, | |
processResults : function(data, params) { | |
// var newData = [{id:0,text:'请选择'}]; | |
var newData = $.map(data.data.data, function(obj) { | |
obj.id = obj[(transfer.id)]; | |
obj.text = obj[(transfer.text)]; | |
return obj; | |
}); | |
newData.unshift({id : 0, text : '请选择'}); | |
return { | |
results : newData, | |
pagination : { | |
more : data.data.current_page < data.data.last_page | |
} | |
}; | |
}, | |
cache : true, | |
}, | |
// minimumInputLength: 2, | |
language : "zh-CN", | |
}; | |
params = $.extend(intiParams, params); | |
return $(this).select2(params); | |
}, | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="/js/plugins/select2/select2.full.min.js"></script> | |
<script src="/js/plugins/select2/zh-CN.js"></script> | |
$('#agent').adminSelect2('/url', {'id':'id','text': 'name'}, , 'name', {state:1}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 搜索框 | |
<div class="col-sm-4"> | |
<select class="form-control m-b input-sm" id="agent" name="agent_name" > | |
@if ($agentName = request('agent_name')) | |
<option value="{{$agentName}}">{{$agentName}}</option> | |
@else | |
<option value="">代理商</option> | |
@endif | |
</select> | |
</div> | |
$('#agent').adminSelect2('/url', {'id':'name','text': 'name'}, , 'name', {state:1}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment