"=sum" function? (Calculating a column)

It’s great we can see how many sweets Tabitha sold on Sept 17, but how many brownies, (or cookies, or peanut brittle) did she sell total without having to count them myself?

So you need to code the content of your columns. So to add a column:

let cols = $w("#myTable").columns; 
cols.push( { 
"id": "newCol", 
"dataPath": "new_col", 
"label": "New Column", 
"width": 100, 
"visible": true, 
"type": "string" 
} ); 
$w("#myTable").columns = cols;

That code will push in a new column at the end of your existing columns. So in that you need to walk through the columns you already have. read about array.forEach anywhere and use that to sum up like the below code sample but I am not sure that will work at all.

Loop through all rows and sum up the correct fieldkeys values like below code shows you.

let brownieTotal = 0;
let cookiesTotal = 0;
let peanutBrittle = 0;
let tableRows = $w("#myTable").rows; 
tableRows.forEach((item) => {
  brownieTotal  += item["brownieFieldKey"]; // "field value"
  cookiesTotal  += item["cookieFieldKey"]; // "field value"
  peanutBrittleTotal  += item["peanutBrittleFieldKey"]; // "field value"
});

// Push a new row into your table with your totals in it!
rows.push(
{
  dateFieldKey: "Totals",
  brownieFieldKey: brownieTotal, 
  cookieFieldKey: cookiesTotal,
  peanutBrittleFieldKey: peanutBrittleTotal
}); 

// Reassign the rows into the #myTable which is the ID of your table.
$w("#myTable").rows = rows;

If you like this post please mark it as TOP COMMENT, it helps me a lot.

1 Like