Damitha
Damitha

Reputation: 23

ng2-smart-table derived column

I need to create a custom column in ng2-smart-table by calculating values of two columns.

I tried using valuePrepareFunction() but it wont work

    OrderQuantity:{
      title: 'OrderQuantity',
    },
    UnitPrice:{
      title: 'UnitPrice',
    },
    Total:{
      title: 'Total',
      type: 'custom',
      //Need to get total by : OrderQuantity*UnitPrice
    },

I need to get the total value by = OrderQuantity*UnitPrice

Upvotes: 2

Views: 2144

Answers (1)

Buddhika
Buddhika

Reputation: 248

As you mentioned you could do this by using the valuePrepareFunction in the ng2-smart-table.

As per the document this function will be invoked with 2 parameters: cell, row. Therefore you could simple use this as follows.

settings = {
columns: {
OrderQuantity:{
      title: 'OrderQuantity',
    },
    UnitPrice:{
      title: 'UnitPrice',
    },
    Total:{
      title: 'Total',
      valuePrepareFunction :(cell, row) =>{
          return row.OrderQuantity * row.UnitPrice;
     } 
    }
}
}

Upvotes: 3

Related Questions