How can i enable the button on checked the single checkbox in the Mat table?












0















I need to enable the submit button if checked the single check box from the "td" tag and its working fine when checked the master checkbox from "th" tag. Am using angular material. thanks in advance.



Find the Stackblitz link: https://angular-material2-5cgxqf.stackblitz.io/



app.component.html



<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()"></mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"></mat-checkbox>
</td>
</ng-container>
</table>
/**Button*/

<button [disabled]="isButtonEnable">Submit</button>


app.component.ts



import {SelectionModel} from '@angular/cdk/collections';
import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';

isButtonEnable:boolean =true;
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}

/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
if(this.isAllSelected()){
this.selection.clear();
this.isButtonEnable = true;
}else{
this.dataSource.data.forEach(row => this.selection.select(row));
this.isButtonEnable = false;
}
}









share|improve this question

























  • Create the stackblitz demo

    – Sunil Singh
    Nov 14 '18 at 15:20











  • @SunilSingh Added example link

    – Mathi
    Nov 15 '18 at 5:09











  • @Mathi please provide editor url of the stackblitz

    – Javascript Lover - SKT
    Nov 15 '18 at 5:16











  • @SunilSingh stackblitz.com/edit/…

    – Mathi
    Nov 15 '18 at 5:18
















0















I need to enable the submit button if checked the single check box from the "td" tag and its working fine when checked the master checkbox from "th" tag. Am using angular material. thanks in advance.



Find the Stackblitz link: https://angular-material2-5cgxqf.stackblitz.io/



app.component.html



<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()"></mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"></mat-checkbox>
</td>
</ng-container>
</table>
/**Button*/

<button [disabled]="isButtonEnable">Submit</button>


app.component.ts



import {SelectionModel} from '@angular/cdk/collections';
import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';

isButtonEnable:boolean =true;
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}

/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
if(this.isAllSelected()){
this.selection.clear();
this.isButtonEnable = true;
}else{
this.dataSource.data.forEach(row => this.selection.select(row));
this.isButtonEnable = false;
}
}









share|improve this question

























  • Create the stackblitz demo

    – Sunil Singh
    Nov 14 '18 at 15:20











  • @SunilSingh Added example link

    – Mathi
    Nov 15 '18 at 5:09











  • @Mathi please provide editor url of the stackblitz

    – Javascript Lover - SKT
    Nov 15 '18 at 5:16











  • @SunilSingh stackblitz.com/edit/…

    – Mathi
    Nov 15 '18 at 5:18














0












0








0








I need to enable the submit button if checked the single check box from the "td" tag and its working fine when checked the master checkbox from "th" tag. Am using angular material. thanks in advance.



Find the Stackblitz link: https://angular-material2-5cgxqf.stackblitz.io/



app.component.html



<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()"></mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"></mat-checkbox>
</td>
</ng-container>
</table>
/**Button*/

<button [disabled]="isButtonEnable">Submit</button>


app.component.ts



import {SelectionModel} from '@angular/cdk/collections';
import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';

isButtonEnable:boolean =true;
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}

/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
if(this.isAllSelected()){
this.selection.clear();
this.isButtonEnable = true;
}else{
this.dataSource.data.forEach(row => this.selection.select(row));
this.isButtonEnable = false;
}
}









share|improve this question
















I need to enable the submit button if checked the single check box from the "td" tag and its working fine when checked the master checkbox from "th" tag. Am using angular material. thanks in advance.



Find the Stackblitz link: https://angular-material2-5cgxqf.stackblitz.io/



app.component.html



<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()"></mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"></mat-checkbox>
</td>
</ng-container>
</table>
/**Button*/

<button [disabled]="isButtonEnable">Submit</button>


app.component.ts



import {SelectionModel} from '@angular/cdk/collections';
import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';

isButtonEnable:boolean =true;
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}

/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
if(this.isAllSelected()){
this.selection.clear();
this.isButtonEnable = true;
}else{
this.dataSource.data.forEach(row => this.selection.select(row));
this.isButtonEnable = false;
}
}






angular angular6 typescript2.0 angular-material-6 mat-table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:09







Mathi

















asked Nov 14 '18 at 14:50









MathiMathi

4119




