首页 > 解决方案 > ng2-smart-table 派生列

问题描述

我需要通过计算两列的值在 ng2-smart-table 中创建一个自定义列。

我尝试使用 valuePrepareFunction() 但它不起作用

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

我需要通过 = OrderQuantity*UnitPrice 获得总值

标签: angularng2-smart-table

解决方案


正如您所提到的,您可以通过使用 ng2-smart-table 中的 valuePrepareFunction 来做到这一点。

根据文档,将使用 2 个参数调用此函数:单元格、行。因此,您可以简单地使用它,如下所示。

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

推荐阅读