[英]Add editable field to mat-row in mat-table in angular 9
I am working on a requirement for an angular screen to allow users to maintain data in a table that has a single column.我正在研究 angular 屏幕的要求,以允许用户在具有单列的表中维护数据。 The code I have below is working to display the data correctly, but so far I have not been able to find any examples where I can make the one column of data editable.
我下面的代码正在正确显示数据,但到目前为止,我还没有找到任何可以使一列数据可编辑的示例。
HTML HTML
<div class="SERIALCLASS">
<br/>
<div class="center" >
Serial Exceptions<br/>
<div>
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="partNo">
<th mat-header-cell *matHeaderCellDef mat-sort-header style="width: 300px !important;padding-right: 15px !important;"> Part No: </th>
<td mat-cell *matCellDef="let row" style="padding-right: 15px !important;"> {{row.partNo}} </td>
</ng-container>
<ng-container matColumnDef="partNo1">
<th mat-header-cell *matHeaderCellDef style="width: 300px !important;padding-right: 15px !important;">
<mat-form-field class="filter" floatLabel="never" style="width: 80px !important;">
<mat-label>Filter</mat-label>
<input matInput [formControl]="partNoFilter">
</mat-form-field></th>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
<tr mat-header-row *matHeaderRowDef="displayedColumns1"> </tr>
</table>
</div>
</div>
</div>
.TS file .TS 文件
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { UserApiService } from 'src/app/services/api/user-apiservice';
@Component({
selector: 'app-serial-exceptions',
templateUrl: './serial-exceptions.component.html',
styleUrls: ['./serial-exceptions.component.scss']
})
export class SerialExceptionsComponent implements OnInit {
isLoading = true;
isempty = false;
dataSource = new MatTableDataSource<any>([]);
partNoFilter = new FormControl('');
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
displayedColumns: string[] = ['partNo']
displayedColumns1: string[] = ['partNo1']
filterValues = {
partNo: ''
}
constructor(
private repoService: UserApiService,
) { }
ngOnInit(): void {
this.getData();
this.partNoFilterFunc();
}
getData(){
this.isLoading = true
this.isempty = false;
setTimeout(() => {
this.repoService.getSerialExceptions().subscribe(largeDataSet => {
console.log(largeDataSet);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.dataSource.data = largeDataSet;
})
})
this.dataSource.filterPredicate = this.createFilter();
}
partNoFilterFunc(){
this.partNoFilter.valueChanges
.subscribe(
partNo => {
this.filterValues.partNo = partNo;
this.dataSource.filter = JSON.stringify(this.filterValues);
}
)
}
createFilter(): (data: any, filter: string) => boolean {
let filterFunction = function(data, filter): boolean {
let searchTerms = JSON.parse(filter);
return data.partNo.toString().toLowerCase().indexOf(searchTerms.partNo.toLowerCase()) !== -1;
}
return filterFunction;
}
}
A co-worker pointed me to this example, but for what I am trying to do it seems overly complicated.一位同事向我指出了这个例子,但对于我正在尝试做的事情来说,它似乎过于复杂。 In fact I backed out the work I did to try this example because of the numerous errors I was getting.
事实上,由于我遇到了许多错误,我放弃了我为尝试这个示例所做的工作。
https://stackblitz.com/edit/inline-edit-mat-table?file=app%2Finline-edit%2Finline-edit.component.ts https://stackblitz.com/edit/inline-edit-mat-table?file=app%2Finline-edit%2Finline-edit.component.ts
Is there an easy way to make a column editable within a mat-row>有没有一种简单的方法可以在 mat-row 中使列可编辑>
Easiest way is to use contenteditable
HTML attribute最简单的方法是使用
contenteditable
HTML 属性
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.