javascript - Angular 2 pipe change on button click for table rows - Stack Overflow

Is it possible to changeput pipe to format a specific column in Angular 2 on button click? Like for ex

Is it possible to change/put pipe to format a specific column in Angular 2 on button click? Like for example I have a table with 'Name' and 'Hours' columns, when I click a button the data displayed for 'Hours' should be converted into days and vice-versa.

<button class="btn btn-secondary" (click)="convertToDays()">Days</button>
<button class="btn btn-secondary" (click)="convertToHours()">Hours</button>

<tbody>
<td>{{availableLeave.hours}}</td>
</tbody>

convertToDays(){
//put pipe to format availableLeave.hours to display as days ex.('48' hours should be displayed as '2' days)
}

convertToHours(){
//will just revert the original formatting of availableLeave.hours which is in hour format
}

Is it possible to change/put pipe to format a specific column in Angular 2 on button click? Like for example I have a table with 'Name' and 'Hours' columns, when I click a button the data displayed for 'Hours' should be converted into days and vice-versa.

<button class="btn btn-secondary" (click)="convertToDays()">Days</button>
<button class="btn btn-secondary" (click)="convertToHours()">Hours</button>

<tbody>
<td>{{availableLeave.hours}}</td>
</tbody>

convertToDays(){
//put pipe to format availableLeave.hours to display as days ex.('48' hours should be displayed as '2' days)
}

convertToHours(){
//will just revert the original formatting of availableLeave.hours which is in hour format
}
Share Improve this question edited Feb 13, 2017 at 10:02 xird asked Feb 13, 2017 at 3:47 xirdxird 7851 gold badge8 silver badges16 bronze badges 2
  • 1 Please post the code that demonstrates what you try to acplish. Yes, it's possible, but hard to tell which of the 200 ways would fit your need. – Günter Zöchbauer Commented Feb 13, 2017 at 7:02
  • hi Gunter, thanks for checking out, I added some pseudo of what I want to achieve, but seems like someone posted an answer that could help, I'll check which way fits the most. – xird Commented Feb 13, 2017 at 10:06
Add a ment  | 

2 Answers 2

Reset to default 5

You can pass a parameter to the pipe like

{{someValue | myPipe:param}}

<button class="btn btn-secondary" (click)="param = 'd'">Days</button>
<button class="btn btn-secondary" (click)="param = 'h'">Hours</button>
class MyPipe implements PipeTransform {
  transform(value, param) {
    if(param === 'h') {
      return "...";
    } else {
      return "...";  
    }
  }
}

As per your requirement, you can use Pipe directly in your ponent file. You can make two pipes one for Hours and one for Days then call the transform function of required pipe.

for more details on how to use pipes in ponent or in service, check this post.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745314144a4622136.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信