Scroll to specific Expansion Panel in Material Accordion after Component loaded












0















We are trying to scroll to a specific <mat-expansion-panel> item within a <mat-accordion>. The problem is that ngAfterViewInit() is triggered before the accordion and its panels are fully loaded. This means the scrollIntoView() function is called while the accordions are being loaded and the page size afterwards change making our scroll operation take us to the wrong position of the page.



We also tried the other lifecycle hooks, which did not help, since they're all called to early. Does somebody have any good practice for this issue?



Our source is simple since we are trying to implement something very basic:



landingpage.component.html



<mat-accordion>
<mat-expansion-panel id="pangel-1">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>
<mat-expansion-panel id="panel-2">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>

[ ... ] // more panels

</mat-accordion>


landingpage.component.ts



ngAfterViewInit() {
this.scroll("panel-1");
}

scroll(id) {
console.log(`scrolling to ${id}`);
let el = document.getElementById(id);
el.scrollIntoView();
}









share|improve this question




















  • 1





    Please share some example code and template so that we can understand the structure of your app. A stackblitz example would be ideal.

    – G. Tranter
    Nov 13 '18 at 22:15











  • @G.Tranter Question was updated

    – Entertain
    Nov 15 '18 at 14:03
















0















We are trying to scroll to a specific <mat-expansion-panel> item within a <mat-accordion>. The problem is that ngAfterViewInit() is triggered before the accordion and its panels are fully loaded. This means the scrollIntoView() function is called while the accordions are being loaded and the page size afterwards change making our scroll operation take us to the wrong position of the page.



We also tried the other lifecycle hooks, which did not help, since they're all called to early. Does somebody have any good practice for this issue?



Our source is simple since we are trying to implement something very basic:



landingpage.component.html



<mat-accordion>
<mat-expansion-panel id="pangel-1">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>
<mat-expansion-panel id="panel-2">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>

[ ... ] // more panels

</mat-accordion>


landingpage.component.ts



ngAfterViewInit() {
this.scroll("panel-1");
}

scroll(id) {
console.log(`scrolling to ${id}`);
let el = document.getElementById(id);
el.scrollIntoView();
}









share|improve this question




















  • 1





    Please share some example code and template so that we can understand the structure of your app. A stackblitz example would be ideal.

    – G. Tranter
    Nov 13 '18 at 22:15











  • @G.Tranter Question was updated

    – Entertain
    Nov 15 '18 at 14:03














0












0








0








We are trying to scroll to a specific <mat-expansion-panel> item within a <mat-accordion>. The problem is that ngAfterViewInit() is triggered before the accordion and its panels are fully loaded. This means the scrollIntoView() function is called while the accordions are being loaded and the page size afterwards change making our scroll operation take us to the wrong position of the page.



We also tried the other lifecycle hooks, which did not help, since they're all called to early. Does somebody have any good practice for this issue?



Our source is simple since we are trying to implement something very basic:



landingpage.component.html



<mat-accordion>
<mat-expansion-panel id="pangel-1">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>
<mat-expansion-panel id="panel-2">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>

[ ... ] // more panels

</mat-accordion>


landingpage.component.ts



ngAfterViewInit() {
this.scroll("panel-1");
}

scroll(id) {
console.log(`scrolling to ${id}`);
let el = document.getElementById(id);
el.scrollIntoView();
}









share|improve this question
















We are trying to scroll to a specific <mat-expansion-panel> item within a <mat-accordion>. The problem is that ngAfterViewInit() is triggered before the accordion and its panels are fully loaded. This means the scrollIntoView() function is called while the accordions are being loaded and the page size afterwards change making our scroll operation take us to the wrong position of the page.



We also tried the other lifecycle hooks, which did not help, since they're all called to early. Does somebody have any good practice for this issue?



Our source is simple since we are trying to implement something very basic:



landingpage.component.html



<mat-accordion>
<mat-expansion-panel id="pangel-1">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>
<mat-expansion-panel id="panel-2">
<mat-expansion-panel-header>
<mat-panel-title>Lorem ipsum</mat-panel-title>
</mat-expansion-panel-header>
<p>
Lorem ipsum dolor sit amet,
consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt...
</p>
</mat-expansion-panel>

[ ... ] // more panels

</mat-accordion>


landingpage.component.ts



ngAfterViewInit() {
this.scroll("panel-1");
}

scroll(id) {
console.log(`scrolling to ${id}`);
let el = document.getElementById(id);
el.scrollIntoView();
}






angular typescript angular-material






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:02







Entertain

















asked Nov 13 '18 at 11:01









EntertainEntertain

198523




198523








  • 1





    Please share some example code and template so that we can understand the structure of your app. A stackblitz example would be ideal.

    – G. Tranter
    Nov 13 '18 at 22:15











  • @G.Tranter Question was updated

    – Entertain
    Nov 15 '18 at 14:03














  • 1





    Please share some example code and template so that we can understand the structure of your app. A stackblitz example would be ideal.

    – G. Tranter
    Nov 13 '18 at 22:15











  • @G.Tranter Question was updated

    – Entertain
    Nov 15 '18 at 14:03








