[英]Hide and Show Multiple columns in html table with button click in Angular8
I have to toggle show and hide multiple columns in a html table.我必须在 html 表中切换显示和隐藏多个列。 Currently i can show and hide column2.目前我可以显示和隐藏 column2。 BUt i want to hide and show multiple columns say column 3 and 4 and 6 with the click and also toggle the button name between show and hide.但是我想通过单击隐藏和显示多列,例如第 3 列、第 4 列和第 6 列,并在显示和隐藏之间切换按钮名称。 or IS there any easy way to handle this in angular 8?或者在 angular 8 中是否有任何简单的方法来处理这个问题?
Below is my table下面是我的表
html html
<button onclick='document.getElementById("foo").classList.toggle("hide2")'>Show/hide</button>
<table id="foo" class="table table-sm fs-13px mt-2 mb-5">
<tr>
<th class="text-danger fw7 pl-0" colspan="6">
TABLE 1
</th>
</tr>
<tr>
<th class="text-nowrap pr-3 pl-0 sticky-header">Year
</th>
<th class="text-nowrap pr-3 sticky-header">Platform
</th>
<th class="text-nowrap pr-3 sticky-header">Name
</th>
<th class="text-nowrap pr-3 sticky-header">Variants
</th>
<th class="text-nowrap pr-3 sticky-header">Program
</th>
<th class="w-100 sticky-header"></th>
</tr>
<tr *ngFor="let u of apiData.unassigned">
<td class="text-nowrap pr-3 pl-0">
{{ u.year }}
</td>
<td class="text-nowrap text-danger pr-3">
Not Assigned
</td>
<td class="text-nowrap pr-3">
{{ u.modelName }}
</td>
<td class="text-nowrap pr-3">
{{ u.unassignedVariantCount }}
</td>
<td class="text-nowrap pr-3">
{{ u.programCode }}
</td>
<td></td>
</tr>
<tr>
<td colspan="6">
</td>
</tr>
</table>
css code css代码
#foo.hide2 tr > *:nth-child(2) {
display: none;
}
Found a solution in Angular with boolean variable and scss class turning it on and off like below:在 Angular 中找到了一个解决方案,其中 boolean 变量和 scss class 打开和关闭它,如下所示:
.Html file .Html 文件
<button class="mr-2 mb-1" [class.rs-inactive]="!showPlatformDetail" [class.rs-active]="showPlatformDetail" (click)="showPlatformDetail=!showPlatformDetail"> {{showPlatformDetail ? 'Show Detail' : 'Hide Detail'}} </button>
<table id="platforms" class="table table-sm fs-13px mt-2 mb-5" [class.platform-detail]="showPlatformDetail">
<tr>
<th class="text-danger fw7 pl-0" colspan="6">
TABLE 1
</th>
</tr>
<tr>
<th class="text-nowrap pr-3 pl-0 sticky-header">Year
</th>
<th class="text-nowrap pr-3 sticky-header">Platform
</th>
<th class="text-nowrap pr-3 sticky-header">Name
</th>
<th class="text-nowrap pr-3 sticky-header">Variants
</th>
<th class="text-nowrap pr-3 sticky-header">Program
</th>
<th class="w-100 sticky-header"></th>
</tr>
<tr *ngFor="let u of apiData.unassigned">
<td class="text-nowrap pr-3 pl-0">
{{ u.year }}
</td>
<td class="text-nowrap text-danger pr-3">
Not Assigned
</td>
<td class="text-nowrap pr-3">
{{ u.modelName }}
</td>
<td class="text-nowrap pr-3">
{{ u.unassignedVariantCount }}
</td>
<td class="text-nowrap pr-3">
{{ u.programCode }}
</td>
<td></td>
</tr>
<tr>
<td colspan="6">
</td>
</tr>
</table>
.scss file .scss 文件
.platform-detail > tr > *:nth-child(7),
.platform-detail > tr > *:nth-child(8),
.platform-detail > tr > *:nth-child(10),
.platform-detail > tr > *:nth-child(11),
.platform-detail > tr > *:nth-child(13),
.platform-detail > tr > *:nth-child(15) {
display: none;
}
.ts file .ts 文件
showPlatformDetail = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.