Bootstrap multiselect选择instanciation中的所有选项

时间:2016-10-10 16:29:26

标签: javascript jquery twitter-bootstrap

我正在使用bootstrap multiselect插件将动态代码添加到select中。这是我的代码:

HTML

<label>
    <span>Underlyings</span>
    <select class="multiselect" multiple></select>
</label>

的Javascript

var name = ['joe', 'mary', 'rose'];

R.map(function (x) {
    return $('.multiselect', d.el).append("<option>" + x + "</option>");
}, name);

$('.multiselect', d.el).multiselect({
    allSelectedText: 'All',
    maxHeight: 200,
    includeSelectAllOption: true
});

当多重选择被实例化时,它会在浏览器中显示(有一些css格式说明其方面):

enter image description here

我希望它显示为(在实例中选中所有复选框,而不是点击'全选'):

enter image description here

我查看了文档,但没找到它......

Bootstrap multiple select documentation

4 个答案:

答案 0 :(得分:10)

您需要同时运行selectAllfalse作为第二个参数 - 这表示将选择所有值,甚至是不可见的值)和updateButtonText(更改文本出现在下拉菜单中。)

检查此示例:

&#13;
&#13;
$(function() {
  var name = ['joe', 'mary', 'rose'];
  $.map(name, function (x) {
    return $('.multiselect').append("<option>" + x + "</option>");
  });
  
  $('.multiselect')
    .multiselect({
      allSelectedText: 'All',
      maxHeight: 200,
      includeSelectAllOption: true
    })
    .multiselect('selectAll', false)
    .multiselect('updateButtonText');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.min.js"></script>

<label>
    <span>Underlyings</span>
    <select class="multiselect" multiple="multiple"></select>
</label>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果要刷新多重引导,请尝试:

$('[data-plugin-multiselect]').multiselect('destroy').multiselect();

答案 2 :(得分:0)

如果你想刷新bootstrap multi select那么你可以尝试三行代码: $("#YourButtonId").click(function () {
$("#YourMultiSelectId").multiselect("deselectAll", false); $("#YourMultiSelectId").multiselect('updateButtonText'); });

答案 3 :(得分:0)

因为我有来自api或JSON数据的动态列,所以这里是可行的。

            var results = response;
            if (results.length > 0) {
                var columnsIn = results[0];

                for (var key in columnsIn) {
                    var col = new Object();

                    col.data = key;
                    col.title = key;
                    col.value = key;
                    col.label = key;
                    var htm = '';
                    htm += '<option selected>' + col.value + '</option>';

                    $('#dropdownname').append(htm);
                }

                $('#dropdownname').multiselect("rebuild");