Angular 2 - 在ag-grid中显示的JSON.stringify()对象

时间:2017-12-20 19:06:59

标签: json angular object stringify

我有ag-grid,从服务中我得到的数据我在网格中显示如下:

我的ts代码如下所示:

constructor( private codeService: CodeService ) {
        this.columnDefs = [
                 { headerName: "Name", field: "name"},
                 { headerName: "Property", field: "properties", editable: true },
                            ];
    }

    ngOnInit() {
        this.codeService.getCodeType( this.name ).subscribe(
                response => { this.handleSuccess( response ); },
                error => { console.error( error ); });

    }

    handleSuccess( aaTypes ) {
        var data = [];
        aaTypes.forEach( ( aaType ) => {
            var entriesForType = [];
            entriesForType.push( aaType );    
                if ( entriesForType.length > 0 ) {
                    entriesForType.forEach( entry => data.push( entry ) );
                    this.data = data;
                    if(this.gridOptions.api !== null){
                        this.gridOptions.api.setRowData( data );
                    }
                } 
        });
    }

正如你所看到的......属性实际上是对象,它在图片中的网格中显示出来......我的问题是,有什么方法可以将字符串化#34;属性"所以它会显示像字符串而不是对象..如果它显示类似" {location:0,color; 255}&#34 ;.

1 个答案:

答案 0 :(得分:1)

将valueFormatter添加到columnDefs并创建格式化程序:

constructor( private codeService: CodeService ) {
    this.columnDefs = [
             { headerName: "Name", field: "name"},
             { headerName: "Property", field: "properties", valueFormatter: jsonFormatter, editable: true },
                        ];
}

function jsonFormatter(d) {
     return JSON.stringify(d);
}