Dataview plugin snippet showcase

It’s the difference between a YAML sequence and a map, elsewhere also called array and dictionary.

You can easily debug your YAML using the Online YAML Parser.

Hey thanks for sharing that! I did run it through a validator since my YAML was being generated using a script and since it showed fine, I didn’t bother much further. I checked Dataview’s documentation and couldn’t find an example of something like this, so decided to jump in here. Bookmarking the parser as I’m sure I’ll be needing this again.

Re duration. Does anyone know if it is possible to get the Number used in a duration from a different field? So instead of dur(18 days) it’s dur(“DaysToHarvest” days)?

Is there any possibility to create a database of images?
How to add metadata to images?
In the github page of dataview plugin we can find in the roadmap the ‘future’ feature of “Gallery view (primarily for images)”… This means that is possible to add some metadata to images… Or it will only work with the ‘implicit atttributes’ (file.name, etc.)?

Any ideas how to display subtags (#tag1/tag2) as a single (and hence clickable) tag rather than two tags - one parent of the other tags in dataview tables?

Hooray for Dataview 0.3.0+ and the new dataviewjs code blocks!

To continue Dataview plugin snippet showcase - #218 by Moonbase59 and others, here’s a “family birthday list” as an example, with formatted dates, age calculation and sorting:

// Family birthdays using DataviewJS
let pages = dv.pages("#family").where(p => p.birthday);
dv.table(["Name", "Birthday", "Age", "This year"],
  pages.sort(p => moment(p.birthday.toString()).format("MM-DD"), 'asc')
  .map(p => [
    // The name
    p.file.link,
    // Formatted birthday from YAML frontmatter
    moment(p.birthday.toString()).format("ddd, YYYY-MM-DD"),
    // Current age in years
    moment().diff(moment(p.birthday.toString()), 'years'),
    // This year’s birthday as formatted date
    moment(p.birthday.toString().substring(5, 10), "MM-DD").format("ddd, DD MMMM")]
  )
);

Result:

Since I’m using moment.js, the dates shown are locale-aware and should adapt to whatever language you set Obsidian to.

Happy experimenting!

Docs on @blacksmithgu’s new page: https://blacksmithgu.github.io/obsidian-dataview/docs/intro


EDIT 2021-05-09: This has now been superseded by Upcoming Birthdays (robust & flexible) which is much more robust and YAML-configurable!

6 Likes

Is there a way to put an ellipsis maximum character length limit using CSS in File Name?

Well, you could go for a CSS snippet like the following, that shortens only links to the available column width in Dataview tables:

/*
    dataview-shorten-links.css snippet

    Shortens long filename links in Dataview tables with an ellipsis.
    2021-05-07 Matthias C. Hormann (Moonbase59)
*/
.dataview.table-view-table > tbody > tr > td a {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

Voting for this Dataview issue could maybe also help: Tables: Better column width adjustment depending on content? · Issue #186 · blacksmithgu/obsidian-dataview · GitHub

2 Likes

Do you know if we are able to find the headings in backlinks? I’ve been trying to automate MOCs. Dataview seems to treat [[YouTube Channels#Coding]] the same as [[YouTube Channels#Obsidian]].

If Note A has [[YouTube Channels#Coding]] and Note B has [[YouTube Channels#Obsidan]]. A Dataview table searching for only [[YouTube Channels#Coding]] will show both. I’ve tried different where statements and contain statements.

In the table or list preview, it will display as “YouTube Channels” and then when I hover over it, it will show only the “Coding” section or only the “Obsidian” section.

Thank you very much for your help, it has helped me!

You’re welcome, it was a nice exercise for me, too. :slight_smile:

It’s the same with getting aliases too so [[Youtube|YT]] only returns [[Youtube]] too. I don’t know if the new Dataviewjs exposes some way to get more details from a link, I haven’t played with it enough. I’d suggest raising an issue about this.

1 Like

Anyone knows how to cross table via dataview? I want to create some view and reuse that view. “JOIN TABLE”

Let’s say I created a custom YAML field (called Type) that I used for some notes only.

I want to see on which notes I have filled that YAML field and the value of the field respectively.

I used this:

    ```dataview
    table Type
    sort file.name desc
    ```

but this returns all notes and it’s a huge list (with only a few notes having the field “Type” has a value). I just want to see these notes that the “Type” field has a value.

Any ideas how to setup the query?

I found it:

   ```dataview
    table Type where Type
    sort file.name desc
    ```

Adding “Where Type” means show files where Type field has a value :slight_smile:

2 Likes

With some help from @Moonbase59 and @blacksmithgu, I was able to put together the following dataviewjs table for my daily notes (which are in YYYY-MM-DD format): it displays all the notes that were either created or last modified on the day in question. This gives me a good overview of (much) of what I was up to on that day.

(This is a revision of my earlier note Dataview plugin snippet showcase - #264 by AutonomyGaps)

// default dateformat in case it’s forgotten in front matter
var dateformat = "YYYY-MM-DD";
if (dv.current().dateformat) { dateformat = dv.current().dateformat; }

dv.table(["File", "Last Modified", "Date Created"],
  dv.pages()
  .where(p => p.file.mday.equals(dv.current().file.day) || p.file.cday.equals(dv.current().file.day))
  .sort(p => p.file.mtime, 'asc')
  .map(p => [
    p.file.link,
    moment(p.file.mtime.toString()).format(dateformat),
    moment(p.file.ctime.toString()).format(dateformat),
  ])
);
5 Likes

I thought I would share my book list. I had a similar table in notion, but wanted it in obsidian. Here is what it looks like:

Here is my new book template

# title
---
Cover:: 
Creator:: 
Priority:: 
Purpose::
Referrer::
Status:: To Read
Subtitle:: 

Here is my book index page:

```button
name Add Book
type note(Books\NEWBOOK, split) template
action New Book
```
^button-AddNewBook


```dataview
TABLE Subtitle, ("![coverimg|100](" + Cover + ")") as Cover, Creator, Referrer, Priority, Purpose, Status from "Books"
where file.name != "_Book Index"
SORT file.name
```

I got the idea of using the button plugin to create a new book here: Replicating Notion's tables with a couple of Markdown files and Obsidian plugins

30 Likes

What is the exact syntax (including the dataview part) to make this work?

Also, do we need to change the default date format?

  1. Just put the code shown into a dataviewjs code block (instead of dataview).

  2. No. But there are cases where it is extremely useful.

Be aware that the code shown by @AutonomyGaps will pick up any file in the vault that has something in its title that looks like a date to Dataview. If you don’t want that, you can specify a folder name inside the dv.pages() line, in the form of dv.pages('"Daily Notes"').

Docs on Dataview here: https://blacksmithgu.github.io/obsidian-dataview/docs/intro

1 Like

I am getting this error message:

Evaluation Error: TypeError: Cannot read property ‘isValid’ of undefined
at DateTime.equals (eval at (app://obsidian.md/app.js:1:1153594), :7732:34)
at eval (eval at (eval at (app://obsidian.md/app.js:1:1153594)), :9:27)
at Array.filter ()
at Proxy.where (eval at