如何使用angular7超链接数据表中的表列值?
在我的应用程序中,我获取数据表中的数据。应用程序前端正在angular7上运行。现在我希望一些列的值应该有一个链接,然后单击链接将显示一个新组件。 例如:如何使用angular7超链接数据表中的表列值?,angular,angular-material,Angular,Angular Material,在我的应用程序中,我获取数据表中的数据。应用程序前端正在angular7上运行。现在我希望一些列的值应该有一个链接,然后单击链接将显示一个新组件。 例如: Column1 Column2 Column3 value with Link 如何使用angular7实现此场景 我正在查看中的路由文档 但无法将其映射到我当前的要求。如果我没弄错,您使用的是角材料数据表,不是吗 如果是,请尝试以下方法: <ng-container matColumnDef="pro
Column1 Column2 Column3
value with Link
如何使用angular7实现此场景
我正在查看中的路由文档
但无法将其映射到我当前的要求。如果我没弄错,您使用的是角材料数据表,不是吗 如果是,请尝试以下方法:
<ng-container matColumnDef="prop">
<mat-header-cell *matHeaderCellDef> Prop Name </mat-header-cell>
<mat-cell *matCellDef="let element">
<a routerLink="/your-link">{{element.prop}}</a>
</mat-cell>
</ng-container>
道具名称
{{element.prop}}
如果我没弄错的话,您使用的是角材料数据表,不是吗
如果是,请尝试以下方法:
<ng-container matColumnDef="prop">
<mat-header-cell *matHeaderCellDef> Prop Name </mat-header-cell>
<mat-cell *matCellDef="let element">
<a routerLink="/your-link">{{element.prop}}</a>
</mat-cell>
</ng-container>
道具名称
{{element.prop}}
解决方案1:使用角度材质表
import { ProductService } from './../data-service/data-service-product/product.service';
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IProduct } from '../models/IProduct';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort} from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, AfterViewInit, OnDestroy {
products: IProduct[];
filteredProducts: any[];
subscriptionVariable: Subscription;
displayedColumns = ['title', 'price', 'action'];
dataSource = new MatTableDataSource();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
constructor(private productDataService: ProductService) {
this.subscriptionVariable = this.productDataService.getAll().subscribe(
p => this.dataSource.data = this.filteredProducts = this.products = p as IProduct[] );
}
ngOnInit() {
}
// tslint:disable-next-line: use-lifecycle-interface
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
ngOnDestroy() {
this.subscriptionVariable.unsubscribe();
}
}
export interface IProduct {
category: string;
imageUrl: string;
price: number;
title: string;
}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of filteredProducts">
<td>
{{ product.title}}
</td>
<td>
{{ product.price}}
</td>
<td>
<a [routerLink] ="['/admin/products/', product.key]" routerLinkActive="router-link-active" >Edit</a>
</td>
</tr>
</tbody>
</table>
如果您专门处理角度材质,并且发现在数据表列中添加超链接很棘手,那么下面的解决方案将有所帮助
.ts文件
import { ProductService } from './../data-service/data-service-product/product.service';
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IProduct } from '../models/IProduct';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort} from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, AfterViewInit, OnDestroy {
products: IProduct[];
filteredProducts: any[];
subscriptionVariable: Subscription;
displayedColumns = ['title', 'price', 'action'];
dataSource = new MatTableDataSource();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
constructor(private productDataService: ProductService) {
this.subscriptionVariable = this.productDataService.getAll().subscribe(
p => this.dataSource.data = this.filteredProducts = this.products = p as IProduct[] );
}
ngOnInit() {
}
// tslint:disable-next-line: use-lifecycle-interface
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
ngOnDestroy() {
this.subscriptionVariable.unsubscribe();
}
}
export interface IProduct {
category: string;
imageUrl: string;
price: number;
title: string;
}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of filteredProducts">
<td>
{{ product.title}}
</td>
<td>
{{ product.price}}
</td>
<td>
<a [routerLink] ="['/admin/products/', product.key]" routerLinkActive="router-link-active" >Edit</a>
</td>
</tr>
</tbody>
</table>
.interface
import { ProductService } from './../data-service/data-service-product/product.service';
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IProduct } from '../models/IProduct';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort} from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, AfterViewInit, OnDestroy {
products: IProduct[];
filteredProducts: any[];
subscriptionVariable: Subscription;
displayedColumns = ['title', 'price', 'action'];
dataSource = new MatTableDataSource();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
constructor(private productDataService: ProductService) {
this.subscriptionVariable = this.productDataService.getAll().subscribe(
p => this.dataSource.data = this.filteredProducts = this.products = p as IProduct[] );
}
ngOnInit() {
}
// tslint:disable-next-line: use-lifecycle-interface
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
ngOnDestroy() {
this.subscriptionVariable.unsubscribe();
}
}
export interface IProduct {
category: string;
imageUrl: string;
price: number;
title: string;
}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of filteredProducts">
<td>
{{ product.title}}
</td>
<td>
{{ product.price}}
</td>
<td>
<a [routerLink] ="['/admin/products/', product.key]" routerLinkActive="router-link-active" >Edit</a>
</td>
</tr>
</tbody>
</table>
component.html
<p>
<a routerLink="/admin/products/new" class="btn btn-primary">
New Product
</a>
</p>
<p>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</p>
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header> Title </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.title}} </mat-cell>
</ng-container>
<ng-container matColumnDef="price">
<mat-header-cell *matHeaderCellDef mat-sort-header> Price </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.price}} </mat-cell>
</ng-container>
<!-- Star Column -->
<ng-container matColumnDef="action" >
<mat-header-cell *matHeaderCellDef > Action </mat-header-cell>
<mat-cell *matCellDef="let element">
<a [routerLink] ="['/admin/products/', element.key]" routerLinkActive="router-link-active" >Edit</a>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [length]="5" [pageSize]="5" [pageSizeOptions]="[5, 10, 25]">
</mat-paginator>
</div>
解决方案1:使用角度材质表
import { ProductService } from './../data-service/data-service-product/product.service';
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IProduct } from '../models/IProduct';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort} from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, AfterViewInit, OnDestroy {
products: IProduct[];
filteredProducts: any[];
subscriptionVariable: Subscription;
displayedColumns = ['title', 'price', 'action'];
dataSource = new MatTableDataSource();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
constructor(private productDataService: ProductService) {
this.subscriptionVariable = this.productDataService.getAll().subscribe(
p => this.dataSource.data = this.filteredProducts = this.products = p as IProduct[] );
}
ngOnInit() {
}
// tslint:disable-next-line: use-lifecycle-interface
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
ngOnDestroy() {
this.subscriptionVariable.unsubscribe();
}
}
export interface IProduct {
category: string;
imageUrl: string;
price: number;
title: string;
}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of filteredProducts">
<td>
{{ product.title}}
</td>
<td>
{{ product.price}}
</td>
<td>
<a [routerLink] ="['/admin/products/', product.key]" routerLinkActive="router-link-active" >Edit</a>
</td>
</tr>
</tbody>
</table>
如果您专门处理角度材质,并且发现在数据表列中添加超链接很棘手,那么下面的解决方案将有所帮助
.ts文件
import { ProductService } from './../data-service/data-service-product/product.service';
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IProduct } from '../models/IProduct';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort} from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, AfterViewInit, OnDestroy {
products: IProduct[];
filteredProducts: any[];
subscriptionVariable: Subscription;
displayedColumns = ['title', 'price', 'action'];
dataSource = new MatTableDataSource();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
constructor(private productDataService: ProductService) {
this.subscriptionVariable = this.productDataService.getAll().subscribe(
p => this.dataSource.data = this.filteredProducts = this.products = p as IProduct[] );
}
ngOnInit() {
}
// tslint:disable-next-line: use-lifecycle-interface
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
ngOnDestroy() {
this.subscriptionVariable.unsubscribe();
}
}
export interface IProduct {
category: string;
imageUrl: string;
price: number;
title: string;
}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of filteredProducts">
<td>
{{ product.title}}
</td>
<td>
{{ product.price}}
</td>
<td>
<a [routerLink] ="['/admin/products/', product.key]" routerLinkActive="router-link-active" >Edit</a>
</td>
</tr>
</tbody>
</table>
.interface
import { ProductService } from './../data-service/data-service-product/product.service';
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IProduct } from '../models/IProduct';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort} from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, AfterViewInit, OnDestroy {
products: IProduct[];
filteredProducts: any[];
subscriptionVariable: Subscription;
displayedColumns = ['title', 'price', 'action'];
dataSource = new MatTableDataSource();
@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;
constructor(private productDataService: ProductService) {
this.subscriptionVariable = this.productDataService.getAll().subscribe(
p => this.dataSource.data = this.filteredProducts = this.products = p as IProduct[] );
}
ngOnInit() {
}
// tslint:disable-next-line: use-lifecycle-interface
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
}
ngOnDestroy() {
this.subscriptionVariable.unsubscribe();
}
}
export interface IProduct {
category: string;
imageUrl: string;
price: number;
title: string;
}
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of filteredProducts">
<td>
{{ product.title}}
</td>
<td>
{{ product.price}}
</td>
<td>
<a [routerLink] ="['/admin/products/', product.key]" routerLinkActive="router-link-active" >Edit</a>
</td>
</tr>
</tbody>
</table>
component.html
<p>
<a routerLink="/admin/products/new" class="btn btn-primary">
New Product
</a>
</p>
<p>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</p>
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header> Title </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.title}} </mat-cell>
</ng-container>
<ng-container matColumnDef="price">
<mat-header-cell *matHeaderCellDef mat-sort-header> Price </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.price}} </mat-cell>
</ng-container>
<!-- Star Column -->
<ng-container matColumnDef="action" >
<mat-header-cell *matHeaderCellDef > Action </mat-header-cell>
<mat-cell *matCellDef="let element">
<a [routerLink] ="['/admin/products/', element.key]" routerLinkActive="router-link-active" >Edit</a>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [length]="5" [pageSize]="5" [pageSizeOptions]="[5, 10, 25]">
</mat-paginator>
</div>
您如何显示数据表?我正在使用角度材质显示数据表。Column1{{event.coulmn1}}如何显示数据表?我使用角度材质来显示数据表。Column1{{event.coulmn1}}对于链接,我需要创建一个新组件。不,用RouterLink包装你的值。我想我需要在router.module.ts中映射链接。否则会出现错误::无法匹配任何路由如何将查询参数与routerLink一起发送?@stumbler,您可以参考我在本Regard中提供的解决方案,以获得创建新组件所需的链接。不,只需将您的值与routerLink打包。我想我需要在router.module.ts中映射链接。否则会出现错误::无法匹配任何路由如何使用routerLink发送查询参数?@stumbler,在这方面,您可以参考我提供的解决方案