我想通过将数据保存在数组对象( IUser [] )中来执行粗操作。因此,我完成了将表单数据添加到数组并将其显示在数据表中并进行编辑,并且删除也由普通表使用* ngFor完成。
现在,我想使用MatDataSource创建相同的内容。我从一天开始尝试。谁能帮我。 q usermodel.ts
export class Usermodel {
user: IUser= [];
userlist: any;
constructor() { }
addOject(user: IUser): void {
this.user = user;
this.userlist.push(this.user);
}
getUsers(): Observable<IUser[]> {
return Observable.of(this.userlist);
}
}
userdialogue.component.ts
export class UserdialogueComponent implements OnInit {
userList: IUser[];
// for table and pagination
displayedColumns = ['userid', 'firstName', 'mobile', 'lastName'];
dataSource = new MatTableDataSource<IUser>(this.userList);
constructor(private dialog: MatDialog, private user: Usermodel) { }
ngOnInit() {
this.user.getUsers()
.subscribe(result => {
this.userList = result;
this.dataSource.data = this.userList;
});
}
openDialog(): void {
const dialogConfig = new MatDialogConfig(); // for default settings
dialogConfig.position = { right: '0px' };
dialogConfig.disableClose = true;
const dialogRef = this.dialog.open(UserformComponent, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
const userData = {
id: this.generateRandom(),
firstName: result.data.firstName,
lastName: result.data.lastName,
userid: result.data.userid,
mobile: result.data.mobile,
accsprofile: result.data.accsprofile
};
this.user.addOject(userData);
});
}
editDialog(userData: IUser): void {
const dialogConfig = new MatDialogConfig(); // for default settings
dialogConfig.position = { right: '0px' };
dialogConfig.disableClose = true;
dialogConfig.data = userData;
const dialogRef = this.dialog.open(UserformComponent, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
// console.log('Data inserted into records', result);
if (this.isAnObject(result) && this.isEmptyObject(result)) {
const userFormData = {
id: result.data.id,
firstName: result.data.firstName,
lastName: result.data.lastName,
userid: result.data.userid,
mobile: result.data.mobile,
accsprofile: result.data.accsprofile
};
this.user.updateUser(userFormData);
console.log('update done', result);
}
});
}
deleteDialog(userData: IUser): void {
const dialogConfig = new MatDialogConfig(); // for default settings
// dialogConfig.position = { right: '0px' };
dialogConfig.disableClose = true;
dialogConfig.data = { userid: userData.userid, id: userData.id };
const dialogRef = this.dialog.open(UserdeleteComponent, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
if (result) {
this.user.deleteUser(result.id);
console.log('deleted userid:', result.id);
}
});
}
generateRandom(): number {
return Math.floor((Math.random() * 100) + 1);
}
}
userdialogue.componet.html
<mat-table #table *ngIf [dataSource]="dataSource">
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let user"> {{user.firstName}} </mat-cell>
</ng-container>
<ng-container matColumnDef="userid">
<mat-header-cell *matHeaderCellDef> User ID </mat-header-cell>
<mat-cell *matCellDef="let user"> {{user.userid}} </mat-cell>
</ng-container>
<ng-container matColumnDef="mobile">
<mat-header-cell *matHeaderCellDef> Mobile </mat-header-cell>
<mat-cell *matCellDef="let user"> {{user.mobile}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
答案 0 :(得分:1)