最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ag grid: Array object values is not displaying properly - Stack Overflow

programmeradmin4浏览0评论

How to load the following array object mark "name" values in cell.

Here the object looks like

 {
      id: "1"
      mark: [
        0: {name: "AUS", id: 1000}
        1: {name: "BRA", id: 1050}
        2: {name: "CHN", id: 1100}
        3: {name: "ECE", id: 1200}
        4: {name: "EG", id: 1250}
        5: {name: "JAP", id: 1450}
        6: {name: "RUS", id: 1500}
      ]
    }

HTML

<ag-grid-angular #agGrid style="width: 100%; height: 100%;" id="myGrid" class="ag-theme-balham cis-ag-grid"
      [columnDefs]="columnDefs" [defaultColDef]="defaultColDef" [rowSelection]="rowSelection" [rowData]="rowData"
      [context]="context" [frameworkComponents]="frameworkComponents" (selectionChanged)="onSelectionChanged()"
      (rowClicked)='onRowClicked($event)' (gridReady)="onGridReady($event)" [gridOptions]="gridOptions">
    </ag-grid-angular>

TS Inside constructor (),

this.defaultColDef = {
      resizable: true,
      sortable: true,
      filter: true,
      headerComponentParams: { menuIcon: 'fa fa-filter' }
    };

this.columnDefs = [  
{
  headerName: "ID",            
  minWidth: 144,
  field: "id", 
  valueGetter: "data.id"
},
{
  headerName: "Mark",            
  minWidth: 144,
  field: "mark", 
  valueGetter: "data.mark.name"
}];

currently, It is displaying in grid column cell like,

ID        Mark
--------------
1         [object Object], [object Object], [object Object], [object Object], 
          [object Object], [object Object], [object Object]

Expected is,

ID        Mark
  --------------
   1       AUS, BRA, CHN, ECE, EG,JAP,RUS

How to load the following array object mark "name" values in cell.

Here the object looks like

 {
      id: "1"
      mark: [
        0: {name: "AUS", id: 1000}
        1: {name: "BRA", id: 1050}
        2: {name: "CHN", id: 1100}
        3: {name: "ECE", id: 1200}
        4: {name: "EG", id: 1250}
        5: {name: "JAP", id: 1450}
        6: {name: "RUS", id: 1500}
      ]
    }

HTML

<ag-grid-angular #agGrid style="width: 100%; height: 100%;" id="myGrid" class="ag-theme-balham cis-ag-grid"
      [columnDefs]="columnDefs" [defaultColDef]="defaultColDef" [rowSelection]="rowSelection" [rowData]="rowData"
      [context]="context" [frameworkComponents]="frameworkComponents" (selectionChanged)="onSelectionChanged()"
      (rowClicked)='onRowClicked($event)' (gridReady)="onGridReady($event)" [gridOptions]="gridOptions">
    </ag-grid-angular>

TS Inside constructor (),

this.defaultColDef = {
      resizable: true,
      sortable: true,
      filter: true,
      headerComponentParams: { menuIcon: 'fa fa-filter' }
    };

this.columnDefs = [  
{
  headerName: "ID",            
  minWidth: 144,
  field: "id", 
  valueGetter: "data.id"
},
{
  headerName: "Mark",            
  minWidth: 144,
  field: "mark", 
  valueGetter: "data.mark.name"
}];

currently, It is displaying in grid column cell like,

ID        Mark
--------------
1         [object Object], [object Object], [object Object], [object Object], 
          [object Object], [object Object], [object Object]

Expected is,

ID        Mark
  --------------
   1       AUS, BRA, CHN, ECE, EG,JAP,RUS
Share Improve this question edited Jul 18, 2019 at 5:20 klmuralimohan asked Jul 18, 2019 at 4:42 klmuralimohanklmuralimohan 9516 gold badges23 silver badges45 bronze badges 4
  • 1 Show the HTML file and ponent code – Shashank Vivek Commented Jul 18, 2019 at 4:45
  • Updated the post with expected result – klmuralimohan Commented Jul 18, 2019 at 4:47
  • 1 Hi, I am asking about .html and .ts file. – Shashank Vivek Commented Jul 18, 2019 at 4:49
  • Updated the post HTML and ts – klmuralimohan Commented Jul 18, 2019 at 5:22
Add a ment  | 

6 Answers 6

Reset to default 2

Try to this for Ag-grid.

Set Column:-

constructor() {
 this.columnDefs = [                    
  {
    headerName: 'Id', sortable: true, resizable: true, valueGetter: 'data.mark.id'
  },
  {
    headerName: 'Mark', sortable: true, resizable: true, valueGetter: 'data.mark.name'
  }
 ]
}

In typescript file :-

public dataMarkList:any;

constructor() {

}

yourGetFunction() {
   this.dataMarkList = data.mark
}

And set rowData properties in ag-grid like

<ag-grid-angular class="ag-theme-material" style="width: 100%; height: calc(100vh - 200px);" rowSelection="single"
   animateRows="true" [rowHeight]="43" [columnDefs]="columnDefs" rowModelType="infinite"
   paginationPageSize="50" [rowData]="dataMarkList">
</ag-grid-angular>

Since there isn't any HTML code provided, you can try:

<span *ngFor="let obj of data.mark">
 {{ obj.name }},
</span>

inside that HTML column.

It's ing as [object Object] because it an object ({name: "AUS", id: 1000}). You can visualize it by putting pipe as | json in {{ obj | json }}

there is no html provided so a reproducible plunker or stackblitz will help. Try like this:

<div *ngFor="let data of dataList">
     <span *ngFor="let item of data.mark">
        {{ item .name }},
      </span>
 </div>

I used Cell Renderer to acplish this. I use Vue not Angular. This is how I did it in Vue.

I created a Cell Renderer ponent file called CellRenderList.vue and imported it into the parent. For defining the field in the parent file I did.

headerName: 'SomeName',
field: 'mark',
cellRendererFramework: 'CellRendererList',

Importing the ponent and adding that cell renderer line should be the only thing you need to do on your original file.

My CellRenderList.vue ponent file looks like this.

<template>
 <div class="">
  <span v-for="(value, index) in mark">
    <span v-if="index != 0">, </span><span>{{ value.name }}</span>
  </span>
 </div>
</template>

<script>
  export default {
    name: 'CellRendererList',
    puted: {
      mark() {
        return this.params.data.mark
      }
    }
  }
</script>

The Ag Grid documentation on Cell Renderer has some good examples how to do it in Angular.

this.columnDefs = [  
{
  headerName: "ID",            
  minWidth: 144,
  field: "id",
},
{
  headerName: "Mark",            
  minWidth: 144,
  field: "mark", 
  valueGetter: (row: any) => {
        return row.data.mark.name;
      },
}];

this works for me:

{
  headerName: 'SomeName',
  field: 'mark',
  valueGetter: (row: any) => {
    return row.data.mark.map((mark: any) => profile.name);
  },
},
发布评论

评论列表(0)

  1. 暂无评论