admin管理员组文章数量:1389797
I am trying to have sorting on table header. I am able to utilize this but I wanted to add arrows on click on header (asc/desc) or maybe a class to toggle which I can do it from css.
sort(property) {
this.isDesc = !this.isDesc; //change the direction
this.column = property;
let direction = this.isDesc ? 1 : -1;
this.records.sort(function (a, b) {
if (a[property] < b[property]) {
return -1 * direction;
}
else if (a[property] > b[property]) {
return 1 * direction;
}
else {
return 0;
}
});
};
Thanks for your help.
Happy New Year!
I am trying to have sorting on table header. I am able to utilize this https://stackblitz./edit/angular-sort-filter but I wanted to add arrows on click on header (asc/desc) or maybe a class to toggle which I can do it from css.
sort(property) {
this.isDesc = !this.isDesc; //change the direction
this.column = property;
let direction = this.isDesc ? 1 : -1;
this.records.sort(function (a, b) {
if (a[property] < b[property]) {
return -1 * direction;
}
else if (a[property] > b[property]) {
return 1 * direction;
}
else {
return 0;
}
});
};
Thanks for your help.
Happy New Year!
Share Improve this question asked Jan 5, 2021 at 0:12 Kunal VijanKunal Vijan 4572 gold badges10 silver badges34 bronze badges 2- 1 I'm confused what this code has to do with arrows. – evolutionxbox Commented Jan 5, 2021 at 0:13
- If you want arrows. You could use a service like fontawesome. They have a good selection of icons for free. And you could insert them right into your html. fontawesome./icons?d=gallery&q=arrow – Alanaj Commented Jan 5, 2021 at 0:35
2 Answers
Reset to default 4In css:
.pointer.active.desc:after {
content: "▲";
}
.pointer.active.asc:after {
content: "▼";
}
In template:
<div class="form-group">
<div class="col-md-6">
<input type="text" [(ngModel)]="searchText"
class="form-control" placeholder="Search By Category" />
</div>
</div>
<div class="col-md-12">
<table class="table table-responsive table-hover">
<tr>
<th [ngClass]="{pointer: true, active:column=='CategoryID',desc:isDesc, asc:!isDesc}"
(click)="sort('CategoryID')">
Category ID</th>
<th [ngClass]="{pointer: true, active:column=='CategoryName',desc:isDesc, asc:!isDesc}"
(click)="sort('CategoryName')">
Category</th>
<th [ngClass]="{pointer: true, active:column=='Description',desc:isDesc, asc:!isDesc}"
(click)="sort('Description')">
Description</th>
</tr>
<tr *ngFor="let item of records | category : searchText">
<td>{{item.CategoryID}}</td>
<td>{{item.CategoryName}}</td>
<td>{{item.Description}}</td>
</tr>
</table>
</div>
try
You can create a method to get the right symbol for your headers
In your class, create this method:
arrow(columnName) {
if (this.column === columnName) {
return this.isDesc ? '↑' : '↓';
}
return '';
}
In you template, change each header to be like:
<th class="pointer" (click)="sort('CategoryID')">
Category ID
<span [innerHTML]="arrow('CategoryID')"></span>
</th>
same on each header
本文标签: javascriptHow can I add arrow class on table sort headerStack Overflow
版权声明:本文标题:javascript - How can I add arrow class on table sort header? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744654841a2617903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论