4119













  • Create the stackblitz demo

    – Sunil Singh
    Nov 14 '18 at 15:20











  • @SunilSingh Added example link

    – Mathi
    Nov 15 '18 at 5:09











  • @Mathi please provide editor url of the stackblitz

    – Javascript Lover - SKT
    Nov 15 '18 at 5:16











  • @SunilSingh stackblitz.com/edit/…

    – Mathi
    Nov 15 '18 at 5:18



















  • Create the stackblitz demo

    – Sunil Singh
    Nov 14 '18 at 15:20











  • @SunilSingh Added example link

    – Mathi
    Nov 15 '18 at 5:09











  • @Mathi please provide editor url of the stackblitz

    – Javascript Lover - SKT
    Nov 15 '18 at 5:16











  • @SunilSingh stackblitz.com/edit/…

    – Mathi
    Nov 15 '18 at 5:18

















Create the stackblitz demo

– Sunil Singh
Nov 14 '18 at 15:20





Create the stackblitz demo

– Sunil Singh
Nov 14 '18 at 15:20













@SunilSingh Added example link

– Mathi
Nov 15 '18 at 5:09





@SunilSingh Added example link

– Mathi
Nov 15 '18 at 5:09













@Mathi please provide editor url of the stackblitz

– Javascript Lover - SKT
Nov 15 '18 at 5:16





@Mathi please provide editor url of the stackblitz

– Javascript Lover - SKT
Nov 15 '18 at 5:16













@SunilSingh stackblitz.com/edit/…

– Mathi
Nov 15 '18 at 5:18





@SunilSingh stackblitz.com/edit/…

– Mathi
Nov 15 '18 at 5:18












2 Answers
2






active

oldest

votes


















1














You should check for any change in any checkbox. If any of the checkbox is selected then it should enable the submit button. Add the below snippet.



  constructor(){
this.selection.changed.subscribe(item=>{
this.isButtonEnable = this.selection.selected.length == 0;
})
}


Here is the working demo - https://stackblitz.com/edit/angular-material2-p8r4pe






share|improve this answer
























  • its working fine and also let me know about the below solution. whether its too correct or not.

    – Mathi
    Nov 15 '18 at 5:43











  • That is the same solution.

    – Sunil Singh
    Nov 15 '18 at 5:49











  • okay Thank you.

    – Mathi
    Nov 15 '18 at 5:52



















0














app.component.html



<mat-checkbox (click)="SingleSelection(); $event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-checkbox>


app.component.ts



SingleSelection(){
const numSelected = this.selection.selected.length;
numSelected <= 0 ? this.isButtonEnable = false : this.isButtonEnable = true;
}


