Dataview: link(A, B), causes two "cards" if "A" has two values

Continuing the discussion from Dataview "contains" help:

So I have this Dataview Query for a book gallery:

```dataview
TABLE WITHOUT ID
      file.link AS "FILE",
      Q_FLAG AS "Q_FLAG"
      
FROM "100_Project/02_dataview/Q25_MetaFunction/Q25_test_data02"
WHERE Country

FLATTEN join(map(choice(typeof(Country) = "array", Country, list(Country)), (c) => {
BZ: "🇧🇿",
CA: "🇨🇦",
US: "🇺🇸"

}[meta(c).display])) AS Q_FLAG


SORT file.name ASC

and I thought I’d add something so not only the book title, and the author would be clickable (since the author’s are aliased like this in the book note: "[[King, Stephen|Stephen King]]"), but also the Flag would be clickable, by changing it like so:

TABLE WITHOUT ID
      file.link AS "FILE",
      Q_Country AS "FLAG"      <------------------
      
FROM "100_Project/02_dataview/Q25_MetaFunction/Q25_test_data02"
WHERE Country

FLATTEN join(map(choice(typeof(Country) = "array", Country, list(Country)), (c) => {
BZ: "🇧🇿",
CA: "🇨🇦",
US: "🇺🇸"

}[meta(c).display])) AS Q_FLAG

FLATTEN link(Country, Q_FLAG) AS Q_Country <----------------------

SORT file.name ASC

This actually works! However, sometimes there are books that have two countries, making the Country: key look like this:

Country:
- "[[United Kingdom|GB]]"
- "[[United States|US]]"

This causes TWO cards to be created, one with a link to United Kingdom, one to US:

The links are clickable. But I’d prefer it if the links were clickable only within the respective flag emoji, and there only to be one Card if I’ve only read the book once.
Is this possible?

Thanks!

EDIT: Replacing Country with list(Country) in the last bit, solves one problem but causes another. Now the last line looks like this:

FLATTEN link(list(Country), Q_FLAG) AS Q_Country

this prevents TWO Cards being created, but now the two flags are doubled, one with a link to the UK one to US:

One step forward, one sideways, I guess haha

So basically you’re flattening the Country links to get the flag showing, and now you’re reassembling the Country links? This seems like an idiom in the Nordic countries which translates to something like: Don’t cross the stream to get water.

Let’s do both actions when we’re first fumbling around with the Country field:

```dataview
TABLE WITHOUT ID
  file.link as "File", 
  Q_Country as "Flag"
  
FROM #f54646 
FLATTEN
  join( map( choice(typeof(Country) = "array", Country, list(Country)), 
    (c) => link(
      meta(c).path, 
      { NO: "🇳🇴", US: "🇺🇸", GB: "🇬🇧"}[meta(c).display]
  )), ", ") as Q_Country
```

Which outputs this (when adding cssClass: cards):

Instead of doing a double manoeuvre, of first splitting and then joining, this query does the following:

  • join( map( ... , – We’re going to join a list by mapping up some value(s), so set up the functions in preparations for this to happen
  • ... choice(typeof(Country) = "array", Country, list(Country)) ... – When mapping the countries, we need to ensure that the Country field is an actual list of links, even if only there is one entry in that list
  • (c) => link ( – In the mapping, we’re going to be building a linking which can take two parameters, the link and the display text
    • meta(c).path – For the actual link, we’re just using the original path
    • { NO: "🇳🇴", ... }[meta(c).display] – For the display text, we’re mapping the country code against the list of flags (as before)
  • )), ", ") as Q_Country – Finish of the link, map, before applying ", " as parameter to the join, and aliasing everything as Q_Country

Also note that I’m using the FROM #f54646 to limit my query, which you of course also need to adjust when adapting to your scenario.


Hope this helps, and gets you further on the way to building this library of yours!

1 Like

Incredible! Thank you :slight_smile:

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