Dataview List Query - Group By and Display Link + Field

Trying to create a glossary of terms used throughout my vault - I am 90% of the way to what I want, just struggling to get a list query to display the information I want.

I want to display as a list the file link and the content of the ‘description’ inline field, grouped by the first letter of the file name with content within the group listed in alphabetical order. I have been able to achieve this so far by filtering the query by first letter of file name and then just repeating the query 26 times for each letter.

LIST description
FROM #type/term 
WHERE file.folder != "00 META/01 TEMPLATES"
AND substring(file.name,0,1) = "e"
SORT file.name ASC

So far have tried:

LIST rows.description
FROM #type/term 
WHERE file.folder != "00 META/01 TEMPLATES"
GROUP BY substring(file.name,0,1)
SORT file.name ASC

image

LIST rows.file.link + rows.description
FROM #type/term 
WHERE file.folder != "00 META/01 TEMPLATES"
GROUP BY substring(file.name,0,1)
SORT file.name ASC

image

LIST rows.file.link + ": " + rows.description
FROM #type/term 
WHERE file.folder != "00 META/01 TEMPLATES"
GROUP BY substring(file.name,0,1)
SORT file.name ASC

image

Is there a way to accomplish this with a single Dataview query, rather than 26 of them? I feel like I’m so close but yet so far haha :sweat_smile:

I have no idea if this would be feasible in DQL but it should be possible with DataviewJS…

Maybe something like this ? :

```dataviewjs
let groups = dv.pages("#type/term")
    .where(p => p.file.folder != "00 META/01 TEMPLATES")
    .sort(p => p.file.name)
    .groupBy(p => p.file.name.substring(0,1).toUpperCase())

for (let group of groups) {
    dv.header(3, group.key);
    dv.list(
       group.rows
       .sort(k => k.file.name)
       .map(k => k.file.link + ": " + k.description)
   )
}
```

I’m no Dataview expert :sweat_smile: so this might not work and/or require some adjustments to look like the result you expect :innocent:

While testing, that query (adapted to my vault) returned something like this:

Do I have any idea what this says? Nope. Does it look pretty much exactly like I want it to? Yup!
Hopefully I don’t break it or want to update it in the future haha but I like this way more than my 26 repeated bits of dataview code :laughing: Thank you!

1 Like

I don’t really have the knowledge to explain what this query does exactly (:sweat_smile:) but I guess I can somewhat say that this first part :

let groups = dv.pages("#type/term")
    .where(p => p.file.folder != "00 META/01 TEMPLATES")
    .sort(p => p.file.name)
    .groupBy(p => p.file.name.substring(0,1).toUpperCase())

… returns a list of all the pages in your vault tagged with #type/term (see dv.pages()) excluding the ones located in your folder "00 META/01 TEMPLATES" sorted by file.name and then grouped by the 1st letter of said file.name transformed into uppercases.

The whole list is stored in a variable called groups :blush: .
This, so far, is very similar to what you were already doing with your DQL queries …

Now, because groups is in fact, underneath, a data array of page objects, one can manipulate further each specific group within groups, which is done in the second part :

for (let group of groups) {
    dv.header(3, group.key);
    dv.list(
       group.rows
       .sort(k => k.file.name)
       .map(k => k.file.link + ": " + k.description)
   )
}

… which kind says:

For each specific group within groups, create a header (dv.header()) of level 3 displaying as text value the group.key (corresponding to A for the notes starting with A or a, etc…) and a list (dv.list()) composed of the group.rows (the group.rows being all the notes starting with A or a for the group A, etc…) where the rows are sorted by file.name and each note within the group (each bullet point) should display: the note’s file.link + ": " + the value of the metadata description.

I have no idea if this makes things a little bit clearer though :sweat_smile: … But I tried :innocent: .

On the other hand, if it breaks or you want to modify it but you’re stuck, you can always come back on the forum and ask :wink: !
Anyone able to help you, will probably do so :grin: !

2 Likes

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