Search code examples
angularbootstrap-4angular6bootstrap-accordion

Applying a background class to parent div in angular 6


I am building an accordion with bootstrap 4 using cards. In side each card i have some icons like edit, view, detail etc. When these icons are clicked, a function in the component gets called which then navigate the user to the child route.

The child router outlet displays to the right side of this accordion.

I want to change the card-header background when any of the icons under that card is clicked. How can i do this?

Here is the html

  <div id="accordion" role="tablist" aria-multiselectable="true">

    <!--Kitchen-->
    <div class="card mwk-project-left" *ngFor="let psl of projectSubList; let i = index">
          <div class="card-header mwk-project-details-hdr" role="tab" id="heading{{psl.Id}}">
        <h5 class="mb-0">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapse{{psl.Id}}" aria-expanded="true" [attr.aria-controls]="'collapse'+psl.Id">
            {{psl.Description}}
          </a>
        </h5>
      </div>

      <div id="collapse{{psl.Id}}" class="collapse show" role="tabpanel" [attr.aria-labelledby]="'heading'+psl.Id">
        <div class="card-body">

          <ul *ngIf="psl.Features">
            <li *ngFor="let ftr of psl.Features; let fi = index">
              {{ftr.FeatureGroup}}: {{ftr.Description}}
            </li>
          </ul>

          <h4 class="text-danger" *ngIf="!psl.Features">No features found!</h4>

          <div class="mwk-actions-sub">
            <button mat-icon-button color="accent" type="button" (click)="onView(psl.Id)" title="View {{psl.Description}}" class="matIconButton"><mat-icon>ballot</mat-icon></button>
            <button mat-icon-button color="primary" type="button" (click)="onDetail(psl.Id)" title="Detail {{psl.Description}}" class="matIconButton"><mat-icon>view_list</mat-icon></button>
            <button mat-icon-button color="primary" type="button" (click)="onEdit(psl.Id)" title="Edit {{psl.Description}}" class="matIconButton"><mat-icon>edit</mat-icon></button>
            <button mat-icon-button color="primary" type="button" (click)="onConfig(psl.Id)" title="Config {{psl.Description}}" class="matIconButton"><mat-icon>settings_applications</mat-icon></button>
            <button mat-icon-button color="warn" type="button" (click)="onDelete(psl.Id, psl.Description)" title="Delete {{psl.Description}}" class="matIconButton"><mat-icon>clear</mat-icon></button>
          </div>
        </div>
      </div>
     </div>    
  </div>

How can i do this?

Looked at the div example from this page, but i don't have the a tags.

https://angular.io/api/router/RouterLinkActive

<div routerLinkActive="active-link" [routerLinkActiveOptions]="{exact: true}">
  <a routerLink="/user/jim">Jim</a>
  <a routerLink="/user/bob">Bob</a>
</div>

Update 1

I have made the following two changes and now my background is getting applied to the card-header section.

<div class="card mwk-project-left" *ngFor="let psl of projectSubList; let i = index" routerLinkActive="mwk-project-left-active" [routerLinkActiveOptions]="{exact: true}">

and then

<a mat-icon-button color="accent" [routerLink]="[psl.Id]" title="View {{psl.Description}}" class="matIconButton"><mat-icon>ballot</mat-icon></a>
            <a mat-icon-button color="primary" [routerLink]="[psl.Id, 'detail-list']" title="Detail {{psl.Description}}" class="matIconButton"><mat-icon>view_list</mat-icon></a>
            <a mat-icon-button color="primary" [routerLink]="[psl.Id, 'edit']" title="Edit {{psl.Description}}" class="matIconButton"><mat-icon>edit</mat-icon></a>
            <a mat-icon-button color="primary" [routerLink]="[psl.Id, 'config-edit']" title="Config {{psl.Description}}" class="matIconButton"><mat-icon>settings_applications</mat-icon></a>
            <a mat-icon-button color="warn" [routerLink]="[psl.Id, 'edit']" [queryParams]="{delete: 'yes'}" title="Delete {{psl.Description}}" class="matIconButton"><mat-icon>clear</mat-icon></a>

Above update 1 helped with when clicking outside the router outlet section.

When i click a link to go to another child page from inside router outlet, my update 1 class gets removed at this time. So i loose mt background. How to fix this now?

Update 2

exact: false fixed it when going to different child page from inside the router outlet.

<div class="card mwk-project-left" *ngFor="let psl of projectSubList; let i = index" routerLinkActive="mwk-project-left-active" [routerLinkActiveOptions]="{exact: false}">

I am good, thanks for looking!


Solution

  • This post helped https://angular.io/api/router/RouterLinkActive

    Then applied the routerLinkActive to the card div with exact false since one of the child page has links that take the user to some other pages.

    <div class="card mwk-project-left" *ngFor="let psl of projectSubList; 
    let i = index" routerLinkActive="mwk-project-left-active" 
    [routerLinkActiveOptions]="{exact: false}">
    

    and finally, I had to change the buttons to anchor tags

    from

    <button mat-icon-button color="accent" type="button" (click)="onView(psl.Id)" 
    title="View {{psl.Description}}" class="matIconButton"><mat-icon>ballot</mat-icon></button>
    

    to

    <a mat-icon-button color="accent" [routerLink]="[psl.Id]" title="View {{psl.Description}}" 
    class="matIconButton"><mat-icon>ballot</mat-icon></a>