【发布时间】:2021-07-28 09:39:23
【问题描述】:
我在 Angular-12 中有一个项目,我正在使用 @tusharghoshbd ngx-datatable 来显示日期。
我有三个表:用户、公司和角色。一个用户可以拥有多个角色,但只能属于一个公司。用户是主表
我在 API JSON GET 请求中得到了整理,如下所示:
{
"message": "You have successfully Retrieved User Detail",
"error": false,
"code": 200,
"results": {
"user": {
"id": 2,
"company_id": 6,
"first_name": "Ashar",
"email": "example@email.com",
"last_name": "Nelo",
"roles": [{
"id": 4,
"name": "HOD",
"guard_name": "api"
},
{
"id": 6,
"name": "Account Officer",
"guard_name": "api"
}
],
"company": {
"id": 6,
"name": "ABC Company",
"website": "https://mycompany.com",
}
},
}
}
现在我尝试使用@tusharghoshbd ngx-datatable 在 Angular 中显示它
组件:
export class SiteInfoComponent implements OnInit {
@ViewChild('actionTpl', {
static: true
}) actionTpl: TemplateRef < any > ;
@ViewChild('addressTpl', {
static: true
}) addressTpl: TemplateRef < any > ;
isLoading: boolean = false;
options: any = {};
userInfoList: any[] = [];
columns: any = {};
constructor(private userInfoService: SUserInfoService) {}
ngOnInit(): void {
this.isLoading = true;
this.userInfoService.getAllUserDetail().subscribe(
data => {
this.userInfoList = data.results.users;
console.log(data);
},
error => {
this.isLoading = false;
}
);
this.options = {
loader: true
};
this.columns = [{
key: 'id',
title: '<div class="blue"><i class="fa fa-id-card-o"></i> ID</div>',
width: 60,
sorting: true,
align: {
head: 'center',
body: 'center'
},
vAlign: {
head: 'bottom',
body: 'middle'
}
},
{
key: 'first_name',
title: '<div class="blue"><i class="fa fa-user"></i> First Name</div>',
width: 100
},
{
key: 'last_name',
title: '<div class="blue"><i class="fa fa-user"></i> Last Name</div>',
width: 100
},
{
key: 'email',
title: '<div class="blue"><i class="fa fa-phone"></i> Email</div>',
align: {
head: 'left'
},
width: 100,
sorting: true
},
{
key: '',
title: '<div class="blue">Action</div>',
align: {
head: 'center',
body: 'center'
},
sorting: false,
width: 80,
cellTemplate: this.actionTpl
}
];
}
edit(row: any) {}
remove(rowIndex: any) {}
}
html:
<ngx-datatable tableClass="table table-striped table-bordered table-hover" [data]="userInfoList" [columns]="columns" [options]="options">
<ngx-caption>
<div class="row">
<div class="col-sm-6 col-xs-6 col-6 ">
<b>
<i class="fa fa-table" aria-hidden="true"></i>
Site Info. List
</b>
</div>
<div class="col-sm-6 col-xs-6 col-6 text-right">
<button type="button" class="btn btn-primary">
<i class="fa fa-plus" aria-hidden="true"></i> Add New Data
</button>
</div>
</div>
</ngx-caption>
<ng-template #addressTpl let-row let-rowIndex="rowIndex" let-columnValue="columnValue">
{{columnValue.name}}, {{columnValue.email}}
</ng-template>
<ng-template #actionTpl let-row let-rowIndex="rowIndex" let-columnValue="columnValue">
<a href="javascript:void(0)" (click)="edit(row)">Edit</a> |
<a href="javascript:void(0)" (click)="remove(rowIndex)">Delete</a>
</ng-template>
</ngx-datatable>
这些工作正常。但在 @tushalghoshbd ngx-datatable 中,我想包含公司和角色的相关字段。每个角色中的名称,以及 API 中显示的公司名称。
我如何做到这一点?
谢谢
【问题讨论】:
标签: angular ngx-datatable