Obtaining a total to a column

Good day, i am currently working on a website that facilitates acquisition of values into a table from users. What i need to display at the end is just the sum of every column say into a text field on my front end. Is there any simple code i can use without having to be a coding guru. All in need is the simplicity one would achieve say in an excel by adding =A1+A70 and getting the total at the point which the formula was inserted.


I have attached a picture of my current table. Your aid is greatly appreciated.

Marcelo,

No such thing as an Excel-like autosum button to click on in Corvid. Coding is the only option. In particular, you will need to get familiar with WixDataAggregate.

i understand that, but i am sure this is already something of a common request, would there be some similar code i could have a look at and try out, i dont even know how to start this. I read a bit on hooks but the examples are so complex i dont wanna touch the back end just anyhow

You can run a JavaScript method like reduce() on the retrieved data array to sum up each field.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

could you please explain to me how would this generate an out put of say 50 is the field “Poupanca” had 20 for line 1 and 30 for line 2, please note this table is already preloaded with 7o lines i simple want to add whichever value is added on those fields

I don’t know how you populate your table. From a collection query? from a dataset? and in any case I think that Anthony’s solution may be simpler and relatively easy to implement.

with a dataset, here is an already filled table : https://em1000x.wixsite.com/kendlemuka/kurhula password to this area is “kk” without the quotes. i havent placed a field for the result but i would use say a text box in read only mode for such

Maybe that will work (I wrote it quickly and haven’t tested it, so there might be bugs and errors):

//run one time
let sumupObj = {};
let tableKeys = $w("#table1").columns;
let tableRows  =  $w("#table1").rows;
tableKeys.forEach((key) => {
sumupObj[key.dataPath] =  0;
})
for(let i = 0; i < tableRows.length;  i++){
tableKeys.forEach((key) => {
if(typeof tableRows[i][key.dataPath] !== "undefined"){
sumupObj[key.dataPath] += Number(tableRows[i][key.dataPath]);
}
})
}
sumupObj.numero = "Total";
tableRows.push(sumupObj);
$w("#table1").rows = tableRows;

thank you for taking the time to help me, do i place this into a textbox to display the result or in the backend data.js

No. This code is supposed to add a new row to the bottom of your table and display the total there.

and it’s for the front-end code panel. You should run it after the table has been populated, and it should add the desired row.

i dont know if this helps, this is the backend, i tried placing the code on the page code i also tried on a hook on the data.js i get the same error that #Esperanca is not a valid selector, i get htis wherever there is an #table1…I have added a read only text box to display the result of the sum of all Poupanca values as per the front end.

I am specifically speaking of this page, if i get this one right i will be able to prpagate the same code to all other areas. Thank again

i dont think it would look good by adding a new row, the table stays at 70 rows at all times nothing more nothing less, it will have information added to any of those 70 rows at random depending on whether the person has pitched or not. The values are always integers, without cents always full integers

First of all, don’t event try to use it on the backend. It won’t work.
Second, this error means your table property Id is not: “Esperanca”. check again.
Third, wrap the code in :

  $w.onReady( function() {
   $w("#myDataset").onReady( () => {
     })
      }) 

Forth, if you don’t want to have it in the bottom row, then delete:

sumupObj.numero = "Total";
tableRows.push(sumupObj);
$w("#table1").rows = tableRows;

and instead use:

$w("#totalPoupenca").text = sumupObj.poupenca.toString();
//push all your fields to the desired text-boxes

thank you, i am starting to understand the reason why table1 is used as a selector here and could clear those errors, i am now getting #totalPoupanca as an invalid selector, dont know if the fact that “Poupanca” is a Number field, thanks again

i also think i could explore Anthony’s suggestion, i dont know how to start the Agregate, where exactly do i place the aggregate code???

If you didn’t define the text-box Id in the properties panel to be “totalPoupanca”, you’ll keep getting this error.

i have now defined i get an error that text dont exist on totalPoupanca most likely cause it is a number not a string. i dont think what i want is really complex to achieve. Please visit my sample page here: https://em1000x.wixsite.com/kendlemuka/esperanca

What happens here:

  1. table has 70 lines with line 1 being loaded by default
  2. users load numbers in every box except the date box
  3. moving to the next line will automatically submit the values into the previous line and so on
    4.The table gets updated on the fly and numbers can be updated at will
  4. it serves the purpose of collecting the right data but we still need to export the data into an .csv file and then load on an excel that has all the formulas that autocalculate the total, like the total of Poupanca, Reembolso, FS respectivey.

All we need is to display the values of the sum of each column as the user inputs the various values

Let’s see if someone else will be able to assist.

thanks for the try JD really appreciated