0

I have a function called getColor(params) which returns a string to be used for CSS.

I'm trying to call it from another function but instead of the color being returned, I'm being returned the literal function body which is being inserted into my CSS.

Calling Function

coldef = (processedJson) => {
    this.customColumns.push(
        {
            headerName: "Test Set Name", field: "field1", width: 310,
            cellStyle: { 'font-size': '12px;' }
        });

    this.customColumns.push(
        {
            headerName: "Test Case Name", field: "field2", width: 310,
            cellStyle: { 'font-size': '12px;' }
        });

    for (var i = 0; i < this.columns.length; i++) {
        var item = this.columns[i];
        this.customColumns.push(
            {
                headerName: this.columns[i], field: this.columns[i], width: 110,
                cellStyle: { 'font-size': '12px;', 'background-color': this.getColor }
            });
    }
}

My problem is the function is returning function body as text.

this.getColor accepts a parameter called params, but from my understanding this value is inserted by angular and contains my JSON object.

How can I get getColor to return just the string I need?

6
  • 1
    Simply add (arguments) to this.getColor. How do you normally call a function? Commented Jan 25, 2018 at 15:07
  • 2
    You need to call it as a function, this.getColor(params) Commented Jan 25, 2018 at 15:07
  • this.getColor is being called when this code runs, not when it's displayed Commented Jan 25, 2018 at 15:08
  • @BenKolyaMansley the problem is params is not in scope inside the function.. Commented Jan 25, 2018 at 15:09
  • Then you've got to find some way to put it into the function Commented Jan 25, 2018 at 15:11

3 Answers 3

1

You're using this :

'background-color': this.getColor

while your function awaits for a parameter (according to you). Doesn't that seems strange to you ?

To call a function, you need to write the parenthesis like so :

'background-color': this.getColor()

In your case, you should also give it params, this means put something into the parenthesis.

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

Comments

1

You need to provide params to the getColor function and then actually call it as others suggested. There are two ways how you can get params in the call:

  • Pass it as an argument to colddef function:
coldef = (processedJson, params) => {
  this.customColumns.push(
    {
        headerName: "Test Set Name", field: "field1", width: 310,
        cellStyle: { 'font-size': '12px;' }
    });

  this.customColumns.push(
    {
        headerName: "Test Case Name", field: "field2", width: 310,
        cellStyle: { 'font-size': '12px;' }
    });

  for (var i = 0; i < this.columns.length; i++) {
    var item = this.columns[i];
    this.customColumns.push(
        {
            headerName: this.columns[i], field: this.columns[i], width: 110,
            cellStyle: { 'font-size': '12px;', 'background-color': this.getColor(params) }
        });
  }
}
  • Add it to the param object/scope (this):
this.params = params;

coldef = (processedJson) => {
  this.customColumns.push(
    {
        headerName: "Test Set Name", field: "field1", width: 310,
        cellStyle: { 'font-size': '12px;' }
    });

  this.customColumns.push(
    {
        headerName: "Test Case Name", field: "field2", width: 310,
        cellStyle: { 'font-size': '12px;' }
    });

  for (var i = 0; i < this.columns.length; i++) {
    var item = this.columns[i];
    this.customColumns.push(
        {
            headerName: this.columns[i], field: this.columns[i], width: 110,
            cellStyle: { 'font-size': '12px;', 'background-color': this.getColor(this.params) }
        });
  }
}

Comments

0

My problem was that params in angular is only in scope inside of a closure. I needed to just pass it down to getColor

 for (var i = 0; i < this.columns.length; i++) {
     var item = this.columns[i];
     this.customColumns.push(
         {   headerName: this.columns[i], field: this.columns[i], width: 110,
             cellStyle: (params) => {
                return { 
                   'font-size': '12px;', 'background-color': this.getColor(params)
                }; 
             }
         }
     );
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.