【发布时间】:2020-07-14 12:03:43
【问题描述】:
我正在使用 Angular 应用程序,我正在开发 COVID 19 应用程序。我有 5 列的表格
-
- 状态
-
- 确认
-
- 活动中
-
- 已恢复
-
- 已故
这是我的堆栈闪电战链接stack blitz link。
这里也是我期待的参考Expectation。
单击任何列时,我会按升序或降序对整个表格数据进行排序,并显示向上箭头表示升序,向下箭头表示降序。
我的问题是当我单击任何列时,所有列上的所有箭头都会更改,因为我只想更改我单击的列的箭头并隐藏其他列的箭头。最初,当页面加载时,我的箭头不应该可见,而是只有当我点击任何列时它们才可见。
这是我的代码
component.html
<tr>
<th (click)="sortAscending(sortedDataBasedOnDate)" class="sticky state-heading">
<div class="heading-content"><abbr title="State">State/UT</abbr>
<div [ngClass]="showarrow ? 'down-arrow' : 'up-arrow'"></div>
</div>
</th>
<th (click)="sortByMaxCases(sortedDataBasedOnDate)" class="sticky">
<div class="heading-content"><abbr class="" title="Confirmed">Confirmed</abbr>
<div [ngClass]="showarrow ? 'down-arrow' : 'up-arrow'"></div>
</div>
</th>
<th (click)="sortByMaxActive(sortedDataBasedOnDate)" class="sticky">
<div class="heading-content"><abbr class="" title="Active">Active</abbr>
<div [ngClass]="showarrow ? 'down-arrow' : 'up-arrow'"></div>
</div>
</th>
<th (click)="sortByMaxRecovered(sortedDataBasedOnDate)" class="sticky">
<div class="heading-content"><abbr class="" title="Recovered">Recovered</abbr>
<div></div>
<div [ngClass]="showarrow ? 'down-arrow' : 'up-arrow'"></div>
</div>
</th>
<th (click)="sortByMaxDeath(sortedDataBasedOnDate)" class="sticky">
<div class="heading-content"><abbr class="" title="Deaths">Deceased</abbr>
<div [ngClass]="showarrow ? 'down-arrow' : 'up-arrow'"></div>
</div>
</th>
</tr>
Component.ts
showarrow=false
sortAscending(data) {
this.isAscendingSort = !this.isAscendingSort;
this.showarrow=true // setting here
data.forEach(item => item.statewise.sort(function (a, b) {
if (b.state < a.state) {
return -1;
}
if (b.state > a.state) {
return 1;
}
return 0;
}))
this.calculateDiff(this.sortedDataBasedOnDate)
if (!this.isAscendingSort) {
this.showarrow=!this.showarrow // for descending toggling class here
let a = data.forEach(item => item.statewise.sort(function (a, b) {
if (a.state < b.state) {
return -1;
}
if (a.state > b.state) {
return 1;
}
return 0;
}))
this.calculateDiff(this.sortedDataBasedOnDate)
}
}
我也尝试过这种方法,最初在页面加载时隐藏箭头,但它有同样的问题,如果我点击任何列箭头将出现在所有列上。
Component.html
<div class="heading-content"><abbr title="State">State/UT</abbr>
<div [ngClass]="{'down-arrow':showarrowdesc , 'up-arrow' :
showarrowasc,'hide':hide}"></div>
</div>
Component.ts
showarrowasc=false
showarrowdesc=false
hide=true
sortAscending(data) {
this.isAscendingSort = !this.isAscendingSort;
this.showarrowasc=!this.showarrowasc
this.showarrowdesc=false
data.forEach(item => item.statewise.sort(function (a, b) {
if (b.state < a.state) {
return -1;
}
if (b.state > a.state) {
return 1;
}
return 0;
}))
this.calculateDiff(this.sortedDataBasedOnDate)
if (!this.isAscendingSort) {
this.showarrowdesc=!this.showarrowdesc;
this.showarrowasc=false
let a = data.forEach(item => item.statewise.sort(function (a, b) {
if (a.state < b.state) {
return -1;
}
if (a.state > b.state) {
return 1;
}
return 0;
}))
this.calculateDiff(this.sortedDataBasedOnDate)
}
}
任何帮助都会很棒。
【问题讨论】:
-
你在这里做事的方式将不得不编写一些不必要的代码来实现这一点。我更喜欢创建一个带有字段、排序等的 json 对象并使用循环创建一个表,在这种情况下,您只需要排序函数和一个函数来获取正确的图标来更新表
-
你能给我一个关于任何测试数据的小演示吗?我是角度方面的初学者
-
您可以通过此链接了解它。 medium.com/@mjthakur413/….
-
说真的这是你的答案我没有学习如何制作表格。我想要的是在特定列的单击上切换箭头
-
是的,您创建表格的方式充满了不必要的代码。创建一个动态表,使用角度框架的力量或使用一些内置的数据表包。
标签: javascript html angular reactjs typescript