Search code examples
angularng2-smart-table

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


Solution

  • 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;
         } 
        }
    }
    }