Dependent columndef

Dependent columndef

karliekarlie Posts: 83Questions: 17Answers: 0

columndef when value of other column is 'x'

I have a column called 'Type' with 3 options EACH, PCT,GRAM
I want to set a column definition for the 'Price' column If 'Type' = PCT

{ targets: ['price'], render: function (data, type, row, meta) {var row['type'] == ['PCT'];return row['ct'] * row['ppct'];} },

The above code doesn't work, but any pointers would be fab.

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    I don't believe your Javascript is correct. The var row['type'] == ['PCT']; uses a comparison operator rather than an assignment among a few other issues with it.

    Try:

    if ( row['type'] = 'PCT' ) {
      return row['ct'] * row['ppct'];
    }
    // .. else return what ?
    

    Allan

  • karliekarlie Posts: 83Questions: 17Answers: 0

    I tried this but it gives an Uncaught SyntaxError: Unexpected token if

    { targets: ['price'], render: function (data, type, row, meta) 
    if ( row['type'] = 'PCT' ) {
      return row['ct'] * row['ppct'];
    } },
    
  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    Your function doesn't seem to have an opening curly brace.

  • karliekarlie Posts: 83Questions: 17Answers: 0

    Thanks for the help Tangerine & Allan, this worked for me in the end

    //Price when different TYPE is selected
            { targets: ['price'], render: function (data, type, row, meta)
            {
            if ( row['type'] == 'PCT' ) {
            return "£" + row['ct'] * row['ppct'];
            }
            if ( row['type'] == 'EACH' ) {
            return "£" + row['each_price'];
            }
            }
            },
    

    Not sure if it's the most efficient way to be honest as I'm a bit out of my depth!

This discussion has been closed.