选择2 onselect选项将选择所有其他选项

时间:2017-12-27 03:30:01

标签: javascript jquery jquery-select2

我在JSFIDDLE中提供了一个选择2。如何选择一个选项调用All它将选择该选项字段中除自身之外的所有选项,这意味着All选项更像是全选按钮。并取消选择All。将取消选择所有选项。我从下面的jsfiddle提供相同的代码:

HTML:

<select class="parent_filter_select2 pull-right" id="parent_filter_select2" multiple="multiple" name="select_project" style="width: 300px;">
<option value="all">All</option>
        <option value="Option A">Option A</option>
        <option value="Option B">Option B</option>
        <option value="Option C">Option C</option>
        <option value="Option D">Option D</option>
</select>

Javascript:

$('#parent_filter_select2').select2({
    placeholder: 'Select'
});

非常感谢任何帮助,谢谢。

更新:我发现类似于我想要的东西,但它使用了复选框,所以我想知道如何在All选项而不是复选框上实现它,还有这个它也选择All选项,代码如下:

$("#checkbox").click(function(){
      if($("#checkbox").is(':checked') ){ //select all
        $("#parent_filter_select2").find('option').prop("selected",true);
        $("#parent_filter_select2").trigger('change');
      } else { //deselect all
        $("#parent_filter_select2").find('option').prop("selected",false);
        $("#parent_filter_select2").trigger('change');
      }
  });

3 个答案:

答案 0 :(得分:4)

Select2 github repo上有一个未解决的问题,请参阅:select all / select none header

根据&#39; bkdotcom&#39;用户(请参阅comment)您可以定义selectAllAdapter并将其与Select2(版本4)一起使用。

检查您的小提琴已更新:https://jsfiddle.net/beaver71/tjvjytp3/

或此片段:

&#13;
&#13;
/*
      Define the adapter so that it's reusable
*/         
$.fn.select2.amd.define('select2/selectAllAdapter', [
    'select2/utils',
    'select2/dropdown',
    'select2/dropdown/attachBody'
], function (Utils, Dropdown, AttachBody) {

    function SelectAll() { }
    SelectAll.prototype.render = function (decorated) {
        var self = this,
            $rendered = decorated.call(this),
            $selectAll = $(
                '<button class="btn btn-xs btn-default" type="button" style="margin-left:6px;"><i class="fa fa-check-square-o"></i> Select All</button>'
            ),
            $unselectAll = $(
                '<button class="btn btn-xs btn-default" type="button" style="margin-left:6px;"><i class="fa fa-square-o"></i> Unselect All</button>'
            ),
            $btnContainer = $('<div style="margin-top:3px;">').append($selectAll).append($unselectAll);
        if (!this.$element.prop("multiple")) {
            // this isn't a multi-select -> don't add the buttons!
            return $rendered;
        }
        $rendered.find('.select2-dropdown').prepend($btnContainer);
        $selectAll.on('click', function (e) {
            var $results = $rendered.find('.select2-results__option[aria-selected=false]');
            $results.each(function () {
                self.trigger('select', {
                    data: $(this).data('data')
                });
            });
            self.trigger('close');
        });
        $unselectAll.on('click', function (e) {
            var $results = $rendered.find('.select2-results__option[aria-selected=true]');
            $results.each(function () {
                self.trigger('unselect', {
                    data: $(this).data('data')
                });
            });
            self.trigger('close');
        });
        return $rendered;
    };

    return Utils.Decorate(
        Utils.Decorate(
            Dropdown,
            AttachBody
        ),
        SelectAll
    );

});

$('#parent_filter_select2').select2({
    placeholder: 'Select',
    dropdownAdapter: $.fn.select2.amd.require('select2/selectAllAdapter')
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.min.js"></script>

<select class="parent_filter_select2 pull-right" id="parent_filter_select2" multiple="multiple" name="select_project" style="width: 300px;">
        <option value="Option A">Option A</option>
        <option value="Option B">Option B</option>
        <option value="Option C">Option C</option>
        <option value="Option D">Option D</option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用

手动选择选项
$('#parent_filter_select2').select2('val', values);

从您的下拉菜单中选择选项All

您可以将values作为javascript数组。

 var values = new Array();
$('#parent_filter_select2').find('option').each(function () {
    var opt= $(this);
    var opvalue= opt.attr('value');
    if(opvalue != "all")
        values.push(opvalue);        
});

示例代码在这里。

  $('body').on('change', '#parent_filter_select2', function () {
 var val = $(this).val();
    //alert(val);
    if(val.indexOf('all') != -1){
            $('#parent_filter_select2').select2('val', values);
         }
        else{
            $('#parent_filter_select2').select2('val', val);
        }
});

你可以查看jsfiddle demo here

看看它有帮助..

答案 2 :(得分:1)

使用以下脚本

&#13;
&#13;
$("#parent_filter_select2 option").click(function () {
    var val = $(this).attr("value");
    if(val == 'all'){
        $('#parent_filter_select2 option').prop('selected', true);
        $('#parent_filter_select2 option[value="all"]').prop('selected', false);
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="parent_filter_select2 pull-right" id="parent_filter_select2" multiple="multiple" name="select_project" style="width: 300px;">
<option value="all">All</option>
        <option value="Option A">Option A</option>
        <option value="Option B">Option B</option>
        <option value="Option C">Option C</option>
        <option value="Option D">Option D</option>
</select>
&#13;
&#13;
&#13;