简体   繁体   English

Select2 当另一个选择时删除默认选项

[英]Select2 remove default option when another selected

I'd like to have a default option act like a placeholder.我希望有一个默认选项像占位符一样。

I'm doing an ajax search for Products.我正在对产品进行 ajax 搜索。 I want my input box to show "All" until I've selected a product.我希望我的输入框显示“全部”,直到我选择了一个产品。

This will be the default view:这将是默认视图:

在此处输入图片说明

But as soon as I select a product, I want All to disappear and just the product to show:但是一旦我选择了一个产品,我就希望 All 消失,只显示产品: 在此处输入图片说明

I've put together a codepen to hopefully get things rolling!我已经组装了一个代码笔,希望能让事情顺利进行!

Thank you all!谢谢你们!

https://codepen.io/saltcod/pen/bGdzoGN https://codepen.io/saltcod/pen/bGdzoGN

HTML: HTML:

<div class="container">

  <select class="form-control am-product-selector__product-list-select" name="choices-multiple-default" id="choices-multiple-default" placeholder="All Products" multiple>
    <option value="All" selected>All</option>

  </select>

</div>

And the JS:和JS:


const options = [{ id: 1, text: 'All' }];

const select2Instance = jQuery( '#choices-multiple-default' ).select2( {
        placeholder: 'Search products',
        ajax: {
            url: url,
            dataType: 'json',
            delay: 250,

            data: params => {
                return {
                    //term: params.term // search query
                };
            },

            // Process fetched results
            processResults: data => {
                if ( data ) {
                    data.map( item => {
                        options.push( { id: item.id, text: item.title } );
                    } );
                }

                return {
                    results: options
                };
            },
            cache: true
        },
        minimumInputLength: 3 // the minimum of symbols to input before perform a search
    } );

We can use the change event of native select elements and its scoped version change.select2 .我们可以使用原生select元素的change事件及其作用域版本change.select2

When adding select2 plugin on an element, the change event is triggered whenever an option is selected or removed.在元素上添加select2插件时,只要选择或删除选项,就会触发change事件。

So, when an option from the async list is added, we should remove the all option.因此,当添加异步列表中的选项时,我们应该删除all选项。 Also, when all options are deleted (the select has no value), we should re-add the all option.另外,当所有选项都被删除后(select没有值),我们应该重新添加all选项。 But we need to inform the select2 about the changes, so we trigger change.select2 event.但是我们需要通知select2有关更改的信息,因此我们触发了change.select2事件。

The full code is:完整代码是:

select2Instance.on('change', function () {
  const values = $(this).val();
  if (values.length > 1) {
    const index = values.indexOf('all');
    if (index > -1) {
      values.splice(index, 1);
      $(this).val(values);
    }
  } else if (values.length === 0) {
    $(this).val('all');
  }
  $(this).trigger('change.select2'); // Notify only Select2 of changes;
});

Codepen demo: https://codepen.io/andreivictor/pen/yLNZzbb Codepen 演示: https ://codepen.io/andreivictor/pen/yLNZzbb

More info about Select2 Events:有关 Select2 事件的更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM