John Theoden
John Theoden

Reputation: 325

Angular 2 - JSON.stringify() object to show in ag-grid

I have ag-grid and from service I get data that i show in grid like this:

My ts code looks like this:

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 );
                    }
                } 
        });
    }

As you can see... properties are actually object and it shows like that in grid on picture one ...My question is, is there any way for me to stringify that "properties" so it will show like string and not object.. Im ok if it shows something like "{location: 0, color; 255}".

Upvotes: 0

Views: 1472

Answers (1)

Ron
Ron

Reputation: 209

Add a valueFormatter to your columnDefs and create the formatter:

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);
}

Upvotes: 3

Related Questions