Skip to content

Instantly share code, notes, and snippets.

@taichunmin
Last active July 18, 2021 07:12
Show Gist options
  • Save taichunmin/ac6708565509eece258c0e2cfba4c977 to your computer and use it in GitHub Desktop.
Save taichunmin/ac6708565509eece258c0e2cfba4c977 to your computer and use it in GitHub Desktop.
jQuery Chosen for Vue 2
.chosen-select {
width: 100%; }
.chosen-select-deselect {
width: 100%; }
.chosen-container {
display: inline-block;
font-size: 14px;
position: relative;
vertical-align: middle; }
.chosen-container .chosen-drop {
background: #fff;
border: 1px solid #ccc;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
-webkit-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.25);
box-shadow: 0 8px 8px rgba(0, 0, 0, 0.25);
margin-top: -1px;
position: absolute;
top: 100%;
left: -9000px;
z-index: 1060; }
.chosen-container.chosen-with-drop .chosen-drop {
left: 0;
right: 0; }
.chosen-container .chosen-results {
color: #555555;
margin: 0 4px 4px 0;
max-height: 240px;
padding: 0 0 0 4px;
position: relative;
overflow-x: hidden;
overflow-y: auto;
-webkit-overflow-scrolling: touch; }
.chosen-container .chosen-results li {
display: none;
line-height: 1.42857;
list-style: none;
margin: 0;
padding: 5px 6px; }
.chosen-container .chosen-results li em {
background: #feffde;
font-style: normal; }
.chosen-container .chosen-results li.group-result {
display: list-item;
cursor: default;
color: #999;
font-weight: bold; }
.chosen-container .chosen-results li.group-option {
padding-left: 15px; }
.chosen-container .chosen-results li.active-result {
cursor: pointer;
display: list-item; }
.chosen-container .chosen-results li.highlighted {
background-color: #337ab7;
background-image: none;
color: white; }
.chosen-container .chosen-results li.highlighted em {
background: transparent; }
.chosen-container .chosen-results li.disabled-result {
display: list-item;
color: #777777; }
.chosen-container .chosen-results .no-results {
background: #eeeeee;
display: list-item; }
.chosen-container .chosen-results-scroll {
background: white;
margin: 0 4px;
position: absolute;
text-align: center;
width: 321px;
z-index: 1; }
.chosen-container .chosen-results-scroll span {
display: inline-block;
height: 1.42857;
text-indent: -5000px;
width: 9px; }
.chosen-container .chosen-results-scroll-down {
bottom: 0; }
.chosen-container .chosen-results-scroll-down span {
background: url("chosen-sprite.png") no-repeat -4px -3px; }
.chosen-container .chosen-results-scroll-up span {
background: url("chosen-sprite.png") no-repeat -22px -3px; }
.chosen-container-single .chosen-single {
background-color: #fff;
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box;
border: 1px solid #ccc;
border-top-right-radius: 4px;
border-top-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
color: #555555;
display: block;
height: 34px;
overflow: hidden;
line-height: 34px;
padding: 0 0 0 8px;
position: relative;
text-decoration: none;
white-space: nowrap; }
.chosen-container-single .chosen-single span {
display: block;
margin-right: 26px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap; }
.chosen-container-single .chosen-single abbr {
background: url("chosen-sprite.png") right top no-repeat;
display: block;
font-size: 1px;
height: 10px;
position: absolute;
right: 26px;
top: 12px;
width: 12px; }
.chosen-container-single .chosen-single abbr:hover {
background-position: right -11px; }
.chosen-container-single .chosen-single.chosen-disabled .chosen-single abbr:hover {
background-position: right 2px; }
.chosen-container-single .chosen-single div {
display: block;
height: 100%;
position: absolute;
top: 0;
right: 0;
width: 18px; }
.chosen-container-single .chosen-single div b {
background: url("chosen-sprite.png") no-repeat 0 7px;
display: block;
height: 100%;
width: 100%; }
.chosen-container-single .chosen-default {
color: #777777; }
.chosen-container-single .chosen-search {
margin: 0;
padding: 3px 4px;
position: relative;
white-space: nowrap;
z-index: 1000; }
.chosen-container-single .chosen-search input[type="text"] {
background: url("chosen-sprite.png") no-repeat 100% -20px, #fff;
border: 1px solid #ccc;
border-top-right-radius: 4px;
border-top-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
margin: 1px 0;
padding: 4px 20px 4px 4px;
width: 100%; }
.chosen-container-single .chosen-drop {
margin-top: -1px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box; }
.chosen-container-single-nosearch .chosen-search input[type="text"] {
position: absolute;
left: -9000px; }
.chosen-container-multi .chosen-choices {
background-color: #fff;
border: 1px solid #ccc;
border-top-right-radius: 4px;
border-top-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
cursor: text;
height: auto !important;
height: 1%;
margin: 0;
overflow: hidden;
padding: 0;
position: relative; }
.chosen-container-multi .chosen-choices li {
float: left;
list-style: none; }
.chosen-container-multi .chosen-choices .search-field {
margin: 0;
padding: 0;
white-space: nowrap; }
.chosen-container-multi .chosen-choices .search-field input[type="text"] {
background: transparent !important;
border: 0 !important;
-webkit-box-shadow: none;
box-shadow: none;
color: #555555;
height: 32px;
margin: 0;
padding: 4px;
outline: 0; }
.chosen-container-multi .chosen-choices .search-field .default {
color: #999; }
.chosen-container-multi .chosen-choices .search-choice {
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box;
background-color: #eeeeee;
border: 1px solid #ccc;
border-top-right-radius: 4px;
border-top-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
background-image: -webkit-linear-gradient(top, white 0%, #eeeeee 100%);
background-image: -o-linear-gradient(top, white 0%, #eeeeee 100%);
background-image: linear-gradient(to bottom, white 0%, #eeeeee 100%);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#FFFFFFFF', endColorstr='#FFEEEEEE', GradientType=0);
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
color: #333333;
cursor: default;
line-height: 13px;
margin: 6px 0 3px 5px;
padding: 3px 20px 3px 5px;
position: relative; }
.chosen-container-multi .chosen-choices .search-choice .search-choice-close {
background: url("chosen-sprite.png") right top no-repeat;
display: block;
font-size: 1px;
height: 10px;
position: absolute;
right: 4px;
top: 5px;
width: 12px;
cursor: pointer; }
.chosen-container-multi .chosen-choices .search-choice .search-choice-close:hover {
background-position: right -11px; }
.chosen-container-multi .chosen-choices .search-choice-focus {
background: #d4d4d4; }
.chosen-container-multi .chosen-choices .search-choice-focus .search-choice-close {
background-position: right -11px; }
.chosen-container-multi .chosen-results {
margin: 0 0 0 0;
padding: 0; }
.chosen-container-multi .chosen-drop .result-selected {
display: none; }
.chosen-container-active .chosen-single {
border: 1px solid #66afe9;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px #66afe9;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px #66afe9;
-webkit-transition: border linear 0.2s, box-shadow linear 0.2s;
-o-transition: border linear 0.2s, box-shadow linear 0.2s;
transition: border linear 0.2s, box-shadow linear 0.2s; }
.chosen-container-active.chosen-with-drop .chosen-single {
background-color: #fff;
border: 1px solid #66afe9;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px #66afe9;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px #66afe9;
-webkit-transition: border linear 0.2s, box-shadow linear 0.2s;
-o-transition: border linear 0.2s, box-shadow linear 0.2s;
transition: border linear 0.2s, box-shadow linear 0.2s; }
.chosen-container-active.chosen-with-drop .chosen-single div {
background: transparent;
border-left: none; }
.chosen-container-active.chosen-with-drop .chosen-single div b {
background-position: -18px 7px; }
.chosen-container-active .chosen-choices {
border: 1px solid #66afe9;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px #66afe9;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px #66afe9;
-webkit-transition: border linear 0.2s, box-shadow linear 0.2s;
-o-transition: border linear 0.2s, box-shadow linear 0.2s;
transition: border linear 0.2s, box-shadow linear 0.2s; }
.chosen-container-active .chosen-choices .search-field input[type="text"] {
color: #111 !important; }
.chosen-container-active.chosen-with-drop .chosen-choices {
border-bottom-right-radius: 0;
border-bottom-left-radius: 0; }
.chosen-disabled {
cursor: default;
opacity: 0.5 !important; }
.chosen-disabled .chosen-single {
cursor: default; }
.chosen-disabled .chosen-choices .search-choice .search-choice-close {
cursor: default; }
.chosen-rtl {
text-align: right; }
.chosen-rtl .chosen-single {
padding: 0 8px 0 0;
overflow: visible; }
.chosen-rtl .chosen-single span {
margin-left: 26px;
margin-right: 0;
direction: rtl; }
.chosen-rtl .chosen-single div {
left: 7px;
right: auto; }
.chosen-rtl .chosen-single abbr {
left: 26px;
right: auto; }
.chosen-rtl .chosen-choices .search-field input[type="text"] {
direction: rtl; }
.chosen-rtl .chosen-choices li {
float: right; }
.chosen-rtl .chosen-choices .search-choice {
margin: 6px 5px 3px 0;
padding: 3px 5px 3px 19px; }
.chosen-rtl .chosen-choices .search-choice .search-choice-close {
background-position: right top;
left: 4px;
right: auto; }
.chosen-rtl.chosen-container-single .chosen-results {
margin: 0 0 4px 4px;
padding: 0 4px 0 0; }
.chosen-rtl .chosen-results .group-option {
padding-left: 0;
padding-right: 15px; }
.chosen-rtl.chosen-container-active.chosen-with-drop .chosen-single div {
border-right: none; }
.chosen-rtl .chosen-search input[type="text"] {
background: url("chosen-sprite.png") no-repeat -28px -20px, #fff;
direction: rtl;
padding: 4px 5px 4px 20px; }
@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-resolution: 2dppx) {
.chosen-rtl .chosen-search input[type="text"],
.chosen-container-single .chosen-single abbr,
.chosen-container-single .chosen-single div b,
.chosen-container-single .chosen-search input[type="text"],
.chosen-container-multi .chosen-choices .search-choice .search-choice-close,
.chosen-container .chosen-results-scroll-down span,
.chosen-container .chosen-results-scroll-up span {
background-image: url("[email protected]") !important;
background-size: 52px 37px !important;
background-repeat: no-repeat !important; } }
/*# sourceMappingURL=bootstrap-chosen.css.map */
/*
* Disabled
*/
fieldset[disabled] .chosen-container {
pointer-events: none;
cursor: not-allowed;
opacity: .65;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Document</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="chosen-bootstrap.css">
</head>
<body>
<div class="container" id="app">
<form @submit.prevent>
<div class="form-group">
<label>ID Alphabet</label>
<select-chosen v-model="chosen" :options="options"placeholder="選擇一個選項..."></select-chosen>
</div>
<div class="form-group">
<label>所選項目</label>
<p class="form-control-static" v-if="chosen">{{chosen + ': ' + options[chosen]}}</p>
</div>
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.5/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- vue-chosen.js Require jQuery and Vue -->
<script src="vue-chosen.js"></script>
<script type="text/javascript">
'use strict';
var vm = new Vue({
el: '#app',
data() {
return {
options: {
A: '臺北市',
B: '臺中市',
C: '基隆市',
D: '臺南市',
E: '高雄市',
F: '新北市',
G: '宜蘭縣',
H: '桃園市',
I: '嘉義市',
J: '新竹縣',
K: '苗栗縣',
M: '南投縣',
N: '彰化縣',
O: '新竹市',
P: '雲林縣',
Q: '嘉義縣',
T: '屏東縣',
U: '花蓮縣',
V: '臺東縣',
W: '金門縣',
X: '澎湖縣',
Z: '連江縣',
L: '臺中縣',
R: '臺南縣',
S: '高雄縣',
Y: '陽明山管理局',
},
chosen: '',
};
},
});
</script>
</body>
</html>
Vue.component('select-chosen', {
template: '<select :value="value" v-on:input="updateValue($event.target.value)" :data-placeholder="placeholder" :class="class1 || {}"><option v-for="(name, id) in options" :value="id">{{name}}</option></select>',
props: {
value: {type: String, required: true, default: ''},
options: {type: Object, required: true},
placeholder: {type: String, default: '選擇一個選項...'},
class1: {type: Object, default: function(){ return {} } },
debug: {type: Boolean, default: false},
},
mounted() {
var my = this;
$(this.$el).change(function(){
if(this.debug) console.log('chosen:change', this.value);
my.updateValue(this.value); // this == this.$el
});
this.init();
},
methods: {
init() {
if(this.debug) console.log('chosen:init');
$(this.$el).chosen({
disable_search_threshold: 5,
inherit_select_classes: true,
no_results_text: "什麼都沒找到...",
search_contains: true,
width: '100%',
});
this.$nextTick(function(){
// 修正 select 在 value 為空時 inserted 後會自動選擇第一個的問題
this.$el.value = this.value;
$(this.$el).trigger('chosen:updated');
});
return this;
},
destroy() {
if(this.debug) console.log('chosen:destroy');
$(this.$el).chosen('destroy');
return this;
},
updateValue(value) {
this.$emit('input', value);
},
},
watch: {
value() {
this.$nextTick(function(){
if(this.debug) console.log('chosen:updated', this.value);
$(this.$el).trigger('chosen:updated');
});
},
options() {
this.$nextTick(function(){
if(this.debug) console.log('chosen:rebuild', this.options.length);
this.destroy().init();
});
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment