简体   繁体   English

使用PrimeNG以角度进行$ event绑定

[英]$event binding in angular using PrimeNG

According to the documentation of PrimeNG, they have 2 properties for row expansions in a table. 根据PrimeNG的文档,它们在表中有2个行扩展属性。 onRowExpand and onRowCollapse.I want to change the background color of the current row I have clicked on to expand. onRowExpand和onRowCollapse.I想要更改我单击的当前行的背景颜色以进行展开。

My html: 我的HTML:

<p-table [columns]="cols" [value]="cars" dataKey="vin" expandableRows="true" styleClass="x-expandable-row"
(onRowExpand)="onRowExpandCallback($event)" (onRowCollapse)="onRowCollapseCallback($event)">
   <ng-template pTemplate="header">
      <tr>
          <th style="width: 2.25em"></th>
          <th>Make</th>
          <th>Model</th>
          <th>Color</th>
      </tr>
    </ng-template>
<ng-template pTemplate="body" let-car let-expanded="expanded">
   <tr>
       <td class="x-expand-handeler-row" >
          <a href="#" [pRowToggler]="car">
             <i [ngClass]="expanded ? 'fas fa-minus' : 'fas fa-plus'"></i>
          </a>                                    
       </td>
       <td>{{car.make}}</td>
       <td>{{car.model}}</td>
       <td>{{car.color}}</td>
    </tr>
 </ng-template>
 <ng-template pTemplate="rowexpansion" let-car>
    <td [attr.colspan]="4" class="x-expanded-row">                                                                                          
        <label>Row Details Go Here</label>
    </td>                                                           
 </ng-template>
</p-table>

My ts file: 我的ts档案:

 onRowExpandCallback(e) {
    //need to find the best way to add and remove a class on expanded row
    e.originalEvent.currentTarget.parentElement.parentElement.parentElement.parentElement.bgColor = "#edf6fa";
  }
  onRowCollapseCallback(e) {
    //need to find the best way to add and remove a class on expanded row
    e.originalEvent.currentTarget.parentElement.parentElement.bgColor = '#fff';
  }

I have been playing with the number of "parentElements" I add, but I didn't have luck. 我一直在玩我添加的“parentElements”的数量,但我没有运气。 I have the code working in the PrimeNG data-table using only 2 parentElements. 我只使用2个parentElements在PrimeNG数据表中使用代码。

If you notice in your code you have 'expanded' local variable using that you can easily toggle class like how you toggle icons using: 如果您在代码中注意到您已使用“扩展”局部变量,则可以轻松切换类,就像您使用以下方式切换图标一样:

 <i [ngClass]="expanded ? 'fas fa-minus' : 'fas fa-plus'"></i> 

You no need to handle onRowExpand and onRowCollapse events 您无需处理onRowExpand和onRowCollapse事件

add [ngClass]="expanded ? 'edf6faClass' : 'fffClass'" on tr tag 在tr标签上添加[ngClass] =“expanded?'edf6faClass':'fffClass'”

 <p-table [columns]="cols" [value]="cars" dataKey="vin" expandableRows="true" styleClass="x-expandable-row" (onRowExpand)="onRowExpandCallback($event)" (onRowCollapse)="onRowCollapseCallback($event)"> <ng-template pTemplate="header"> <tr> <th style="width: 2.25em"></th> <th>Make</th> <th>Model</th> <th>Color</th> </tr> </ng-template> <ng-template pTemplate="body" let-car let-expanded="expanded"> <tr [ngClass]="expanded ? 'edf6faClass' : 'fffClass'"> <td class="x-expand-handeler-row"> <a href="#" [pRowToggler]="car"> <i [ngClass]="expanded ? 'fas fa-minus' : 'fas fa-plus'"></i> </a> </td> <td>{{car.make}}</td> <td>{{car.model}}</td> <td>{{car.color}}</td> </tr> </ng-template> <ng-template pTemplate="rowexpansion" let-car> <td [attr.colspan]="4" class="x-expanded-row"> <label>Row Details Go Here</label> </td> </ng-template> </p-table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM