Open close panel by Id material accordion












1















Am I able to control, hence open/close individual panels by an assigned Id using the Material accordion, https://material.angular.io/components/expansion/overview#accordion.










share|improve this question























  • can you share some code, what you have tried so far and also provide a minimal stackblitz.com example of your scenario.

    – Farhat Zaman
    Jan 1 at 3:22











  • Can you explain what do you mean by Open close panel by Id material accordion

    – Prashant Pimpale
    Jan 1 at 7:15











  • I meant dynamically creating the accordion in the code-behind, whilst assigning a unique id to each header. Hence enabling the ability to open and close individual panels programatically. I believe the ng-bootstrap accordion is capable of this, however I would like to stick to the MatAccordion, rather than install/use multiple component libraries.

    – vicgoyso
    Jan 1 at 13:25


















1















Am I able to control, hence open/close individual panels by an assigned Id using the Material accordion, https://material.angular.io/components/expansion/overview#accordion.










share|improve this question























  • can you share some code, what you have tried so far and also provide a minimal stackblitz.com example of your scenario.

    – Farhat Zaman
    Jan 1 at 3:22











  • Can you explain what do you mean by Open close panel by Id material accordion

    – Prashant Pimpale
    Jan 1 at 7:15











  • I meant dynamically creating the accordion in the code-behind, whilst assigning a unique id to each header. Hence enabling the ability to open and close individual panels programatically. I believe the ng-bootstrap accordion is capable of this, however I would like to stick to the MatAccordion, rather than install/use multiple component libraries.

    – vicgoyso
    Jan 1 at 13:25
















1












1








1








Am I able to control, hence open/close individual panels by an assigned Id using the Material accordion, https://material.angular.io/components/expansion/overview#accordion.










share|improve this question














Am I able to control, hence open/close individual panels by an assigned Id using the Material accordion, https://material.angular.io/components/expansion/overview#accordion.







angular angular-material accordion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 1:14









vicgoysovicgoyso

236220




236220













  • can you share some code, what you have tried so far and also provide a minimal stackblitz.com example of your scenario.

    – Farhat Zaman
    Jan 1 at 3:22











  • Can you explain what do you mean by Open close panel by Id material accordion

    – Prashant Pimpale
    Jan 1 at 7:15











  • I meant dynamically creating the accordion in the code-behind, whilst assigning a unique id to each header. Hence enabling the ability to open and close individual panels programatically. I believe the ng-bootstrap accordion is capable of this, however I would like to stick to the MatAccordion, rather than install/use multiple component libraries.

    – vicgoyso
    Jan 1 at 13:25





















  • can you share some code, what you have tried so far and also provide a minimal stackblitz.com example of your scenario.

    – Farhat Zaman
    Jan 1 at 3:22











  • Can you explain what do you mean by Open close panel by Id material accordion

    – Prashant Pimpale
    Jan 1 at 7:15











  • I meant dynamically creating the accordion in the code-behind, whilst assigning a unique id to each header. Hence enabling the ability to open and close individual panels programatically. I believe the ng-bootstrap accordion is capable of this, however I would like to stick to the MatAccordion, rather than install/use multiple component libraries.

    – vicgoyso
    Jan 1 at 13:25



















can you share some code, what you have tried so far and also provide a minimal stackblitz.com example of your scenario.

– Farhat Zaman
Jan 1 at 3:22





can you share some code, what you have tried so far and also provide a minimal stackblitz.com example of your scenario.

– Farhat Zaman
Jan 1 at 3:22













Can you explain what do you mean by Open close panel by Id material accordion

– Prashant Pimpale
Jan 1 at 7:15





Can you explain what do you mean by Open close panel by Id material accordion

– Prashant Pimpale
Jan 1 at 7:15













I meant dynamically creating the accordion in the code-behind, whilst assigning a unique id to each header. Hence enabling the ability to open and close individual panels programatically. I believe the ng-bootstrap accordion is capable of this, however I would like to stick to the MatAccordion, rather than install/use multiple component libraries.

– vicgoyso
Jan 1 at 13:25







I meant dynamically creating the accordion in the code-behind, whilst assigning a unique id to each header. Hence enabling the ability to open and close individual panels programatically. I believe the ng-bootstrap accordion is capable of this, however I would like to stick to the MatAccordion, rather than install/use multiple component libraries.

– vicgoyso
Jan 1 at 13:25














2 Answers
2






active

oldest

votes


















0














Okay, I tried a solution, StackBlitz HERE.



I've not find a way to use an ID for panel control, but you can use a variable.
Here, I use activePanel which takes a value depending on the event (1 for the button_1, 2 for the button_2, ...).



<button mat-raised-button (click)="activePanel = 1">Open 1</button>
<button mat-raised-button (click)="activePanel = 2">Open 2</button>
<button mat-raised-button (click)="activePanel = 3">Open 3</button>


In your panel you use the function ngOnChanges which will open the panel according to your variable.



ngOnChanges() {
if(this.panelState2 == 2) {this.panelState = true;}
else {this.panelState = false;}
}


The panelState variable is a boolean to open or close your panel with [expanded].



<mat-expansion-panel [expanded]="panelState">
<mat-expansion-panel-header>
<mat-panel-title>
Title Panel
</mat-panel-title>
</mat-expansion-panel-header>
<p>Accedat huc suavitas quaedam oportet sermonum atque morum,...</p>
</mat-expansion-panel>


I hope this will help you.



DEMO:



enter image description here






share|improve this answer



















  • 1





    Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

    – vicgoyso
    Jan 25 at 7:27





















0














Ok, so I tried another solution, StackBlitz HERE.



This time I managed to use an ID for panel control.
Here, I use a unique ID on each mat-expansion-panel (#first for the panel_1, #second for the panel_2, ...).



<mat-accordion multi="true">
<mat-expansion-panel #first></mat-expansion-panel>
<mat-expansion-panel #second></mat-expansion-panel>
<mat-expansion-panel #third></mat-expansion-panel>
</mat-accordion>


I also used buttons to control these panels without interacting with them.



<button mat-raised-button (click)="changeState1()">ON/OFF Panel_1</button>
<button mat-raised-button (click)="changeState2()">ON/OFF Panel_2</button>
<button mat-raised-button (click)="changeState3()">ON/OFF Panel_3</button>


In your TypeScript code, you use @ViewChild to retrieve the panel ID so you can control it.



@ViewChild('first') first: MatExpansionPanel;
@ViewChild('second') second: MatExpansionPanel;
@ViewChild('third') third: MatExpansionPanel;


Then, I created functions to open one to close the mat-expansion-panel according to its ID and according to its state.



changeState1() {
if(this.first.expanded){
this.first.close();
} else {
this.first.open();
}
}

changeState2() {...}


PS: <mat-accordion multi="true"> allows multiple panels to open simultaneously.



DEMO:



enter image description here






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%2f53992454%2fopen-close-panel-by-id-material-accordion%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









    0














    Okay, I tried a solution, StackBlitz HERE.



    I've not find a way to use an ID for panel control, but you can use a variable.
    Here, I use activePanel which takes a value depending on the event (1 for the button_1, 2 for the button_2, ...).



    <button mat-raised-button (click)="activePanel = 1">Open 1</button>
    <button mat-raised-button (click)="activePanel = 2">Open 2</button>
    <button mat-raised-button (click)="activePanel = 3">Open 3</button>


    In your panel you use the function ngOnChanges which will open the panel according to your variable.



    ngOnChanges() {
    if(this.panelState2 == 2) {this.panelState = true;}
    else {this.panelState = false;}
    }


    The panelState variable is a boolean to open or close your panel with [expanded].



    <mat-expansion-panel [expanded]="panelState">
    <mat-expansion-panel-header>
    <mat-panel-title>
    Title Panel
    </mat-panel-title>
    </mat-expansion-panel-header>
    <p>Accedat huc suavitas quaedam oportet sermonum atque morum,...</p>
    </mat-expansion-panel>


    I hope this will help you.



    DEMO:



    enter image description here






    share|improve this answer



















    • 1





      Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

      – vicgoyso
      Jan 25 at 7:27


















    0














    Okay, I tried a solution, StackBlitz HERE.



    I've not find a way to use an ID for panel control, but you can use a variable.
    Here, I use activePanel which takes a value depending on the event (1 for the button_1, 2 for the button_2, ...).



    <button mat-raised-button (click)="activePanel = 1">Open 1</button>
    <button mat-raised-button (click)="activePanel = 2">Open 2</button>
    <button mat-raised-button (click)="activePanel = 3">Open 3</button>


    In your panel you use the function ngOnChanges which will open the panel according to your variable.



    ngOnChanges() {
    if(this.panelState2 == 2) {this.panelState = true;}
    else {this.panelState = false;}
    }


    The panelState variable is a boolean to open or close your panel with [expanded].



    <mat-expansion-panel [expanded]="panelState">
    <mat-expansion-panel-header>
    <mat-panel-title>
    Title Panel
    </mat-panel-title>
    </mat-expansion-panel-header>
    <p>Accedat huc suavitas quaedam oportet sermonum atque morum,...</p>
    </mat-expansion-panel>


    I hope this will help you.



    DEMO:



    enter image description here






    share|improve this answer



















    • 1





      Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

      – vicgoyso
      Jan 25 at 7:27
















    0












    0








    0







    Okay, I tried a solution, StackBlitz HERE.



    I've not find a way to use an ID for panel control, but you can use a variable.
    Here, I use activePanel which takes a value depending on the event (1 for the button_1, 2 for the button_2, ...).



    <button mat-raised-button (click)="activePanel = 1">Open 1</button>
    <button mat-raised-button (click)="activePanel = 2">Open 2</button>
    <button mat-raised-button (click)="activePanel = 3">Open 3</button>


    In your panel you use the function ngOnChanges which will open the panel according to your variable.



    ngOnChanges() {
    if(this.panelState2 == 2) {this.panelState = true;}
    else {this.panelState = false;}
    }


    The panelState variable is a boolean to open or close your panel with [expanded].



    <mat-expansion-panel [expanded]="panelState">
    <mat-expansion-panel-header>
    <mat-panel-title>
    Title Panel
    </mat-panel-title>
    </mat-expansion-panel-header>
    <p>Accedat huc suavitas quaedam oportet sermonum atque morum,...</p>
    </mat-expansion-panel>


    I hope this will help you.



    DEMO:



    enter image description here






    share|improve this answer













    Okay, I tried a solution, StackBlitz HERE.



    I've not find a way to use an ID for panel control, but you can use a variable.
    Here, I use activePanel which takes a value depending on the event (1 for the button_1, 2 for the button_2, ...).



    <button mat-raised-button (click)="activePanel = 1">Open 1</button>
    <button mat-raised-button (click)="activePanel = 2">Open 2</button>
    <button mat-raised-button (click)="activePanel = 3">Open 3</button>


    In your panel you use the function ngOnChanges which will open the panel according to your variable.



    ngOnChanges() {
    if(this.panelState2 == 2) {this.panelState = true;}
    else {this.panelState = false;}
    }


    The panelState variable is a boolean to open or close your panel with [expanded].



    <mat-expansion-panel [expanded]="panelState">
    <mat-expansion-panel-header>
    <mat-panel-title>
    Title Panel
    </mat-panel-title>
    </mat-expansion-panel-header>
    <p>Accedat huc suavitas quaedam oportet sermonum atque morum,...</p>
    </mat-expansion-panel>


    I hope this will help you.



    DEMO:



    enter image description here







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 22 at 10:46









    QuentinQuentin

    335112




    335112








    • 1





      Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

      – vicgoyso
      Jan 25 at 7:27
















    • 1





      Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

      – vicgoyso
      Jan 25 at 7:27










    1




    1





    Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

    – vicgoyso
    Jan 25 at 7:27







    Thanks @Quentin, however my ultimate goal is to have nested accordions, hence activate 'accordion_1', then activate a nested accordion 'accordion_1_2', then activate nested accordion on 2nd level > 'accordion_1_2_1', and so on. This is the reason i requested by Id, hence a unique identifier.

    – vicgoyso
    Jan 25 at 7:27















    0














    Ok, so I tried another solution, StackBlitz HERE.



    This time I managed to use an ID for panel control.
    Here, I use a unique ID on each mat-expansion-panel (#first for the panel_1, #second for the panel_2, ...).



    <mat-accordion multi="true">
    <mat-expansion-panel #first></mat-expansion-panel>
    <mat-expansion-panel #second></mat-expansion-panel>
    <mat-expansion-panel #third></mat-expansion-panel>
    </mat-accordion>


    I also used buttons to control these panels without interacting with them.



    <button mat-raised-button (click)="changeState1()">ON/OFF Panel_1</button>
    <button mat-raised-button (click)="changeState2()">ON/OFF Panel_2</button>
    <button mat-raised-button (click)="changeState3()">ON/OFF Panel_3</button>


    In your TypeScript code, you use @ViewChild to retrieve the panel ID so you can control it.



    @ViewChild('first') first: MatExpansionPanel;
    @ViewChild('second') second: MatExpansionPanel;
    @ViewChild('third') third: MatExpansionPanel;


    Then, I created functions to open one to close the mat-expansion-panel according to its ID and according to its state.



    changeState1() {
    if(this.first.expanded){
    this.first.close();
    } else {
    this.first.open();
    }
    }

    changeState2() {...}


    PS: <mat-accordion multi="true"> allows multiple panels to open simultaneously.



    DEMO:



    enter image description here






    share|improve this answer




























      0














      Ok, so I tried another solution, StackBlitz HERE.



      This time I managed to use an ID for panel control.
      Here, I use a unique ID on each mat-expansion-panel (#first for the panel_1, #second for the panel_2, ...).



      <mat-accordion multi="true">
      <mat-expansion-panel #first></mat-expansion-panel>
      <mat-expansion-panel #second></mat-expansion-panel>
      <mat-expansion-panel #third></mat-expansion-panel>
      </mat-accordion>


      I also used buttons to control these panels without interacting with them.



      <button mat-raised-button (click)="changeState1()">ON/OFF Panel_1</button>
      <button mat-raised-button (click)="changeState2()">ON/OFF Panel_2</button>
      <button mat-raised-button (click)="changeState3()">ON/OFF Panel_3</button>


      In your TypeScript code, you use @ViewChild to retrieve the panel ID so you can control it.



      @ViewChild('first') first: MatExpansionPanel;
      @ViewChild('second') second: MatExpansionPanel;
      @ViewChild('third') third: MatExpansionPanel;


      Then, I created functions to open one to close the mat-expansion-panel according to its ID and according to its state.



      changeState1() {
      if(this.first.expanded){
      this.first.close();
      } else {
      this.first.open();
      }
      }

      changeState2() {...}


      PS: <mat-accordion multi="true"> allows multiple panels to open simultaneously.



      DEMO:



      enter image description here






      share|improve this answer


























        0












        0








        0







        Ok, so I tried another solution, StackBlitz HERE.



        This time I managed to use an ID for panel control.
        Here, I use a unique ID on each mat-expansion-panel (#first for the panel_1, #second for the panel_2, ...).



        <mat-accordion multi="true">
        <mat-expansion-panel #first></mat-expansion-panel>
        <mat-expansion-panel #second></mat-expansion-panel>
        <mat-expansion-panel #third></mat-expansion-panel>
        </mat-accordion>


        I also used buttons to control these panels without interacting with them.



        <button mat-raised-button (click)="changeState1()">ON/OFF Panel_1</button>
        <button mat-raised-button (click)="changeState2()">ON/OFF Panel_2</button>
        <button mat-raised-button (click)="changeState3()">ON/OFF Panel_3</button>


        In your TypeScript code, you use @ViewChild to retrieve the panel ID so you can control it.



        @ViewChild('first') first: MatExpansionPanel;
        @ViewChild('second') second: MatExpansionPanel;
        @ViewChild('third') third: MatExpansionPanel;


        Then, I created functions to open one to close the mat-expansion-panel according to its ID and according to its state.



        changeState1() {
        if(this.first.expanded){
        this.first.close();
        } else {
        this.first.open();
        }
        }

        changeState2() {...}


        PS: <mat-accordion multi="true"> allows multiple panels to open simultaneously.



        DEMO:



        enter image description here






        share|improve this answer













        Ok, so I tried another solution, StackBlitz HERE.



        This time I managed to use an ID for panel control.
        Here, I use a unique ID on each mat-expansion-panel (#first for the panel_1, #second for the panel_2, ...).



        <mat-accordion multi="true">
        <mat-expansion-panel #first></mat-expansion-panel>
        <mat-expansion-panel #second></mat-expansion-panel>
        <mat-expansion-panel #third></mat-expansion-panel>
        </mat-accordion>


        I also used buttons to control these panels without interacting with them.



        <button mat-raised-button (click)="changeState1()">ON/OFF Panel_1</button>
        <button mat-raised-button (click)="changeState2()">ON/OFF Panel_2</button>
        <button mat-raised-button (click)="changeState3()">ON/OFF Panel_3</button>


        In your TypeScript code, you use @ViewChild to retrieve the panel ID so you can control it.



        @ViewChild('first') first: MatExpansionPanel;
        @ViewChild('second') second: MatExpansionPanel;
        @ViewChild('third') third: MatExpansionPanel;


        Then, I created functions to open one to close the mat-expansion-panel according to its ID and according to its state.



        changeState1() {
        if(this.first.expanded){
        this.first.close();
        } else {
        this.first.open();
        }
        }

        changeState2() {...}


        PS: <mat-accordion multi="true"> allows multiple panels to open simultaneously.



        DEMO:



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 25 at 8:38









        QuentinQuentin

        335112




        335112






























            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%2f53992454%2fopen-close-panel-by-id-material-accordion%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'