1




1





Please share some example code and template so that we can understand the structure of your app. A stackblitz example would be ideal.

– G. Tranter
Nov 13 '18 at 22:15





Please share some example code and template so that we can understand the structure of your app. A stackblitz example would be ideal.

– G. Tranter
Nov 13 '18 at 22:15













@G.Tranter Question was updated

– Entertain
Nov 15 '18 at 14:03





@G.Tranter Question was updated

– Entertain
Nov 15 '18 at 14:03












2 Answers
2






active

oldest

votes


















1














Here is an example on StackBlitz with Angular Material 7. Your technique works fine, but you need to be careful about a couple of things - make sure that the page is long enough so that the panel can be positioned at the top of the page, and make sure you don't misspell the panel's id.






share|improve this answer
























  • Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

    – Entertain
    Nov 21 '18 at 17:31





















1





+50









In my example i click something and it opens a side nav that takes up half the screen, so on the click function i have this logic:



if ($event) {
setTimeout(() => $event.target.scrollIntoView({behavior: 'smooth', block: 'end'}), 1);
}


So in your example you could tie to the afterExpand event (on mat-expansion-panel) and run the logic.






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%2f53279563%2fscroll-to-specific-expansion-panel-in-material-accordion-after-component-loaded%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














    Here is an example on StackBlitz with Angular Material 7. Your technique works fine, but you need to be careful about a couple of things - make sure that the page is long enough so that the panel can be positioned at the top of the page, and make sure you don't misspell the panel's id.






    share|improve this answer
























    • Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

      – Entertain
      Nov 21 '18 at 17:31


















    1














    Here is an example on StackBlitz with Angular Material 7. Your technique works fine, but you need to be careful about a couple of things - make sure that the page is long enough so that the panel can be positioned at the top of the page, and make sure you don't misspell the panel's id.






    share|improve this answer
























    • Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

      – Entertain
      Nov 21 '18 at 17:31
















    1












    1








    1







    Here is an example on StackBlitz with Angular Material 7. Your technique works fine, but you need to be careful about a couple of things - make sure that the page is long enough so that the panel can be positioned at the top of the page, and make sure you don't misspell the panel's id.






    share|improve this answer













    Here is an example on StackBlitz with Angular Material 7. Your technique works fine, but you need to be careful about a couple of things - make sure that the page is long enough so that the panel can be positioned at the top of the page, and make sure you don't misspell the panel's id.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 16:37









    G. TranterG. Tranter

    4,4311423




    4,4311423













    • Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

      – Entertain
      Nov 21 '18 at 17:31





















    • Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

      – Entertain
      Nov 21 '18 at 17:31



















    Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

    – Entertain
    Nov 21 '18 at 17:31







    Unfortunatly I misclicked and assigned the bounty to the wrong answer, im sorry - it was quite a long work day. This is the correct answer though for providing evidence that the given snippets are actually working. Our component is quite large so it seems that the problem lays somewhere else and I will have to double check.

    – Entertain
    Nov 21 '18 at 17:31















    1





    +50









    In my example i click something and it opens a side nav that takes up half the screen, so on the click function i have this logic:



    if ($event) {
    setTimeout(() => $event.target.scrollIntoView({behavior: 'smooth', block: 'end'}), 1);
    }


    So in your example you could tie to the afterExpand event (on mat-expansion-panel) and run the logic.






    share|improve this answer




























      1





      +50









      In my example i click something and it opens a side nav that takes up half the screen, so on the click function i have this logic:



      if ($event) {
      setTimeout(() => $event.target.scrollIntoView({behavior: 'smooth', block: 'end'}), 1);
      }


      So in your example you could tie to the afterExpand event (on mat-expansion-panel) and run the logic.






      share|improve this answer


























        1





        +50







        1





        +50



        1




        +50





        In my example i click something and it opens a side nav that takes up half the screen, so on the click function i have this logic:



        if ($event) {
        setTimeout(() => $event.target.scrollIntoView({behavior: 'smooth', block: 'end'}), 1);
        }


        So in your example you could tie to the afterExpand event (on mat-expansion-panel) and run the logic.






        share|improve this answer













        In my example i click something and it opens a side nav that takes up half the screen, so on the click function i have this logic:



        if ($event) {
        setTimeout(() => $event.target.scrollIntoView({behavior: 'smooth', block: 'end'}), 1);
        }


        So in your example you could tie to the afterExpand event (on mat-expansion-panel) and run the logic.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 14:15









        Tim.BurnellTim.Burnell

        1515




        1515






























            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%2f53279563%2fscroll-to-specific-expansion-panel-in-material-accordion-after-component-loaded%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

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation