自定义select2搜索框位置

时间:2018-05-17 20:56:24

标签: jquery html css jquery-select2

有没有办法让select2下拉列表的主要字段也作为搜索框?

在文档中,单个选择将搜索框放在字段下方(在下拉列表中),在多选字段中我们可以直接在字段内键入(这是我想要实现的交互,但我只想要单个选择)。

$(document).ready(function() {
  $("#type").select2({
    placeholder: "Select type"
  });
  
  $(".js-example-basic-multiple").select2({
    placeholder: "Select items"
  }).on('change', function(e) {
    if($(this).val() && $(this).val().length) {
      $(this).next('.select2-container').find('li.select2-search--inline input.select2-search__field').attr('placeholder', 'Select items');
    }
  });
});
.input-text {
  height: 34px;
  border: solid 1px #c4c4c4;
  width: 240px;
  font-size: 13px;
  padding: 0 0 0 12px;
  margin-bottom: 24px;
  color: #333;
  background-color: #fff;
  border-radius: 0;
}

.form-row {
  display: block;
  width: 100%;
  margin: 0 !important;
}

.form-divided {
  display: inline-block;
}

.form-divided-left {
  margin-right: 32px;
}

.datepicker-calendar {
  position: absolute;
}

.dropdown-icon {
  transform: translateY(-2px); 
}

.select2-selection {
  height: 34px !important; 
  font-size: 13px;
  font-family: 'Open Sans', sans-serif;
  border-radius: 0 !important;
  border: solid 1px #c4c4c4 !important;
  padding-left: 4px;
}

.select2-selection:focus {
  border: solid 1px #00c983 !important; 
}

.select2-selection--multiple {
  height: 154px !important;
}

.select2-selection__choice__remove {
   float: right;
   margin-right: 0;
   margin-left: 2px;
}

.select2-selection__choice {
  height: 40px;
  line-height: 40px;
  padding-right: 16px !important;
  padding-left: 16px !important;
  background-color: #CAF1FF !important;
  color: #333 !important;
  border: none !important;
  border-radius: 3px !important;
}

.select2-search--inline .select2-search__field {
  line-height: 40px;
  color: #333;
  width: 80px !important;
}

.select2-container:hover,
.select2-container:focus,
.select2-container:active,
.select2-selection:hover,
.select2-selection:focus,
.select2-selection:active {
  outline-color: transparent;
  outline-style: none;
}

.select2-results__options li {
  display: block; 
}

.select2-selection__rendered {
  transform: translateY(2px);
}

.select2-selection__arrow {
  display: none;
}

.select2-results__option--highlighted {
  background-color: #CAF1FF !important;
  color: #333 !important;
}

.select2-dropdown {
  border-radius: 0 !important;
  box-shadow: 0px 3px 6px 0 rgba(0,0,0,0.15) !important;
  border: none !important;
  margin-top: 4px !important;
}

.select2-results__option {
  font-family: 'Open Sans', sans-serif;
  font-size: 13px;
  line-height: 24px !important;
  vertical-align: middle !important;
  padding-left: 8px !important;
}

.select2-results__option[aria-selected="true"] {
  background-color: #eee !important; 
}

.select2-search__field {
  font-family: 'Open Sans', sans-serif;
  color: #333;
  font-size: 13px;
  padding-left: 8px !important;
  border-color: #c4c4c4 !important;
}

.select2-selection__placeholder {
  color: #c4c4c4 !important; 
}

.form-dropdown {
  margin-bottom: 24px; 
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">

<div class="form">
  <div class="form-row">
    <div class="form-unit form-divided form-divided-left">
      
      <label for="type" class="form-input-label">Type</label>
      <select id="type" class="input-text">
        <option></option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
      </select>
      <a class="input-select-btn"><i class="fas fa-sort-down dropdown-icon"></i></a>
    </div>
    <div class="form-unit form-divided">
      <label for="emp-id" class="form-input-label">Pill Box</label>
      <select class="js-example-basic-multiple input-text" name="states[]" multiple="multiple">
        <option value="a1">Item A1</option>
        <option value="a2">Item A2</option>
        <option value="b1">Item B1</option>
        <option value="a1">Item C1</option>
        <option value="a2">Item C2</option>
        <option value="b1">Item C3</option>
      </select>
    </div>
  </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>

0 个答案:

没有答案