0

How do I change the cellRenderer function into a reusable variable? I have tried using a constructor, and declaring a variable, seems simple, but any help would be appreciated.

import {Component} from 'angular2/core';

@Component({
    selector: 'app',
    template: '<ag-grid-ng2 class="ag-fresh" style="height: 300px"    [columnDefs]="columnDefs"   [rowData] = "rowData"></ag-grid-ng2>',
    directives: [(<any>window).ag.grid.AgGridNg2]
})
export class SampleAppComponent {

    columnDefs = [
        { headerName: "Make", field: "make" },
        { headerName: "Model", field: "model" },
        {
            headerName: "Price",
            field: "price",
            cellClass: 'rightJustify',
            cellRenderer: function (params: any) {
                return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
            }
        }
    ];
}

What am I doing wrong here?

export class SampleAppComponent {

constructor() {
    this.convertToMoney = function (params: any) {
        return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
    };


columnDefs = [
    { headerName: "Line Type", field: "line" },
    { headerName: "Expense Type", field: "expense" },
    {
        headerName: "Totals",
        field: "totals",
        cellClass: 'rightJustify',
        cellRenderer: convertToMoney()
    }
];

1 Answer 1

1

Not entirely sure what you are trying to achieve here, and not to sound rude, but perhaps checking the proper syntax of typescript first will help you achieve, and understand what you must do.

Anyways.. I will try to help you a bit, I believe you would like a class like this?

import {Component} from 'angular2/core';

@Component({
    selector: 'app',
    template: '<ag-grid-ng2 class="ag-fresh" style="height: 300px"    [columnDefs]="columnDefs"   [rowData] = "rowData"></ag-grid-ng2>',
    directives: [(<any>window).ag.grid.AgGridNg2]
})
export class SampleAppComponent {

    public columnDefs : any[] = [
        { headerName: "Line Type", field: "line" },
        { headerName: "Expense Type", field: "expense" },
        {
            headerName: "Totals",
            field: "totals",
            cellClass: 'rightJustify',
            cellRenderer: this.convertToMoney //reference to class' convertToMoney method
        }
    ];       

    constructor() {}

    private convertToMoney(params: any) : string {
        return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
    } 

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! I will strictly go off the typescript reference from now on. Was referencing ES6 which I thought was the same syntax here: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.