How to create Angular mutually exclusive checkboxes and filter

Discussão em 'Angular' iniciado por spring_hiber, Março 31, 2025.

  1. spring_hiber

    spring_hiber Guest

    I have 4 checkboxes that filter based on its name/identifierValue. When the page loads ALL should be selected. If I click any other box 'ALL' should deselect and I then filter based on the selection. Then, if I click ALL all of the other checkboxes should deselect but ALL identifiers should show.

    Currently, I set the checkboxes to true or false via FormGroup in parent component. I then listen to form changes to hanlde the logic.

    The problem is I have to uncheck ALL in order to select another box.

    Then when I click ALL after selecting other boxes they deselect properly but all items in the table dont show as expected. After that I have to toggle all of the other boxes just to get the filtering back working again.

    Here is the method I'm calling via ngOnInit by way of initializeSearchPage().

    handleCheckboxSelection(values: any) {
    const allSelected = values.ALL;
    const checkBox1Selected = values.Checkbox1;
    const checkBox2Selected = values.Checkbox2;
    const checkBox3Selected = values.Checkbox3;

    if (allSelected) {
    // If "All" is selected, deselect others
    this.reportListFG.get('filterGroup')?.patchValue(
    {
    Checkbox1: false,
    Checkbox2: false,
    Checkbox3: false,
    },
    { emitEvent: false } // Prevent infinite loop
    );
    } else {
    // If "All" is not selected, deselect "All" if any other is selected
    if (checkBox1Selected || checkBox2Selected || checkBox3Selected) {
    this.reportListFG.get('filterGroup')?.patchValue(
    {
    ALL: false,
    },
    { emitEvent: false }
    );
    }




    Here is my stackblitz

    Continue reading...

Compartilhe esta Página