How to calculate rows in a table.

I have a table with 3 pages and I would like to have a script that can calculate the total rows from these 3 pages and put it in the TEXT box as shown in the link here
https://www.mascusa.com/renewal-membership-test
Any help will be greatly appreciated.

@hl1 There’s several ways to do it. This way will return the desired result whether you are using a dataset to populate the table or otherwise. Having the code inside the onReady function for the page and dataset assures that the page elements and dataset is fully loaded before trying to run any functions. If you are not using a dataset, you can remove the second onReady pertaining to the dataset and a set of closing braces/parenthesis.

$w.onReady(function () {
    $w('#dataset1').onReady(() => {
        let recs = $w("#table1").rows.length;
        $w("#RenewalMembers").text = recs.toString();
    });
});

@hl1 or you can do something like this

import wixData from ‘wix-data’;

wixData.query(“yourCollectionName”)
.count()
.then( (num) => {
let allResults = num;
$w(“#TotalRenewalMembers”).text = allResults.toString();
} );


It will count the total rows on your collection database.
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#count

Dear Anthonyb and Jefferson Uy. The codes that both of you provided are working. Thank you very much!

I hope you don’t mind if I need another help for calculating the grand total of 2 tables side by side as shown in this link.
https://www.mascusa.com/renewal-membership-test2

Note* there are 2 dataset connected to 2 different collection

Any help will be greatly appreciated.

@hl1 Hello

wixData.query(" NewMembersCollection ")
.count()
.then( (num) => {
let totalNewMembers= num;

 wixData.query(" **RenewalMembersCollection** ") 
      .count() 
      .then( (num2) => { 

let totalRenewalMembers= num2;
let grandtotal = totalNewMembers + totalRenewalMembers;
$w(“#REDTEXT”).text = grandtotal.toString();
} );
} );

@jeffersonuy It works! thanks very much.

Im glad I could help.

It seem to me that the script can only be used for 2 tables. I tried to have 4 tables
as shown in my script and obviously it did not work. Please see my script

import wixData from ‘wix-data’;

wixData.query(“paymentForm01”)
.count()
.then( (num) => {
let TotalRegularMembers= num;

wixData.query(“paymentForm012”)
.count()
.then( (num2) => {
let TotalSilverMembers= num2;

wixData.query(“paymentForm013”)
.count()
.then( (num3) => {
let TotalPlatinumMembers= num3;

wixData.query(“StudentMembership”)
.count()
.then( (num4) => {
let TotalStudentMembers= num4;

let grandtotal = totalRegularMembers + totalSilverMembers + TotalPlatinumMembers + TotalStudentMembers;
$w(“#REDTEXT”).text = grandtotal.toString();
} );
} );

The error message is Parsing error: Unexpected token
Any help? Thanks

@hl1

please check your “});” syntax some are missing and your variable names are not the same as declared above.

wixData.query(“paymentForm01”)
.count()
.then( (num) => {
let TotalRegularMembers= num;
wixData.query(“paymentForm012”)
.count()
.then( (num2) => {
let TotalSilverMembers= num2;
wixData.query(“paymentForm013”)
.count()
.then( (num3) => {
let TotalPlatinumMembers= num3;
wixData.query(“StudentMembership”)
.count()
.then( (num4) => {
let TotalStudentMembers= num4;
let grandtotal = TotalRegularMembers + TotalSilverMembers + TotalPlatinumMembers + TotalStudentMembers;
$w(“#REDTEXT”).text = grandtotal.toString();
});
});
});
} );

Great! it works. Thanks again for your great help.