Lou多选过滤器DUAL List by optgroup标签

时间:2019-06-28 16:58:18

标签: javascript

我正在使用http://loudev.com/中的引导多重选择...类似的问题都没有涉及双重列表和纯JS。我需要能够通过optgroup筛选搜索。当我搜索optgroup标签时,搜索结果(组的子级)消失了。

注意**我在示例图片中使用了不同的列表选项。但是结果应该相同。

我尝试对optgroup标签及其子对象可见的地方进行编程。

js小提琴示例:http://jsfiddle.net/ksweat/13sn45xL/23/

过滤之前的列表: List before filtering

在过滤器后面列出: List after filters

<select multiple="multiple" id="searchableb" name="my-select[]">      
  <optgroup label='Friends'>
    <option value='1'>Yoda</option>
    <option value='2' selected>Obiwan</option>
  </optgroup>
  <optgroup label='Enemies'>
    <option value='3'>Palpatine</option>
    <option value='4' disabled>Darth Vader</option>
  </optgroup>
</select>


$('.searchableb').multiSelect({
  selectableHeader: "<div class='custom-header'>All References</div><input type='text' class='search-input' autocomplete='off' placeholder=' Search '>",
  selectionHeader: "<div class='custom-header'>Assigned</div><input type='text' class='search-input' autocomplete='off' placeholder=' Search '>",
  enableClickableOptGroups: true,
  enableCollapsibleOptGroups: true,
  enableFiltering: true,
  includeSelectAllOption: true,
  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,{
      'show': function () {

        $(this).prev(".ms-optgroup-label").show();
        $(this).show();
      },
      'hide': function () {
        $(this).prev(".ms-optgroup-label").hide();
        $(this).hide();
      }
    })
      .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 (values) {
    let url = $('#typeEdit').val();
    // get removed emps
    let removedRefs = $('.removedRefs').val();
    removedRefs = removedRefs.split(',');
    console.log('values', values)
    let listid = values[0].split("- / -");

    // if an employee is reselected, find in string and remove
    var index = removedRefs.indexOf(listid[0]);
    if (index > -1) {
      removedRefs.splice(index, 1);
    }
    removedRefs = removedRefs.toString();
    console.log(removedRefs);
    this.qs1.cache();
    this.qs2.cache();
  },

  afterDeselect: function (values) {
    let url = $('#typeEdit').val();
    let action = false;
    let removedRefs = $('.removedRefs').val();
    let listid = values[0].split("- / -");
    console.log(listid[0])
    vals.push(listid[0]);
    $('.removedRefs').val(vals);


    console.log(vals)

    this.qs1.cache();
    this.qs2.cache();
  },
  selectableOptgroup: true
});

我希望当您搜索敌人时,应该会出现Palpatine和Darth Vader选项

双重传输列表示例均具有搜索选项: dual transfer list image

用于帮助搜索的当前JS:https://github.com/riklomas/quicksearch

1 个答案:

答案 0 :(得分:0)

此代码首先显示所有选项,然后隐藏与搜索不匹配的optgroup。

let searchstr = "Enemies";

document.querySelectorAll("#searchableb optgroup").forEach(function(e){
    e.style.display = "block";
});

document.querySelectorAll("#searchableb optgroup:not([label*='" + searchstr + "'])").forEach(function(e){
    e.style.display = "none";
});
<select multiple="multiple" id="searchableb" name="my-select[]">      
  <optgroup label='Friends'>
    <option value='1'>Yoda</option>
    <option value='2' selected>Obiwan</option>
  </optgroup>
  <optgroup label='Enemies'>
    <option value='3'>Palpatine</option>
    <option value='4' disabled>Darth Vader</option>
  </optgroup>
</select>