Bootstrap Selectpicker不会使用内置函数重置

时间:2022-08-24 16:28:29

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:

我有一组Bootstrap Selectpickers用于过滤数据库的结果。我需要一种方法将所有selectpickers重置为'Nothing Selected',这是我的代码:

HTML

<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');
});

Try to reset all the pickers:

尝试重置所有选择器:

$('#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.

正如你所看到的,我已经尝试了所有的功能来重置,但无济于事,所以显然做了一些错误的进一步逻辑。

2 个解决方案

#1


12  

I got solution from following code.Try it

我从下面的代码中得到了解决方案。试试吧

$("#listID").val('').trigger('change');

And also you can try this

而你也可以试试这个

$("#listID").val('').selectpicker('refresh');

#2


1  

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):

所以我查看了selectpicker.js文件,deselectAll和selectAll函数都通过几个参数过滤它们各自的选项(参见第884行):

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

A little breakdown:

一点细分:

.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!?

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:

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

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

这$ lis.not( '除法 ')不筛选发现( 'A')点击()。(' 禁用 ')。(' 选择')。

#1


12  

I got solution from following code.Try it

我从下面的代码中得到了解决方案。试试吧

$("#listID").val('').trigger('change');

And also you can try this

而你也可以试试这个

$("#listID").val('').selectpicker('refresh');

#2


1  

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):

所以我查看了selectpicker.js文件,deselectAll和selectAll函数都通过几个参数过滤它们各自的选项(参见第884行):

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

A little breakdown:

一点细分:

.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!?

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:

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

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

这$ lis.not( '除法 ')不筛选发现( 'A')点击()。(' 禁用 ')。(' 选择')。