Artur
Artur

Reputation: 93

How to change angular material sort icon

I need to change default arrow icon from angular material matSort to a custom arrow.

The current code

 <mat-table #table [dataSource]="source" matSort (matSortChange)="sortData($event)" [matSortActive]="sort.active" [matSortDirection]="sort.direction">

Is there any way to do this ?

Upvotes: 9

Views: 29145

Answers (6)

Katherine
Katherine

Reputation: 128

Similar to @Amuthan 's answer which hides the default display. This code must reside in the global css. It displays carets instead of arrows:

/* in global css document: */
/* this makes it so that sort header arrow is always shown: */
.mat-sort-header-arrow.ng-trigger.ng-trigger-arrowPosition {
    opacity: 1 !important;
    color: blue;
    
  }
/* Hide the default sort arrow display: */
.mat-sort-header-stem {
    display: none  !important;
}
.mat-sort-header-pointer-left, .mat-sort-header-pointer-right {
    display: none  !important;
}
.mat-sort-header-pointer-middle{
    width: 0px !important;
    height: 0px !important;
}
/* show two carets on top of each other when not sorted takes place: */
    .mat-sort-header-arrow {
         .mat-sort-header-indicator {
           &::before {
             content: "<>"; 
             position: absolute;
             opacity: 1 !important;
             color:  blue !important;
             font-size: 1.2em;
             font-weight: bold;
             transform: translate(-10%, 20%)   rotate(90deg) !important;
           }
         }
       }

/* show ascending caret when sorted ascending:*/
[aria-sort="ascending"] {
 .mat-sort-header-arrow {
      .mat-sort-header-indicator {
        &::before {
          content: "<"; 
          position: absolute;
          color:  blue !important;
          opacity: 1 !important;
          font-size: 1.2em;
          font-weight: bold;
          transform: translate(0,0) rotate(90deg) !important;
        }
      }
    }
  }
  
 /* show descending caret when sorted descending: */
  [aria-sort="descending"] {
.mat-sort-header-arrow {
      .mat-sort-header-indicator {
        &::before {
          content: ">";
          position: absolute;
          color:  blue !important;
          opacity: 1 !important;
          font-size: 1.2em;
          font-weight: bold;
          transform: translate(0,-10%) rotate(90deg) !important;
        }
      }
    }
  }
  

This is the result: Table header with carets for sorting

Upvotes: 1

user11985311
user11985311

Reputation: 21

that is the way that I made it

<mat-header-cell *matHeaderCellDef (dblclick)="sortData(column.field)"> {{ column.title }}<mat-icon *ngIf="sort.active==column.field" style="font-size:11pt;color:dimgray;font-weight: 700;padding-left:5px">{{sort.direction=="asc"?"arrow_upward":"arrow_downward"}}</mat-icon></mat-header-cell>

<mat-icon *ngIf="sort.active==column.field" style="font-size:11pt;color:dimgray;font-weight: 700;padding-left:5px">{{sort.direction=="asc"?"arrow_upward":"arrow_downward"}}</mat-icon>**

Upvotes: 2

SkarXa
SkarXa

Reputation: 1194

Based on @amuthan solution I came up with this, it's the same arrow as dropdown uses, and it also shows up when hovering:

.mat-sort-header-arrow[style] {
  .mat-sort-header-stem {
    display: none;
  }
  .mat-sort-header-indicator {
    opacity: 1;
    color: black;
    font-weight: bold;

    .mat-sort-header-pointer-left, .mat-sort-header-pointer-right, .mat-sort-header-pointer-middle  {
      display: none;
    }
  }
}
.mat-sort-header-indicator {
  &::before {
    content: "";
    top: 0.1em;
    position: absolute;
    color: $primary;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid;
    margin: 0 4px;
    transform: rotate(180deg);
    opacity: 0.6;
  }
}


[aria-sort="ascending"] {
  .mat-sort-header-arrow {
    .mat-sort-header-indicator {
      &::before {
        top: 0.1em;
        transform: rotate(180deg);
        opacity: 1;

      }
    }
  }
}

[aria-sort="descending"] {
  .mat-sort-header-arrow {
    .mat-sort-header-indicator {
      &::before {
        top: -0.6em;
        transform: rotate(0);
        opacity: 1;
      }
    }
  }
}

Upvotes: 0

amuthan
amuthan

Reputation: 76

::ng-deep .mat-sort-header-arrow[style] {

  // Hide default arrow stem
  .mat-sort-header-stem {
    display: none;
  }
  .mat-sort-header-indicator {
    opacity: 1;
    color: black;
    font-weight: bold;

    // Hide default arrow as its composed of left, right and middle
    .mat-sort-header-pointer-left, .mat-sort-header-pointer-right, .mat-sort-header-pointer-middle  {
      display: none;
    }
  }
}

// My custom ascending arrow
[aria-sort="ascending"] {
  ::ng-deep .mat-sort-header-arrow {
    .mat-sort-header-indicator {
      &::before {
        content: "\2191";
        top: -1.6em;
        position: absolute;
      }
    }
  }
}

// My custom descending arrow
[aria-sort="descending"] {
  ::ng-deep .mat-sort-header-arrow {
    .mat-sort-header-indicator {
      &::before {
        content: "\2193";
        top: -2.4em;
        position: absolute;
      }
    }
  }
}

Upvotes: 6

Satish Rabari
Satish Rabari

Reputation: 71

Try this.
app.component.ts

import {Component, ViewEncapsulation} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class AppComponent {
  title = 'app';
}


app.component.css

    .mat-sort-header-stem {
    height: 3px !important;
    width: 10px !important;
    transform: rotate(180deg) !important;
}
.mat-sort-header-pointer-left, .mat-sort-header-pointer-right {
    width: 7px !important;
    height: 3px !important;
}
.mat-sort-header-pointer-middle{
    width: 0px !important;
    height: 0px !important;
}

Upvotes: 3

Tameshwar
Tameshwar

Reputation: 809

@Artur

You can try this

[aria-sort='ascending'] {
  ::ng-deep .mat-sort-header-arrow{
    .mat-sort-header-indicator {
      &::before{
        font: normal normal normal 1.1rem/1 FontAwesome;
        content: "\f0d7";
        position: absolute;
        top: .2rem;
      }
    }
  }
}

[aria-sort='descending'] {
  ::ng-deep .mat-sort-header-arrow { 
    .mat-sort-header-indicator {
      &::before{
        font: normal normal normal 1.1rem/1 FontAwesome;
        content: "\f0d8";
        position: absolute;
        top: -.9rem;
      }
    }
  }
}

Upvotes: 2

Related Questions