this also working fine and please find the link: https://stackblitz.com/edit/angular-material2-p8r4pe?file=app%2Fapp.component.ts






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302949%2fhow-can-i-enable-the-button-on-checked-the-single-checkbox-in-the-mat-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should check for any change in any checkbox. If any of the checkbox is selected then it should enable the submit button. Add the below snippet.



      constructor(){
    this.selection.changed.subscribe(item=>{
    this.isButtonEnable = this.selection.selected.length == 0;
    })
    }


    Here is the working demo - https://stackblitz.com/edit/angular-material2-p8r4pe






    share|improve this answer
























    • its working fine and also let me know about the below solution. whether its too correct or not.

      – Mathi
      Nov 15 '18 at 5:43











    • That is the same solution.

      – Sunil Singh
      Nov 15 '18 at 5:49











    • okay Thank you.

      – Mathi
      Nov 15 '18 at 5:52
















    1














    You should check for any change in any checkbox. If any of the checkbox is selected then it should enable the submit button. Add the below snippet.



      constructor(){
    this.selection.changed.subscribe(item=>{
    this.isButtonEnable = this.selection.selected.length == 0;
    })
    }


    Here is the working demo - https://stackblitz.com/edit/angular-material2-p8r4pe






    share|improve this answer
























    • its working fine and also let me know about the below solution. whether its too correct or not.

      – Mathi
      Nov 15 '18 at 5:43











    • That is the same solution.

      – Sunil Singh
      Nov 15 '18 at 5:49











    • okay Thank you.

      – Mathi
      Nov 15 '18 at 5:52














    1












    1








    1







    You should check for any change in any checkbox. If any of the checkbox is selected then it should enable the submit button. Add the below snippet.



      constructor(){
    this.selection.changed.subscribe(item=>{
    this.isButtonEnable = this.selection.selected.length == 0;
    })
    }


    Here is the working demo - https://stackblitz.com/edit/angular-material2-p8r4pe






    share|improve this answer













    You should check for any change in any checkbox. If any of the checkbox is selected then it should enable the submit button. Add the below snippet.



      constructor(){
    this.selection.changed.subscribe(item=>{
    this.isButtonEnable = this.selection.selected.length == 0;
    })
    }


    Here is the working demo - https://stackblitz.com/edit/angular-material2-p8r4pe







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 5:32









    Sunil SinghSunil Singh

    6,3572627




    6,3572627













    • its working fine and also let me know about the below solution. whether its too correct or not.

      – Mathi
      Nov 15 '18 at 5:43











    • That is the same solution.

      – Sunil Singh
      Nov 15 '18 at 5:49











    • okay Thank you.

      – Mathi
      Nov 15 '18 at 5:52



















    • its working fine and also let me know about the below solution. whether its too correct or not.

      – Mathi
      Nov 15 '18 at 5:43











    • That is the same solution.

      – Sunil Singh
      Nov 15 '18 at 5:49











    • okay Thank you.

      – Mathi
      Nov 15 '18 at 5:52

















    its working fine and also let me know about the below solution. whether its too correct or not.

    – Mathi
    Nov 15 '18 at 5:43





    its working fine and also let me know about the below solution. whether its too correct or not.

    – Mathi
    Nov 15 '18 at 5:43













    That is the same solution.

    – Sunil Singh
    Nov 15 '18 at 5:49





    That is the same solution.

    – Sunil Singh
    Nov 15 '18 at 5:49













    okay Thank you.

    – Mathi
    Nov 15 '18 at 5:52





    okay Thank you.

    – Mathi
    Nov 15 '18 at 5:52













    0














    app.component.html



    <mat-checkbox (click)="SingleSelection(); $event.stopPropagation()"
    (change)="$event ? selection.toggle(row) : null"
    [checked]="selection.isSelected(row)">
    </mat-checkbox>


    app.component.ts



    SingleSelection(){
    const numSelected = this.selection.selected.length;
    numSelected <= 0 ? this.isButtonEnable = false : this.isButtonEnable = true;
    }


    this also working fine and please find the link: https://stackblitz.com/edit/angular-material2-p8r4pe?file=app%2Fapp.component.ts






    share|improve this answer




























      0














      app.component.html



      <mat-checkbox (click)="SingleSelection(); $event.stopPropagation()"
      (change)="$event ? selection.toggle(row) : null"
      [checked]="selection.isSelected(row)">
      </mat-checkbox>


      app.component.ts



      SingleSelection(){
      const numSelected = this.selection.selected.length;
      numSelected <= 0 ? this.isButtonEnable = false : this.isButtonEnable = true;
      }


      this also working fine and please find the link: https://stackblitz.com/edit/angular-material2-p8r4pe?file=app%2Fapp.component.ts






      share|improve this answer


























        0












        0








        0







        app.component.html



        <mat-checkbox (click)="SingleSelection(); $event.stopPropagation()"
        (change)="$event ? selection.toggle(row) : null"
        [checked]="selection.isSelected(row)">
        </mat-checkbox>


        app.component.ts



        SingleSelection(){
        const numSelected = this.selection.selected.length;
        numSelected <= 0 ? this.isButtonEnable = false : this.isButtonEnable = true;
        }


        this also working fine and please find the link: https://stackblitz.com/edit/angular-material2-p8r4pe?file=app%2Fapp.component.ts






        share|improve this answer













        app.component.html



        <mat-checkbox (click)="SingleSelection(); $event.stopPropagation()"
        (change)="$event ? selection.toggle(row) : null"
        [checked]="selection.isSelected(row)">
        </mat-checkbox>


        app.component.ts



        SingleSelection(){
        const numSelected = this.selection.selected.length;
        numSelected <= 0 ? this.isButtonEnable = false : this.isButtonEnable = true;
        }


        this also working fine and please find the link: https://stackblitz.com/edit/angular-material2-p8r4pe?file=app%2Fapp.component.ts







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 5:46









        MathiMathi

        4119




        4119






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302949%2fhow-can-i-enable-the-button-on-checked-the-single-checkbox-in-the-mat-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine