详细属性参考官方API,https://github.com/select2/select2/releases/tag/4.0.5
注:4.0.5版本API与3.x版本有差异,有些属性已废弃,以下列出常用属性及其参考值:

1、API

属性 类型 默认值 描述
data Array of objects Null 数据集合,基础数据格式{id:"", text:"", selected: true/选中/, disabled: true/失效/}
width string “” 宽度
style string “” 样式
ajax object null Ajax请求数据
minimumResultsForSearch Integer null 设置支持搜索的最小集合,设置为负数,隐藏搜索框
minimumInputLength Integer   输入指定长度字符后开始搜索
multiple boolean False 是否多选,默认单选
maximumSelectionSize Integer   支持最大的选择数量,int/function
maximumInputLength Integer   支持搜索的最大字符数
placeholder String “” 选择提示
allowClear Boolean false 是否显示清除按钮,只有设置了placeholder才有效
closeOnSelect Boolean true 是否选中后关闭选择框,默认true
templateSelection callback   选中项样式
templateResult callback   选项样式
matcher callback   过滤选项集合
sorter callback   选项结果集排序
theme String   主题,可以设置bootstrap主题
tags Boolean   是否可动态创建选项
tokenSeparators Aray   输入时使用分隔符创建新选项
createTag callback   创建新标签
insertTag callback   在选项集合后插入标签
disabled boolean false 是否失效
debug boolean false 是否开启debug

注:initSelection 和query已废弃

2、定义组件Select2

/**
* 创建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});
// 最多输入的字符数
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请求数据
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分页,官方例子,没有出现分页情况,后续用到时具体测试(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); // 动态创建新选项
Select2.select2("#dynamicOption", {width:"50%", data:data, tags:true}, true); });
});

链接:https://www.jianshu.com/p/778e9f21fc3d

Select2 4.0.5 API的更多相关文章

  1. ASP.NET CORE 1.0 MVC API 文档用 SWASHBUCKLE SWAGGER实现

    from:https://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/ 代码生成工具: https ...

  2. Zepto,Zepto API 中文版,Zepto 中文手册,Zepto API,Zepto API 中文版,Zepto 中文手册,Zepto API 1.0, Zepto API 1.0 中文版,Zepto 1.0 中文手册,Zepto 1.0 API-translate by yaotaiyang

    Zepto,Zepto API 中文版,Zepto 中文手册,Zepto API,Zepto API 中文版,Zepto 中文手册,Zepto API 1.0, Zepto API 1.0 中文版,Z ...

  3. 从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

  4. 从头编写 asp.net core 2.0 web api 基础框架 (3)

    第一部分:http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html 之前我介 ...

  5. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (3)

    Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratc ...

  6. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

  7. 从头编写asp.net core 2.0 web api 基础框架 (5) + 使用Identity Server 4建立Authorization Server (7) 可运行前后台源码

    前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...

  8. 新浪微博 使用OAuth2.0调用API

    # -*- coding: cp936 -*- #python 2.7.10 #xiaodeng #新浪微博 使用OAuth2.0调用API #微博开放接口的调用,都需要获取用户的身份认证.目前微博开 ...

  9. 分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0(使用 .NET Core 3.0 Desktop API Analyzer )

    今年五月的 Build 大会上,微软说 .NET Core 3.0 将带来 WPF / Windows Forms 这些桌面应用的支持.当然,是通过 Windows 兼容包(Windows Compa ...

随机推荐

  1. IE在开发工具启动的情况下(打开F12)时 JS才能执行

    在开发一个项目时遇到一个bug:在360急速浏览器的兼容模式下并且是线上环境时js无法执行(360急速浏览器的兼容模式下测试环境就ok), 打开f12以后刷新就没问题了,查了一下网上说的IE6/7是没 ...

  2. shut immediate 数据库遭遇 ORA-24324 ORA-24323

    SQL> shut immediateORA-24324: service handle not initializedORA-24323: value not allowedORA-27140 ...

  3. 箱线图boxplot

    箱线图boxplot--展示数据的分布 图表作用: 1.反映一组数据的分布特征,如:分布是否对称,是否存在离群点 2.对多组数据的分布特征进行比较 3.如果只有一个定量变量,很少用箱线图去看数据的分布 ...

  4. 4939-Agent2-洛谷

    传送门 emm... 这次没有原题了 (因为我懒) 就是一道很简单的树状数组 真的很简单很简单 只用到了一点点的差分 注意注意: 只用树状数组,不用差分会t掉的 所以.. 我不仅t了 还wa了 emm ...

  5. CRF 条件随机场工具包

    CRF - 条件随机场 工具包(python/c++) 项目案例 ConvCRF+FullCRF https://github.com/MarvinTeichmann/ConvCRF 需要的包Opti ...

  6. face recognition[Euclidean-distance-based loss][Center Face]

    本文来自<A Discriminative Feature Learning Approach for Deep Face Recognition>,时间线为2016年.采用的loss是C ...

  7. lwip TCP client & FreeRTOS 打开TCP 的 保活机制 LWIP_TCP_KEEPALIVE==1

    参考大神教程:http://blog.sina.com.cn/s/blog_62a85b950101aw8x.html   老衲五木 :http://blog.sina.com.cn/s/blog_6 ...

  8. RPC与Zookeeper注册中心的简单实现

    连接上文:https://www.cnblogs.com/wuzhenzhao/p/9962250.html RPC框架的简单实现,基于这个小程序,在我学习完Zookeeper之后如何将注册中心与RP ...

  9. 基于 Vue + Koa2 + MongoDB + Redis 实现一个完整的登录注册

    项目地址:https://github.com/caochangkui/vue-element-responsive-demo/tree/login-register 通过 vue-cli3.0 + ...

  10. Netty入门(三)之web服务器

    Netty入门(三)之web服务器 阅读前请参考 Netty入门(一)之webSocket聊天室 Netty入门(二)之PC聊天室 有了前两篇的使用基础,学习本文也很简单!只需要在前两文的基础上稍微改 ...