Angular Material分类标题组件

Angular Material分类标题组件

Angular Material是一个UI组件库,由谷歌开发,目的是为了让Angular开发者以结构化和响应式的方式开发现代应用程序。通过使用这个库,我们可以大大增加终端用户的用户体验,从而为我们的应用程序赢得人气。这个库包含了现代的即用型元素,可以直接使用,只需最少或没有额外的代码。

<mat-sort-header>组件用于根据升序或降序对一个表的数据进行排序。为了使排序工作顺利进行,我们必须<mat-sort-header>在每个表头中加入<mat-sort-header>,并提供一个ID来识别它。这些表头必须放在一个带有matSort指令的父元素中,这样,每当我们点击任何表头进行排序时,就会发出一个称为_matSortChange _的事件,其中包含排序方向和它被触发的列名的ID。

我们可以通过2种方式禁用一个表的排序。

  • 我们可以在matSort指令上添加属性matSortDisabled,或
  • 在任何单一表头上添加disabled属性

语法:

<table matSort (matSortChange)="sortData($event)">
   <tr>
     <th mat-sort-header="column_id">
         Column 1
     </th>
   </tr>
   <tr>
     <td>Data</td>
   </tr>
</table>
HTML

安装:

为了在Angular Material中使用Basic Card Section,我们必须在本地机器上安装Angular CLI,这将有助于添加和配置Angular material库。满足所需条件后,在Angular CLI上输入以下命令。

ng add @angular/material
JavaScript

详细的安装过程请参考在Angular应用程序中添加Angular材料组件的文章。

添加排序头组件:

为了使用sort-header组件,我们需要将其导入我们的组件中,如下所示。

import { Sort } from '@angular/material/sort';
JavaScript

然后,我们需要把这个组件导入app.module.ts文件中。

import {MatSortModule} from '@angular/material/sort';
JavaScript

项目结构:

安装成功后,项目结构将看起来像下面的图片。

Angular Material分类标题组件

Project Structure

例子:下面的例子说明了Angular Material Sort Header的实现。

import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
  
import { BrowserModule } from "@angular/platform-browser";
import { AppComponent } from "./app.component";
import { BrowserAnimationsModule } from
    "@angular/platform-browser/animations";
  
import { SortOverviewExampleComponent } from
    "./sort-overview-example/sort-overview-example.component";
import { MatSortModule } from "@angular/material/sort";
  
@NgModule({
  declarations: 
  [AppComponent, 
  SortOverviewExampleComponent],
  exports: [AppComponent],
  imports:
  [ CommonModule,
    BrowserAnimationsModule,
    BrowserModule,
    MatSortModule,
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}
JavaScript
import { Component, OnInit } from "@angular/core";
import { Sort } from "@angular/material/sort";
  
export interface Student {
  physics: number;
  maths: number;
  chemistry: number;
  name: string;
  roll_number: number;
}
  
@Component({
  selector: "app-sort-overview-example",
  templateUrl: "sort-overview-example.component.html",
  styleUrls: ["./sort-overview-example.component.css"],
})
export class SortOverviewExampleComponent implements OnInit {
  ngOnInit(): void {}
  school: Student[] = [
    { name: "Niall", physics: 91, chemistry: 86, maths: 84, roll_number: 4 },
    { name: "Liam", physics: 73, chemistry: 71, maths: 89, roll_number: 2 },
    { name: "Zayn", physics: 74, chemistry: 91, maths: 99, roll_number: 5 },
    { name: "Harry", physics: 82, chemistry: 80, maths: 92, roll_number: 3 },
    { name: "Louis", physics: 96, chemistry: 76, maths: 93, roll_number: 1 },
  ];
  
  sortedData: Student[];
  
  constructor() {
    this.sortedData = this.school.slice();
  }
  sortData(sort: Sort) {
    const data = this.school.slice();
    if (!sort.active || sort.direction === "") {
      this.sortedData = data;
      return;
    }
  
    this.sortedData = data.sort((a, b) => {
      const isAsc = sort.direction === "asc";
      switch (sort.active) {
        case "name":
          return compare(a.name, b.name, isAsc);
        case "roll_number":
          return compare(a.roll_number, b.roll_number, isAsc);
        case "maths":
          return compare(a.maths, b.maths, isAsc);
        case "physics":
          return compare(a.physics, b.physics, isAsc);
        case "chemistry":
          return compare(a.chemistry, b.chemistry, isAsc);
        default:
          return 0;
      }
    });
  }
}
  
function compare(a: number | string, b: number | string, isAsc: boolean) {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
JavaScript
<h1>GeeksforGeeks</h1>
<h3>matSort & mat-sort-header Example</h3>
<table matSort (matSortChange)="sortData($event)">
    <tr>
        <th mat-sort-header="roll_number">Roll No.</th>
        <th mat-sort-header="name">Student Name</th>
        <th mat-sort-header="maths">Mathematics</th>
        <th mat-sort-header="chemistry">Chemistry</th>
        <th mat-sort-header="physics">Physics</th>
    </tr>
    <tr *ngFor="let student of sortedData">
        <td>{{ student.roll_number }}</td>
        <td>{{ student.name }}</td>
        <td>{{ student.maths }}</td>
        <td>{{ student.chemistry }}</td>
        <td>{{ student.physics }}</td>
    </tr>
</table>
HTML
h1,
h3 {
  color: green;
  font-family: "Roboto", sans-serif;
  text-align: center;
}
table {
  font-family: "Open Sans", sans-serif;
  margin-left: 50px;
}
HTML
<app-sort-overview-example></app-sort-overview-example>
HTML

输出:

Angular Material分类标题组件

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册