詳細屬性參考官方API亭病,https://github.com/select2/select2/releases/tag/4.0.5
注:4.0.5版本API與3.x版本有差異,有些屬性已廢棄,以下列出常用屬性及其參考值:
1戒财、API
屬性 | 類型 | 默認值 | 描述 |
---|---|---|---|
data | Array of objects | Null | 數(shù)據(jù)集合阅束,基礎數(shù)據(jù)格式{id:"", text:"", selected: true/選中/, disabled: true/失效/} |
width | string | “” | 寬度 |
style | string | “” | 樣式 |
ajax | object | null | Ajax請求數(shù)據(jù) |
minimumResultsForSearch | Integer | null | 設置支持搜索的最小集合,設置為負數(shù)躬存,隱藏搜索框 |
minimumInputLength | Integer | 輸入指定長度字符后開始搜索 | |
multiple | boolean | False | 是否多選张惹,默認單選 |
maximumSelectionSize | Integer | 支持最大的選擇數(shù)量,int/function | |
maximumInputLength | Integer | 支持搜索的最大字符數(shù) | |
placeholder | String | “” | 選擇提示 |
allowClear | Boolean | false | 是否顯示清除按鈕岭洲,只有設置了placeholder才有效 |
closeOnSelect | Boolean | true | 是否選中后關(guān)閉選擇框宛逗,默認true |
templateSelection | callback | 選中項樣式 | |
templateResult | callback | 選項樣式 | |
matcher | callback | 過濾選項集合 | |
sorter | callback | 選項結(jié)果集排序 | |
theme | String | 主題,可以設置bootstrap主題 | |
tags | Boolean | 是否可動態(tài)創(chuàng)建選項 | |
tokenSeparators | Aray | 輸入時使用分隔符創(chuàng)建新選項 | |
createTag | callback | 創(chuàng)建新標簽 | |
insertTag | callback | 在選項集合后插入標簽 | |
disabled | boolean | false | 是否失效 |
debug | boolean | false | 是否開啟debug |
注:initSelection 和query已廢棄
2盾剩、定義組件Select2
/**
* 創(chuàng)建select2基礎組件
*/
select2: function(selector, option, allDefault){
if(allDefault){
$(selector).select2(option);
}else{
$(selector).select2($.extend(true, {}, defaultOption, option));
}
}
3雷激、測試用例
html(省略)
js
require(["jquery", "js/component/Select2"], function($, Select2){
$(document).ready(function(){
var data = [{id:"1", text:"測試1"}, {id:"2", text:"測試2"}];
var format = function(data){
return $("" + data.text+ "111" + "");
}
// 基本搜索
Select2.select2("#select", {data: data});
// 無搜索框
Select2.select2("#noSearchSelect", {data: data, minimumResultsForSearch: -1});
// 多選
Select2.select2("#multiSelect", {data: data, multiple: true});
// 最多輸入的字符數(shù)
Select2.select2("#maxInput", {data: data, maximumInputLength: 2});
// 顯示清除按鈕
Select2.select2("#allowClear", {data: data, placeholder: "123", allowClear: true});
// 格式化選項
Select2.select2("#formatSection", {data: data, templateSelection: format,
templateResult: format});
// ajax請求數(shù)據(jù)
Select2.select2("#ajaxSelect", {width:"50%", ajax: {
url: 'https://api.github.com/orgs/select2/repos',
data: function (params) {
var query = {
search: params.term,
type: 'public'
}
return query;
}
}}, true);
// ajax分頁,官方例子告私,沒有出現(xiàn)分頁情況屎暇,后續(xù)用到時具體測試(2018.8.31)
Select2.select2("#ajaxPagination", {
width: "50%",
ajax: {
url: "https://api.github.com/search/repositories",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
placeholder: 'Search for a repository',
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1
}, true);
// 動態(tài)創(chuàng)建新選項
Select2.select2("#dynamicOption", {width:"50%", data:data, tags:true}, true);
});
});