0

I am using bootstrap accordion

  <div id="accordion" class="accordion">
    <div *ngFor="let mainRow of tableData; let i = index;" class="card red" id="mainRow-{{i}}">
      <div class="card-header table-header" id="auction-table-{{i}}">
        <table class="table-collapse-btn collapsed" [attr.data-toggle]="'collapse'" [attr.aria-expanded]="false"
          [attr.aria-controls]="'t-collapse-' + i" [attr.data-target]="'#t-collapse-'+i" id="proba-{{i}}" 
          (click)="renderSubTable(i, mainRow, $event, i)">
             ......
        </table>
      </div>
     
      </div>
    </div>
  </div>

there are a lot of accordions on my page. When i click big additional data gets displayed when the accordion is collapsed. The problem is that sometimes when the user is a lot of down on the page and clicks some of the uppper accordions on the window viewport then the accordion gets collapsed but the users still needs to scroll up to see the collapsed data.

To solve this issue i tried the following

 $('.collapse').on('shown.bs.collapse', function(e) {
      var $card = $(this).closest('.card');
      $('html,body').animate({
        scrollTop: ($card.offset().top)
      }, 500);
    });

but still - when i click on the accordion offset caulclation is not good - the scroll goes about 50 px down then the accordion position...

Is there any other way with which when i click on some accordion page will scroll directly to the position where the accordion starts ? The active one ?

i also tried

<div id="accordion" class="accordion">
    <div *ngFor="let mainRow of tableData; let i = index;" class="card" 
       id="mainRow-{{i}}">
         <div class="card-header table-header red" id="auction-table-{{i}}" 
         (click)="onClick(i)">
....

it gets scrolled now but not directly to the accordion it goes down again more then the auction position of the accordion

 onClick(index) {
    let el = document.getElementById(`auction-table-${index}`);
    el.scrollIntoView({behavior: 'smooth'});
  }
sdsd
  • 447
  • 3
  • 20
  • Yeah, no. Stop right were you are. Don't ever use Angular and jQuery together. No tutorial EVER mixes jQuery and Angular together, yet so many beginners do it. These are two completely different frameworks that act independently from each other and have no idea of each other's actions. Mixing them necessarily leads to a broken app, full of workarounds and hacks, not to mention that you have to load two libraries instead of one. – Jeremy Thille Apr 20 '21 at 14:15
  • Well how can i do this with angular ? – sdsd Apr 20 '21 at 14:16
  • I tried with pure JS also but didn't had some success – sdsd Apr 20 '21 at 14:17
  • https://stackoverflow.com/questions/43945548/scroll-to-element-on-click-in-angular-4 – Jeremy Thille Apr 20 '21 at 14:17
  • @JeremyThille thank you for your response. Btw if you know can you please post an answer - i don't know how i can i generate this `#target` dynamically and connect them through HTML in my COMPONENT. I have *ngFor instead one element with one hardcoded - `#target` reference... – sdsd Apr 20 '21 at 14:21
  • When i try it get's scrolled but a not directly to the accordion.It goes again more on the bottom then the accordion's postion.I will edit my question with the code, please take a look – sdsd Apr 20 '21 at 14:28
  • I think this post may help; it's scrolling to the last *ngFor element, but it's probably possible to adapt the code to scroll to any element : https://stackoverflow.com/questions/50208193/scroll-last-element-of-ngfor-loop-into-view – Jeremy Thille Apr 20 '21 at 14:31
  • 2
    @JeremyThille _"No tutorial EVER mixes jQuery and Angular together"_: https://www.smashingmagazine.com/2019/02/angular-application-bootstrap/ That's only the first result on Google. There are probably dozens or hundreds of these. Many projects contain both. AFAIK even the official documentation of Angular, Bootstrap or jQuery describes / described how to install Bootstrap with jQuery in Angular. (I'm not talking about ng-bootstrap). If you check the package.json of an average Angular project you'll see that it already loads 20-50 libraries. jQuery is just one of many. –  Apr 20 '21 at 15:01
  • Well then, no tutorial ever _should_ mix Angular and jQuery. This is just a heresy. Those who don't agree, well... just keep mixing Angular and jQuery, what can I say. – Jeremy Thille Apr 21 '21 at 07:16
  • @JeremyThille As long as you can't name one real reason to not use both together it's just your opinion. I've seen many projects where Angular and jQuery work very well together. If you know what you do there are no problems. –  Apr 22 '21 at 14:43
  • If you know what you do, you don't load and mix two antagonist libraries together. But as you said, it's just my opinion. If you absolutely want to keep doing it, be my guest. – Jeremy Thille Apr 23 '21 at 09:37

0 Answers0