我正在使用jQuery Multi-Select進行“從左到右”的多選,我想向這個多選添加搜索功能,該功能在使用QuickSearch庫的可搜索演示中作業,但我無法讓它作業,我沒有錯誤。
我一直在尋找更現代的解決方案,因為這兩個專案都很老,但我一直沒能做到。
在下面的代碼段中,我正在嘗試做一個示例。
$('.searchable').multiSelect({
selectableHeader: "<input type='text' class='search-input form-control form-control-sm' autocomplete='off' placeholder='Search..'><br>",
selectionHeader: "<input type='text' class='search-input form-control form-control-sm' autocomplete='off' placeholder='Search..'><br>",
afterInit: function(ms) {
var that = this,
$selectableSearch = that.$selectableUl.prev(),
$selectionSearch = that.$selectionUl.prev(),
selectableSearchString = '#' that.$container.attr('id') ' .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#' that.$container.attr('id') ' .ms-elem-selection.ms-selected';
that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
.on('keydown', function(e) {
if (e.which === 40) {
that.$selectableUl.focus();
return false;
}
});
that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
.on('keydown', function(e) {
if (e.which == 40) {
that.$selectionUl.focus();
return false;
}
});
},
afterSelect: function() {
this.qs1.cache();
this.qs2.cache();
},
afterDeselect: function() {
this.qs1.cache();
this.qs2.cache();
}
});
.ms-container {
background: transparent url('../img/switch.png') no-repeat 50% 50%;
}
.ms-container:after {
content: ".";
display: block;
height: 0;
line-height: 0;
font-size: 0;
clear: both;
min-height: 0;
visibility: hidden;
}
.ms-container .ms-selectable,
.ms-container .ms-selection {
background: #fff;
color: #555555;
float: left;
width: 45%;
}
.ms-container .ms-selection {
float: right;
}
.ms-container .ms-list {
/* -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); */
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
/* box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); */
/* -webkit-transition: border linear 0.2s, box-shadow linear 0.2s; */
-moz-transition: border linear 0.2s, box-shadow linear 0.2s;
-ms-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;
border: 1px solid #e4e5ee;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
position: relative;
height: 250px;
padding: 0;
overflow-y: auto;
}
.ms-container .ms-list.ms-focus {
border-color: rgba(66, 80, 165, 0.02);
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
outline: 0;
outline: thin dotted \9;
}
.ms-container ul {
margin: 0;
list-style-type: none;
padding: 0;
}
.ms-container .ms-optgroup-container {
width: 100%;
}
.ms-container .ms-optgroup-label {
margin: 0;
padding: 5px 0px 0px 5px;
cursor: pointer;
color: #4b4e68;
}
.ms-container .ms-selectable li.ms-elem-selectable,
.ms-container .ms-selection li.ms-elem-selection {
border-bottom: 1px #eee solid;
padding: 13px 13px;
color: #71748d;
font-size: 14px;
}
.ms-container .ms-selectable li.ms-hover,
.ms-container .ms-selection li.ms-hover {
cursor: pointer;
color: #fff;
text-decoration: none;
background-color: #5969ff;
}
.ms-container .ms-selectable li.disabled,
.ms-container .ms-selection li.disabled {
background-color: #efeff6;
color: #c6c6d3;
cursor: text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://efp.i-r.dk/js/jquery-quicksearch.js"></script>
<script src="https://efp.i-r.dk/js/jquery.multi-select.js"></script>
<select class="searchable" name="Users" id="Users1" multiple="multiple">
<option value="User1">User1</option>
<option value="User2">User2</option>
<option value="User3">User3</option>
<option value="User4">User4</option>
<option value="User5">User5</option>
</select>
<select class="searchable" name="Users" id="Users2" multiple="multiple">
<option value="User1">User1</option>
<option value="User2">User2</option>
<option value="User3">User3</option>
<option value="User4">User4</option>
<option value="User5">User5</option>
</select>
任何人都可以幫助找到使其作業的解決方案,或者給我指出一個更現代的腳本?.. 對我來說非常重要的是“從左到右”的選擇將在點擊元素時完成,而不是點擊 ekstra 按鈕。
uj5u.com熱心網友回復:
嘗試這個:
我改變了選擇器:
var that = this,
$selectableSearch = that.$container[0].children[0].children[0],
$selectionSearch = that.$container[0].children[1].children[0],
selectableSearchString = '#' that.$container.attr('id') ' .ms-selectable .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#' that.$container.attr('id') ' .ms-selection .ms-elem-selection.ms-selected';
that.qs1 = $($selectableSearch).quicksearch(selectableSearchString)
.on('keydown', function(e) {
if (e.which === 40) {
that.$selectableUl.focus();
return false;
}
});
that.qs2 = $($selectionSearch).quicksearch(selectionSearchString)
.on('keydown', function(e) {
if (e.which == 40) {
that.$selectionUl.focus();
return false;
}
});
演示
顯示代碼片段
$('.searchable').multiSelect({
selectableHeader: "<input type='text' class='search-input form-control form-control-sm' autocomplete='off' placeholder='Search..'><br>",
selectionHeader: "<input type='text' class='search-input form-control form-control-sm' autocomplete='off' placeholder='Search..'><br>",
afterInit: function(ms) {
var that = this,
$selectableSearch = that.$container[0].children[0].children[0],
$selectionSearch = that.$container[0].children[1].children[0],
selectableSearchString = '#' that.$container.attr('id') ' .ms-selectable .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#' that.$container.attr('id') ' .ms-selection .ms-elem-selection.ms-selected';
that.qs1 = $($selectableSearch).quicksearch(selectableSearchString)
.on('keydown', function(e) {
if (e.which === 40) {
that.$selectableUl.focus();
return false;
}
});
that.qs2 = $($selectionSearch).quicksearch(selectionSearchString)
.on('keydown', function(e) {
if (e.which == 40) {
that.$selectionUl.focus();
return false;
}
});
},
afterSelect: function() {
this.qs1.cache();
this.qs2.cache();
},
afterDeselect: function() {
this.qs1.cache();
this.qs2.cache();
}
});
.ms-container {
background: transparent url('../img/switch.png') no-repeat 50% 50%;
}
.ms-container:after {
content: ".";
display: block;
height: 0;
line-height: 0;
font-size: 0;
clear: both;
min-height: 0;
visibility: hidden;
}
.ms-container .ms-selectable,
.ms-container .ms-selection {
background: #fff;
color: #555555;
float: left;
width: 45%;
}
.ms-container .ms-selection {
float: right;
}
.ms-container .ms-list {
/* -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); */
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
/* box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); */
/* -webkit-transition: border linear 0.2s, box-shadow linear 0.2s; */
-moz-transition: border linear 0.2s, box-shadow linear 0.2s;
-ms-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;
border: 1px solid #e4e5ee;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
position: relative;
height: 250px;
padding: 0;
overflow-y: auto;
}
.ms-container .ms-list.ms-focus {
border-color: rgba(66, 80, 165, 0.02);
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
outline: 0;
outline: thin dotted \9;
}
.ms-container ul {
margin: 0;
list-style-type: none;
padding: 0;
}
.ms-container .ms-optgroup-container {
width: 100%;
}
.ms-container .ms-optgroup-label {
margin: 0;
padding: 5px 0px 0px 5px;
cursor: pointer;
color: #4b4e68;
}
.ms-container .ms-selectable li.ms-elem-selectable,
.ms-container .ms-selection li.ms-elem-selection {
border-bottom: 1px #eee solid;
padding: 13px 13px;
color: #71748d;
font-size: 14px;
}
.ms-container .ms-selectable li.ms-hover,
.ms-container .ms-selection li.ms-hover {
cursor: pointer;
color: #fff;
text-decoration: none;
background-color: #5969ff;
}
.ms-container .ms-selectable li.disabled,
.ms-container .ms-selection li.disabled {
background-color: #efeff6;
color: #c6c6d3;
cursor: text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://efp.i-r.dk/js/jquery-quicksearch.js"></script>
<script src="https://efp.i-r.dk/js/jquery.multi-select.js"></script>
<select class="searchable" name="Users" id="Users1" multiple="multiple">
<option value="User1">User1</option>
<option value="User2">User2</option>
<option value="User3">User3</option>
<option value="User4">User4</option>
<option value="User5">User5</option>
</select>
<select class="searchable" name="Users" id="Users2" multiple="multiple">
<option value="User1">User1</option>
<option value="User2">User2</option>
<option value="User3">User3</option>
<option value="User4">User4</option>
<option value="User5">User5</option>
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348606.html
標籤:查询
下一篇:如何隱藏jQuery懸停
