【问题标题】:Use FontAwesome icons in table header in Angular在 Angular 的表头中使用 FontAwesome 图标
【发布时间】:2021-09-23 10:10:32
【问题描述】:

我正在尝试将sort、sortUp 和sortDown 图标设置为我的表格标题,遵循table tutorial from ng-bootstrap。

有一个NgbdSortableHeader 指令可以改变列的排序:

@Directive({
  selector: 'th[sortable]',
  host: {
    '[class.asc]': 'direction === "asc"',
    '[class.desc]': 'direction === "desc"',
    '(click)': 'rotate()'
  }
})
export class NgbdSortableHeader {
  @Input() sortable: string = '';
  @Input() direction: SortDirection = '';
  @Output() sort = new EventEmitter<SortEvent>();

  rotate() {
    this.direction = rotate[this.direction];
    this.sort.emit({column: this.sortable, direction: this.direction});
  }
}

以及拥有表格的组件:

@Component({
  selector: 'app-users',
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.scss'],
  providers: [UserService]
})
export class UsersComponent implements OnInit {
  faSort = faSort;
  faSortUp = faSortUp;
  faSortDown = faSortDown;

  users$: Observable<User[]>;
  total$: Observable<number>;

  @ViewChildren(NgbdSortableHeader) headers!: QueryList<NgbdSortableHeader>;

  constructor() {
  }

  ngOnInit(): void {
  }

  onSort({column, direction}: SortEvent) {
    // resetting other headers
    this.headers.forEach(header => {
      if (header.sortable !== column) {
        header.direction = '';
      }
    });


    this.service.sortColumn = column;
    this.service.sortDirection = direction;
  }
}

我想知道是否有办法访问当前列排序顺序并替换项目中的图标或隐藏它们。

<th scope="col" sortable="firstName" (sort)="onSort($event)">
  Name
  <fa-icon [icon]="faSort"></fa-icon>
  <fa-icon [icon]="faSortUp"></fa-icon>
  <fa-icon [icon]="faSortDown"></fa-icon>
</th>

【问题讨论】:

  • 您的UsersComponent sn-p 中的onSort 处理程序已经在接收有关名称(即column)和最后交互的列的排序方向的信息。如果您将这些暴露给您的模板,那么您可以简单地使用一系列*ngIf 指令来控制每个fa-icon 的可见性(例如&lt;fa-icon [icon]="faSortUp" *ngif="sortColumn === 'firstName' &amp;&amp; sortDirection ==='asc'"&gt;&lt;/fa-icon&gt;)。但是,这会变得冗长,因此请考虑将这些图标封装在一个聚合组件中。
  • 我最初的想法(发布此问题后)是从指令中获取视图子项并更改其中的 fa-icon 元素的图标。这样我就不需要更改每张表,只需一个 fa-icon 元素。
  • @miqh 我能够从指令中更改图标。

标签: html css angular angular-fontawesome


【解决方案1】:

我能够通过在排序指令中使用@ChildComponent 来解决我的问题:

<th scope="col" sortable="firstName" (sort)="onSort($event)">
  Name
  <fa-icon [icon]="faSort" size="lg"></fa-icon>
</th>
import { Directive, EventEmitter, Input, Output, ViewChild, ContentChild, ElementRef } from "@angular/core";
import { FaIconComponent } from "@fortawesome/angular-fontawesome";
import { faSort, faSortUp, faSortDown } from "@fortawesome/pro-regular-svg-icons";

export type SortDirection = 'asc' | 'desc' | '';
const rotate: {[key: string]: SortDirection} = { 'asc': 'desc', 'desc': '', '': 'asc' };

export interface SortEvent {
  column: string;
  direction: SortDirection;
}

@Directive({
  selector: 'th[sortable]',
  host: {
    '[class.asc]': 'direction === "asc"',
    '[class.desc]': 'direction === "desc"',
    '(click)': 'rotate()'
  }
})
export class NgbdSortableHeader {
  @Input() sortable: string = '';
  @Input() direction: SortDirection = '';
  @Output() sort = new EventEmitter<SortEvent>();

  @ContentChild(FaIconComponent) sortIcon?: FaIconComponent;

  rotate() {
    this.direction = rotate[this.direction];
    this.sort.emit({column: this.sortable, direction: this.direction});

    if (this.sortIcon !== undefined)
    {
        this.sortIcon.icon = this.direction === 'asc' ? faSortDown : this.direction === 'desc' ? faSortUp : faSort;
        this.sortIcon.render();
    }
  }
}

【讨论】:

  • 另一种方法可能是拥有sortIcon = faSort; 属性,更改方法this.sortIcon = this.direction === 'asc' ? faSortDown ... 中的值并将其绑定到模板&lt;fa-icon [icon]="sortIcon" size="lg"&gt;&lt;/fa-icon&gt;,但您的解决方案可以正常工作。
  • @YaroslavAdmin 有趣,谢谢。我读到我必须在图标更改时致电render(),所以我不确定它会如何发挥作用。
  • 是的,您需要在以编程方式更改图标时调用render(),但是当您使用模板绑定时 - 您不需要。
  • 啊,很好。顺便说一句,因为我有多个列,所以我需要有多个 sortIcon,每列一个,它会变得冗长。
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 2019-03-14
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多