question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

check/uncheck methods does not handle singleSelect option

See original GitHub issue

Looking at the code, checkAll and uncheckAll methods are equivalent as clicking on the “select all” checkbox:

this.$container.off('click', '[name="btSelectAll"]')
.on('click', '[name="btSelectAll"]', function () {
    var checked = $(this).prop('checked');
    that[checked ? 'checkAll' : 'uncheckAll']();
    that.updateSelected();
});

This is nice.

The problem is the following: there is one code for check / uncheck methods and one different code for the click handler on rows checkboxes:

// check & uncheck methods

BootstrapTable.prototype.check = function (index) { this.check_(true, index); };

BootstrapTable.prototype.uncheck = function (index) { this.check_(false, index); };

BootstrapTable.prototype.check_ = function (checked, index) {
    var $el = this.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
    this.data[index][this.header.stateField] = checked;
    this.updateSelected();
    this.trigger(checked ? 'check' : 'uncheck', this.data[index], $el);
};
// click handler on checkboxes

this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
this.$selectItem.off('click').on('click', function (event) {
    event.stopImmediatePropagation();

    var $this = $(this),
        checked = $this.prop('checked'),
        row = that.data[$this.data('index')];

    if (that.options.maintainSelected && $(this).is(':radio')) {
        $.each(that.options.data, function (i, row) {
            row[that.header.stateField] = false;
        });
    }

    row[that.header.stateField] = checked;

    if (that.options.singleSelect) {
        that.$selectItem.not(this).each(function () {
            that.data[$(this).data('index')][that.header.stateField] = false;
        });
        that.$selectItem.filter(':checked').not(this).prop('checked', false);
    }

    that.updateSelected();
    that.trigger(checked ? 'check' : 'uncheck', row, $this);
});

Looking at this, we can see that check / uncheck methods doesn’t handle maintainSelected and singleSelect options, which can lead to have multiple rows selected even if singleSelect is set to true true.

Would be nice to have one single function for both (event handler and public method), what do you think ?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

Choosing an option to check/uncheck checkboxes not working?
Either make sure the default select selection and the default checkboxes match (as they do in your example with user1) or manually trigger ......
Read more >
How do I check/uncheck a checkbox input or radio button?
You can check or uncheck a checkbox element or a radio button using the .prop() method ... How do I get the text...
Read more >
How to Check and Uncheck Checkbox with JavaScript and ...
In this tutorial, you will read and learn about several JavaScript and jQuery methods for checking and unchecking a checkbox. Choose the best...
Read more >
<input type="checkbox"> - HTML: HyperText Markup Language
A checkbox allows you to select single values for submission in a ... If a checkbox is unchecked when its form is submitted,...
Read more >
How To Select The Check Box In Selenium With Examples
The checkbox can be turned on or off (that is checked or unchecked). A checked Checkbox is the one that is seen as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found