2

I am trying to create a table. Each odd row has data and each even row is an expandable row. I need to dynamically load data to the expandable row when user press the expand icon.

I can't find the way to do it with ComponentResolver, local variable is useless inside a loop. is there another angular way except using the ElementRef workaround?

<template ngFor let-row="$implicit" [ngForOf]="rows" let-i="index">
        <tr class="row-rule-review ">
            <td class="col-applications">
            <img class="stack-img" src="images/expand.png" (click)="toggleButton(i)">

        </td>
             ...
        </tr>
        <tr class="row-extended">
            <td colspan="8">
                <div #target class="extended-content" id="extended-content-{{i}}" [style.height.px]="isExpand[i] ? 150 : 0">
                </div>
            </td>
        </tr>        
    </template>

the function on the component with :

toggleButton(index, cmp) {
        let that = this;
        if (this.isExpand[index]) {
            this.isExpand[index] = false;
        }
        else {
            if (this.extendComponent[index] === undefined) {
                this.resolver.resolveComponent(RowExpand).then((factory: ComponentFactory<any>) => {
                    that.extendComponent[index] = cmp.createComponent(factory);
                });
            }
            that.isExpand[index] = true;
        }
    }

in this senario, cmp.createComponent is undefined

Avi
  • 1,924
  • 3
  • 17
  • 31

2 Answers2

0

You can use the wrapper I explain in Angular 2 dynamic tabs with user-click chosen components like:

<template ngFor let-row="$implicit" [ngForOf]="rows" let-i="index">
    <tr class="row-rule-review ">
        <td class="col-applications">
        <img class="stack-img" src="images/expand.png" (click)="toggleButton(i)">

    </td>
         ...
    </tr>
    <tr class="row-extended">
        <td colspan="8">
            <div #target class="extended-content" id="extended-content-{{i}}" [style.height.px]="isExpand[i] ? 150 : 0">
              <dcl-wrapper type="expandedComponent" *ngIf="isExpand[i]></dcl-wrapper>
            </div>
        </td>
    </tr>        
</template>
Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
-1
     <tbody>
       <tr ng-repeat="dep in vm.department" >
         <td ng-if="!dep.show">
           <label class="checkbox checkbox-inline">
             <input type="checkbox" ng-model="dep.selected" ng-change="oneCheckboxSelected(dep)">
             <i class="input-helper"></i>
           </label>
         </td> 
         <td class="col-xs-2" ng-class="{'':dep.show == false , 'bg-white-row b-t-b-blue':dep.show == true}">
           <div class="col-lg-12  col-md-12 col-sm-12 col-xs-12 text-right" ng-class="{'':dep.show == false , 'bg-edecec-row-div':dep.show == true}">
             <a href="" ng-click="vm.getdepDetails(dep)"> 
               <i ng-if="!dep.show" class="glyphicon glyphicon-plus fa-3x font-BB"></i>
               <i ng-if="dep.show" class="glyphicon glyphicon-minus fa-3x blue-font"></i>
             </a>
           </div>
         </td>
       </tr>
     </tbody>

write onclick on the a tag vm.getdepDetails(dep) and in controller you can write the function and pass the params

null
  • 153
  • 1
  • 3
  • 16
Gayathri Mohan
  • 2,924
  • 4
  • 19
  • 25