Disable Secondary Checkbox if First is Checked

Disable Secondary Checkbox if First is Checked

potato_codepotato_code Posts: 14Questions: 5Answers: 0

Hi,

I'm using the select extension. I have rows with two checkboxes. What I'm trying to accomplish is:

When one checkbox is checked, the other is disabled. If the user want to change their selection they must first deselect their first selection.

Does anyone have an example?

here is my code:
live.datatables.net/yakucafu/1/

Thanks!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,182Questions: 26Answers: 4,925
    edited July 2021

    I'm using the select extension.

    Your example doesn't have the select extension. Is it part of your checkbox solution?

    Assuming your checkboxes aren't apart of the select extension solution and they are just normal checkboxes you can use standard jQuery methods, for the most part, to get the state for all checkboxes. You will need to use cells().nodes() to get all the td nodes that contain checkboxes. The reason is the DOM only has the rows being displayed. I updated the test case so there are two pages to show this.

    This is one way this can be done with standard checkboxes:
    http://live.datatables.net/yakucafu/2/edit

    Kevin

  • potato_codepotato_code Posts: 14Questions: 5Answers: 0
    edited July 2021

    Hi,

    My mistake, I've updated my test case to show that I'm using Select.

    Sorry if my question wasn't clear but I meant that each row would only be allowed to check one or the other, not the entire table

    updated test case:
    live.datatables.net/yakucafu/3/

  • kthorngrenkthorngren Posts: 21,182Questions: 26Answers: 4,925
    edited July 2021 Answer ✓

    First you need to move the select.js include below datatables.js. Next you can get the row in the click event then use that as the row-selector for the cells() API. Updated example:
    http://live.datatables.net/yakucafu/4/edit

    You may want to use select.selector to control which columns can be used to select the rows. Note that I change the row selector to use the row variable instead of null.

    var numChecked = $( 'input[type=checkbox]:checked', table.cells(row, [1,2]).nodes() ).length;
    

    Kevin

  • potato_codepotato_code Posts: 14Questions: 5Answers: 0
    edited July 2021

    Thanks. Everything worked but I had to change the final line to:

    $(this).prop('checked', false);

Sign In or Register to comment.