Is it possible to see a dataview and a header above only if the query has at least one result?

Hey there,

is it possible, that this complete section (also the header of the table and the heading as well) is hidden whenever there are 0 results?

:slight_smile:

In a pure DQL query that’s not possible, but it’s not very hard to do a little boilerplate dataview around such a query to achieve your goals.

The gist of it would be to do a const result = await dv.query() with the ordinary query as parameter, and then depending on the result do appropriate output.

Do you think you’ll manage from here? There are many examples on the usage of dv.query() in this forum, so try searching a little, and if you can’t make sense of any of those example I could type out something tomorrow…

Hey, hm i tried to find a solution, however, a little more help would be awesome :slight_smile:

Here is an example (with a very basic whimsical query) on how to do this:

```dataviewjs

const result = await dv.query(`
  TABLE file.link as Note, file.name, file.folder
  LIMIT 5
`)

if ( result.successful ) {
  const rv = result.value
  if ( rv.values.length > 0 ) {
    dv.header(2, "The fantastic query results")
    dv.table(rv.headers, rv.values)
  } else {
    dv.paragraph("No values returned from my query")
  }
} else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```

Try this one, and just change the LIMIT 5 to LIMIT 0 for an empty result set. If you don’t like anything output if there is an empty result, you could remove the first else { ... } part with the "No values returned … " text.

Although some of you might not be able to write this variant, the trick about this boilerplate code is that you can (and should) test the query as a standalone query and work out the kinks before you then could insert it into this boilerplate and leave all the code as is (with the exception of adapting the texts :smiley: ).

Another advantage, if you do know a little coding, is that you’re allowed to manipulate the rv.values to your liking before presenting the result later on. Which is commonly(?) used to produce table sums and averages, and so on. It’s also possible to use from within Templater with replacing the dv.table() with something like tR += dv.markdownTable() which in turn would produce a static result (allowing for backlinks and so on) of your query.

1 Like

thanks I will try

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.