我正在尝试按照ng-bootstrap 的表格教程sort
将、sortUp
和sortDown
图标设置为我的表格标题。
有一个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>