Assign variable in CellClassRules AgGrid Angular2

1

I have the following code that works but it has the problem that I have to define 'green' about 20 times, I want to call a function and in that function if it fulfills it that returns the 'green' or in the future another different chain.

BUT NOT having to put 20 times 'green' hair, that is, if it changes, change it on one side only.

columnDefs: [
            {
              field: 'Type'
            },
            {
              field: 'ID',
              cellClassRules: {
                'verde': function(params) {
                  return rowIndexColorDetail(params);
                }
              }
            },
            {
              field: 'Name',
              cellClassRules: {
                'verde' : function(params) {
                  return rowIndexColorDetail(params);
                }
              }
            },

And so 20 times.

function rowIndexColorDetail(params) {
      if (params.node.rowIndex === 0 || params.node.rowIndex === 6) {
        return true;
      } else {
        return false;
      }
    }

I've tried with ...: And that I'm returning the 'green'

  cellClassRules: {
                function(params) {
                  return rowIndexColorDetail(params);
                }
              }

function rowIndexColorDetail(params) {
      if (params.node.rowIndex === 0 || params.node.rowIndex === 6) {
        return 'verde';
      } else {
        return false;
      }
    }

I've also tried But string: I take it as reserved, also to define let color: function(params) { return ....... And also take the color let: as reserved and I can not ...

  cellClassRules: {
                string: function(params) {
                  return rowIndexColorDetail(params);
                }
              }
    
asked by EduBw 03.05.2018 в 12:50
source

1 answer

1

Let's see if I understand: you want to transform this

let columnDefs= [
    {
        field: 'Type'
    },
    {
        field: 'ID',
        cellClassRules: {
            'verde': function (params) {
                return rowIndexColorDetail(params);
            }
        }
    },
    {
        field: 'Name',
        cellClassRules: {
            'verde': function (params) {
                return rowIndexColorDetail(params);
            }
        }
    }
]

In something like

let rules={
    'verde': function (params) {
        return rowIndexColorDetail(params);
    }
}

columnDefs: [
    {
        field: 'Type'
    },
    {
        field: 'ID',
        cellClassRules: rules
    },
    {
        field: 'Name',
        cellClassRules: rules
    }
]

Or, even:

let rules={
    'verde': function (params) {
        return rowIndexColorDetail(params);
    }
}

function generateColumnDef(fieldName, rules) {
    return {
        field: fielName,
        cellClassRules: rules //undefined si rules no es pasado como parámetro
    }
}

columnDefs: [ 
    generateColumnDef('Type'),
    generateColumnDef('ID',rules),
    generateColumnDef('Name',rules),

]
    
answered by 03.05.2018 / 12:57
source