简体   繁体   English

如何在单个 ngx-datatable 单元格中显示嵌套对象?

[英]How can I show nested objects in single ngx-datatable cell?

IAttribute:属性:

export interface IAttribute {
id: number;
attributeName: string;
attributeValues: IAttributeValue[];
}

IAttributeValue:属性值:

export interface IAttributeValue {
Value: string;
attributeId: number;
Id: number;
}

AttributeComponent.ts属性组件.ts

 export class AttributesComponent implements OnInit {
 attributes = [] as Array<IAttribute>;
 rows = [];
 temp = [];

 columns = [{ prop: 'attributeName', name: "Attribute Name" }, { prop:'attributeValues' 
,name: 'Attribute Values' }];
 @ViewChild(DatatableComponent, { static: false }) table: DatatableComponent;

 ColumnMode = ColumnMode;

 constructor(private productService: ProductService,
          private toastr: ToastrService) { }

 ngOnInit(): void {
 this.productService.getAttributes().subscribe({
 next: data => {
  this.attributes = data;
  console.log(this.attributes);
  this.rows = this.attributes;
  

  this.temp = [...this.attributes];
  console.log(this.attributes);
},
error: error => {
  this.toastr.error(error.message, "Error loading data");
}
 })
 }


 updateFilter(event) {
const val = event.target.value.toLowerCase();

// filter our data
// update the rows
this.rows = this.temp.filter(function(d) {
  return d.name.toLowerCase().indexOf(val) !== -1 || !val;
});
// Whenever the filter changes, always go back to the first page
this.table.offset = 0;
   }



}

Attribute.Component.Html属性.组件.Html

 <div class="card-body custome-datatable">
                <input type='text' class="filter-ngx form-control" 
  placeholder='Type to filter the name column...'
                       (keyup)='updateFilter($event)' />
                <ngx-datatable #table class='bootstrap' 
  [columns]="columns" [columnMode]="ColumnMode.force"
                               [headerHeight]="50" 
  [footerHeight]="50" rowHeight="auto" [limit]="10" [rows]="rows">



                </ngx-datatable>
            </div>

Here is how it's showing me data:这是它向我显示数据的方式:

在此处输入图像描述

Here you can see my object:在这里你可以看到我的 object:

在此处输入图像描述

Now my question is how can I show only "value" here which is the property of "attributeValue".现在我的问题是如何在这里只显示“值”,这是“属性值”的属性。 Like red, green, blue etc. I tried reading ngx-datatable docs but they showed only source code no good explanation there.像红色、绿色、蓝色等。我尝试阅读 ngx-datatable 文档,但它们只显示源代码,没有很好的解释。

you have two options.你有两个选择。

  1. Use column template and format the values as follows:使用column template并将值格式化如下:
<ngx-datatable
  #myTable
  class="material expandable"
  [columnMode]="'force'"
  [headerHeight]="50"
  [footerHeight]="50"
  [rowHeight]="50"
  [scrollbarV]="true"
  [rows]="rows"
>
  <ngx-datatable-column name="name">
    <ng-template
      let-rowIndex="rowIndex"
      let-row="row"
      ngx-datatable-cell-template
    >
      <strong>{{ row.name }}</strong>
    </ng-template>
  </ngx-datatable-column>

  <ngx-datatable-column name="company">
    <ng-template
      let-rowIndex="rowIndex"
      let-row="row"
      ngx-datatable-cell-template
    >
      <strong>{{ formatValue(row.company) }}</strong>
    </ng-template>
  </ngx-datatable-column>
</ngx-datatable>

and in component.ts file:component.ts文件中:

  formatValue(values) {
    return values.map((i) => i.name).join(',');
  }
  1. Format the values first in the constructor and then pass it to the Grid:首先在构造函数中格式化值,然后将其传递给 Grid:
this.rowToRender = this.rows.map((r) => {
 let formattedVal = r.company.map((k) => k.name).join(',');
 return {
   ...r,
   company: formattedVal,
 };
});
   

Working Demo工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM