【问题标题】:Add class dynamically to td on click and toggle and remove when other td is clicked in angular/typescript单击时将类动态添加到 td 并在 angular/typescript 中单击其他 td 时切换和删除
【发布时间】:2020-07-14 12:03:43
【问题描述】:

我正在使用 Angular 应用程序,我正在开发 COVID 19 应用程序。我有 5 列的表格

    1. 状态
    1. 确认
    1. 活动中
    1. 已恢复
    1. 已故

这是我的堆栈闪电战链接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


【解决方案1】:

我猜您正在剪切您提供的代码,因为我看不到您从模板调用的 sortByMaxCases()(以及除 sortAscending() 之外的其他代码)。

然后,从我在这里看到的情况来看,您似乎正在使用相同的布尔值切换所有列:showarrow。

为了克服这个问题,您可以创建一个包含多个属性的对象,每列一个,如下所示:

const showArrows = {
    confirmed: false,
    active: false
}

添加您拥有的所有其他列,并将其中一列定义为 true 作为初始排序。

然后在您的模板中引用该对象,如下所示:

<th (click)="sortAscending(sortedDataBasedOnDate)" class="sticky state-heading">
    <div class="heading-content"><abbr title="State">Confirmed</abbr>
        <div [ngClass]="showArray.confirmed ? 'down-arrow' : 'up-arrow'"></div>
    </div>
</th>

作为已确认列的示例。

希望对你有帮助。

【讨论】:

  • 那么我应该为我的 5 个列添加显示数组,其中包含 10 个属性为每个列显示隐藏吗? @gerstams
  • 我不太明白你的问题。我建议创建一个具有 n 个属性的对象(不是数组)(n 是您拥有的列数)。然后在模板中使用这些属性。
  • 这也不会是有效的方式,因为我有这么多的桌子和所有那些我不能做同样的事情!!! @gerstams
  • 如果工作量太大而您不想自己做,可以查看 Angular 材料排序标题:material.angular.io/components/sort/overview。这将大部分代码抽象出来,您基本上只需要提供一次列、列标题和数据
  • 好吧,这很有趣
猜你喜欢
  • 2013-03-18
  • 2018-03-17
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多