Suggestion: how many answers in searchBar?

Hi.
Unless I missed it in the docs, there is no way to know how many answers are returned by a searchBar. So here is a quick example as solution. It would be helpfull to be added as a new propertie (for example searchCount).

var searchbar = app.searchbar.create({
		el: '.searchbar',
		searchContainer: '.list',
		searchIn: '.item-title',
		on: {
			search: function () {
				var all = this.searchContainer.querySelectorAll(this.params.searchIn).length;
				var hidden = this.searchContainer.querySelectorAll(".hidden-by-searchbar").length;
				var searchCount = all - hidden;
				console.log("there are "+searchCount+" answers");
			}
		}
	});

Default value (before any search) should be initialized to this.searchContainer.querySelectorAll(this.params.searchIn).length;

I stumbled upon this while searching for another question.

For anyone looking for this.

Don’t know why It’s not documented, but the on search callback passes the following 4 parameters (only 3 are documented), the fourth being the list of founditems’ doms (in case it is not a custom search and not on a virtual list):

var searchbar = app.searchbar.create({
  el: '.searchbar',
  searchContainer: '.list',
  searchIn: '.item-title',
  on: {
    search: function (searchbar, query, previousQuery, foundItems) {
      console.log("there are " + foundItems.length + " answers");
    }
  }
});

Hope it helps someone,

Regards,

1 Like

Hello.
I have read the doc and noticed only 3 parameters as you mentionned. I have just tested your clue about the 4th parameter, and it works.
Maybe this has be added recently and the doc has not been updated yet.
Thank you.

EDIT : I have browsed the codes of previous versions. This was added in version 8.2, line 27326.

1 Like