集算表增强
引用计算字段
DataManager 支持从数据源添加字段,新版本还支持具有公式数据类型的虚拟列。
之后可以在集算表的视图中使用这些公式列来显示运行总和或股票价值比率等内容:
- // Add product table.
- var productTable = dataManager.addTable("productTable", {
- remote: {
- read: {
- url: baseApiUrl + "/Product"
- }
- },
- schema: {
- columns: {
- TotalUnits: {
- dataType: "formula",
- value: "[@UnitsInStock] + [@UnitsOnOrder]"
- },
- StockValue: {
- dataType: "formula",
- value: "[@UnitPrice] * ([@TotalUnits])"
- }
- }
- }
- });
- // Bind a view to the table sheet
- var myView = productTable.addView("myView", [
- { value: "Id", caption: "ID", width: 50 },
- { value: "ProductName", caption: "Name", width: 170 },
- { value: "UnitPrice", caption: "Unit Price", style: { formatter: "$#,##0.00" }, width: 120 },
- { value: "UnitsInStock", caption: "Units In Stock", width: 120 },
- { value: "TotalUnits", caption: "Total Units", width: 120 },
- { value: "StockValue", caption: "Stock Value", style: { formatter: "$#,##0.00" }, width: 120 },
- { value: "=SUM([#1:@[UnitPrice]]*([#1:@[UnitsInStock]]+[#1:@[UnitsOnOrder]])", caption: "Running SUM", style: { formatter: "$#,##0.00" }, width: 150 },
- { value: "=[@StockValue]/SUM([UnitPrice] * ([UnitsInStock] + [UnitsOnOrder]))", caption: "Stock Value Ratio", style: { formatter: "0.00%" }, width: 160 }
- ]);
复制代码 针对列的触发公式
集算表中的触发器公式是根据特定条件或触发器进行计算的公式。
这意味着可以根据特定条件重新计算数据,在输入新值时清理数据,或为列提供默认值。以下是触发公式的一些示例:
- var table = dataManager.addTable("Table", {
- schema: {
- columns: {
- createdDate: {
- dataType: "Date",
- trigger: {
- when: "onNew", <<------- apply the formula on created
- formula: "=NOW()", <<------- trigger formula to set current time
- // fields: "*" <<------- when triggered on new, there is no need to specify the affected fields
- },
- },
- updatedDate: {
- dataType: "Date",
- trigger: {
- when: "onNewAndUpdate", <<------- apply the formula on created and updated
- formula: "=NOW()", <<------- trigger formula to set current time
- fields: "*" <<------- all fields changed will have the formula applied to them
- },
- },
- label: {
- trigger: {
- when: "onNewAndUpdate", <<------- apply the formula on updated
- formula: "=UPPER([@label])" <<------- use the upper formula on the input text of the label field
- fields: "label", <<------- when the current column value is updated the formula will be applied
- },
- },
- amount: {
- dataType: "number",
- trigger: {
- when: "onNewAndUpdate", <<------- apply the formula on updated
- formula: "=[@price] * [@quantity]" <<------- automatically evaluate the amount
- fields: "price,quantity", <<------- the changes of the price and quantity columns will cause the formula to calculate
- },
- },
- price: { dataType: 'number' },
- quantity: { dataType: 'number' }
- },
- }
- });
复制代码
|