How to count how many rows there are in a table??

Hello, so I have a table which is connected to a dataset. The user of my site can search through the table in order to narrow the information displayed. I am putting a filter on the dataset in order to achieve this. I would like to have a text box which says “3 results found” for example or whatever number of results are returned but can’t figure out how to do so. Thank you very much

Since your table is connected to a dataset, you can use the dataset’s getTotalCount() function which returns the number of items in the dataset that match its filter criteria.

@yisrael-wix I have tried that but It has only returned the total number of items in the dataset

You’re “putting a filter on the dataset in order to achieve this”. Isn’t the total number of items in the dataset the items that match the filter criteria?

@yisrael-wix here is my code:

let type = $w(‘#dropdown1’).value;
let val = $w(‘#input1’).value;

$w(‘#dataset2’).setFilter(wixData.filter().contains(type, val));

let size = $w(‘#dataset2’).getTotalCount();
$w(‘#text12’).show();
$w(‘#text12’).text = size + " results found";

1 Like

@spencersolomon6 , note that setFilter returns a Promise . Your code should look something like this:

let type = $w('#dropdown1').value;
let val = $w('#input1').value;

$w('#dataset2').setFilter(wixData.filter()
  
    .contains(type, val)
)
  .then(() => {
    // the Promise is fulfilled and the dataset is filtered
    let size = $w('#dataset2').getTotalCount();    
    $w('#text12').show();    
    $w('#text12').text = size + " results found";
  })
  .catch((err) => {   
    console.log(err);
  });

As is shown in the code above, you need to wait for the setFilter Promise to be fulfilled before you can do getTotalCount. Disclaimer: the above code is illustrative and not tested.

Good luck,

Yisrael

3 Likes

Hi Yisrael,
How do I get the total Count as in above when I don’t have a promise on that dataset as it is driven by another dataset, using a sameas filter. That driving dataset is a reference collection derived dataset.
I ntoiced i can do this if i stick it in an event or a button and run it after the #dataset2 has been completely updated.

$w('#dataset2').getTotalCount(); 

 
1 Like

Where i put the code? How i show in my page the result of coding?
Tx