Bootstrap Selectpicker不会使用内置功能重置 [英] Bootstrap Selectpicker will not reset using built-in functions

查看:493
本文介绍了Bootstrap Selectpicker不会使用内置功能重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Bootstrap Selectpickers数组,用于过滤数据库中的结果.我需要一种将所有选择器重置为没有选择"的方法,这是我的代码:

I have an array of Bootstrap Selectpickers for filtering results from a database. I need a way of resetting all the selectpickers to 'Nothing Selected', this is my code:

<div class="row">
    <div class="col-md-3">
        <label>By Group</label>
        <select id="groups" name="group" class="form-control selectpicker" multiple></select>
    </div>
    <div class="col-md-3">
        etc...
    </div>
</div>

JS

ajax_fetch('build_group_options', {groupno:groupno}).done(function(html) {
    //var html is a list of options in html format
    $('#groups').html(html).find('option[value=""]').remove();
    //refresh the selectpicker to make sure options are registered in the picker
    $('.selectpicker').selectpicker('refresh');
});

尝试重置所有选择器:

$('#reset_filters').click(function() {
    $('.selectpicker').selectpicker('deselectAll');
    $('.selectpicker').selectpicker('render');
    $('.selectpicker').selectpicker('refresh');
    $(this).closest('form').find('.selectpicker').each(function() {
        $(this).selectpicker('render');
    });
});

如您所见,我已经尝试过重置所有功能,但无济于事,因此显然在进一步的逻辑上做了一些错误.

As you can see I have tried all the functions to reset but to no avail so am obviously doing some wrong further up the logic.

推荐答案

所以我查看了selectpicker.js文件,deselectAllselectAll函数都通过一些参数过滤了各自的选项(请参见第884行) :

So I looked in the selectpicker.js file, the deselectAll and selectAll functions both filter their respective options by a few arguments (see line 884):

deselectAll: function () {
  this.findLis();
  this.$lis.not('.divider').not('.disabled').filter('.selected').filter(':visible').find('a').click();
}

一些细目:

.not('.divider') //prevents the divider receiving a click event! 
.not('.disabled') //ignore any disabled elements
.filter('.selected') / .not('.selected') //depending if its selectAll() or deselectAll()
.filter(':visible') //prevent any non-visible element receiving a click event!?

我的问题是.filter(':visible'),当触发click事件时,该列表不可见,因此这些选项被过滤掉,因此没有被单击"/取消选择". 我修改了插件的版本,现在我的重置"按钮可以正常工作了.新行是:

My problem was the .filter(':visible'), the list was not visible when the click event was triggered so these options were filtered out and therefore did not get 'clicked'/'deselected'. I amended my version of the plugin and now my 'reset' button works as expected. The new line is:

this.$lis.not('.divider').not('.disabled').filter('.selected').find('a').click();

这篇关于Bootstrap Selectpicker不会使用内置功能重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