bootstrap3使用select2插件实现

源代码如下:

注意事件绑定:$(“#myModal”).on(“shown.bs.modal”

并且要把这个属性去除model的 tabindex属性

ajax返回格式
2017-08-15 14:04:54 补充
返回格式有要求,请注意

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
错误
//如果直接是对象会报错:
//select2.js:3604 Uncaught TypeError: data.unshift is not a function
[
{
"name": "呵呵呵",
"id": 11
},
{
"name": "啦啦啦",
"id": 2
}
]


正确
[
{
"text": "呵呵呵",
"id": 11
},
{
"text": "啦啦啦",
"id": 2
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap 101 Template</title>

<!-- Bootstrap -->
<link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">

<link rel="stylesheet" href="css/select2.min.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>

<![endif]-->
</head>
<body>

<div class="container">
<div class="row">
<div class="col-md-12">
<select class="js-example-disabled-results form-control">
<option value="one">First</option>
<option value="two" disabled="disabled">Second (disabled)</option>
<option value="three">Third</option>
</select>
<button type="button" id="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>

</div>
</div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel">

<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="myModalLabel">测试</h4>
</div>
<div class="modal-body">
<select class="js-example-disabled-results2 form-control">
<option value="one">First</option>
<option value="two" disabled="disabled">Second (disabled)</option>
<option value="three">Third</option>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary">保存操作</button>
</div>
</div>
</div>
</div>


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="js/select2.js"></script>


<script>
$("#myModal").on("shown.bs.modal", function(){
//取消model属性 tabindex="-1",防止输入框被禁止
$.fn.modal.Constructor.prototype.enforceFocus = function () { };
//绑定select2插件
function formatTopic (topic) {
return "<div class='select2-result-repository clearfix'>" +
"<div class='select2-result-repository__meta'>" +
"<div class='select2-result-repository__title'>" +
topic.name ? topic.name : "topic" +
"</div></div></div>";
}

function formatTopicSelection (topic) {
return topic.name || topic.text;
}
$(".js-example-disabled-results2").select2({
//tags 关闭 没有你输入的那个选项
tags: true,
placeholder: '选择相关话题',
minimumInputLength: 2,
ajax: {
url: '/api/topics',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term
};
},
processResults: function (data, params) {
return {
results: data
};
},
cache: true
},
templateResult: formatTopic,
templateSelection: formatTopicSelection,
escapeMarkup: function (markup) { return markup; }
});
})
</script>


</body>
</html>
感谢老板!