Dataview plugin snippet showcase

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

Hi, this is great.
Iā€™m trying to do a similar database but for research purposes.
In the image below, a database of images.

I tried to add the image-preview in the table using the HTML format in the frontmatter or in inline field (something like <img src="file:///Users/......"> ) and it worksā€¦ But the table turns unstable and when it does an automatic refresh the table presents some glitches (a kind of ā€˜shakesā€™, as a graphical issue).
Now Iā€™ll try to use your format ("![coverimg|100](" + Cover + ")") and verify if the graphical issues gone away. One question: what kind of link you put in ā€œcoverā€ property?

1 Like

Didnā€™t try @AutonomyGapsā€™ new code, but let me check in my vault. What version of Dataview are you using, and which version of Obsidian?

EDIT: RESULTS

I found that I got the same error as you, had to leave the note and reopen it a few times and wait some minutes until it worked. The error occurs when file.day canā€™t be populated (because the file has no recognized date in the title, and no date: YYYY-MM-DD frontmatter field).

I have this now, and it works (the file is also called 2021-05-23 Test DataviewJS AutonomyGaps, to provide a date to file.day):

# 2021-05-23 Test DataviewJS AutonomyGaps

file.day: `=this.file.day`

file.mday: `=this.file.mday`

file.cday `=this.file.cday`


```dataviewjs
// 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),
  ])
);
ā€‹```

(Note the last line ``` is faked for the forumā€”remove it if copying, and use real 3 backticks.)

Result:

1 Like

all the latest versions :man_shrugging:

Saying ā€œlatestā€ is always bad if people come here months after ā€¦ using numbers is preferred. :wink:

Btw, I used Obsidian 0.12.3 and Dataview 0.3.9 for this test.

2 Likes

@yalcin @AutonomyGaps It seems Dataview sometimes doesnā€™t ā€œseeā€ the latest, probably due to Obsidianā€™s rather effective caching. In my case, I sometimes had to wait a few minutes, or close & reopen Obsidian until all was seen again (especially when renaming the note that has the code).

To make it more user-friendly, even if the current note doesnā€™t have a valid file.day, you could change the code like this:

// 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 => dv.current().file.day && (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),
  ])
);

Study the where clause: We only do it if file.day is valid.

On a note that has no date, you would now of course also get no results. But also no error message. (I removed the date from the noteā€™s title for this test):

3 Likes

I just use a regular link in the cover property. I copy the image URL from Amazon and stick it in there.

2 Likes

Thanks so much for your fine-tuning of this!

1 Like

Thanks. Regarding the mentioned graphic issues, this seems to indicate that there is a difference between links to local files and URL links.

I have a simple table to collect my journal entries related to a given book. Iā€™m trying to have one of the columns show a link to the heading where I wrote the entry:

table Chapters, file.link+"#Reading:: The Lost Symbol" as Link
from #my/journal where Reading="The Lost Symbol"
sort file.ctime asc

And it results in this
image

Instead of resulting in a [[2021-05-23#Reading The Lost Symbol]] link, it gives me a separate text ([[2021-05-23]] reading ā€¦)

Iā€™m not very literate in codes, but Iā€™ve been studying the documentation to see if thereā€™s a way to achieve this. Do you guys know if itā€™s even possible?

Nevermind: found the answer here: Dataview plugin snippet showcase - #158 by jyrodgers

I want to do this (extract data from a field for use in the query) as well.
Context: I want to make a query that shows people I havenā€™t contacted in a defined amount of time. The people have a field ā€˜desired contact frequencyā€™ and in it is ā€˜2 weeksā€™ or ā€˜2 monthsā€™, whatever. The query:

TABLE 
context, file.mday AS "Last Contact"
FROM "3 - Resources/People" 
WHERE desired-contact-frequency AND file.cday <= date(now) - dur(1 week) 
SORT file.mday ASC

^ Works

\/ Doesnā€™t work:

TABLE 
context, file.mday AS "Last Contact"
FROM "3 - Resources/People" 
WHERE desired-contact-frequency AND file.cday <= date(now) - dur(desired-contact-frequency) 
SORT file.mday ASC

Thereā€™s probably a way to do this in the js version, but 1) Iā€™m not capable of that and 2) it seems like if thereā€™s a way to access ā€œdesired-contact-frequencyā€ this is a much simpler format.

Instead of this, could you try,

"[[" + file.name + "#Reading:: The Lost Symbol]]"

You can even add alias to the link if it gets long like,

"[[" + file.name + "#Reading:: The Lost Symbol|The Lost Symbol]]"

4 Likes

Oh! It worked. Thatā€™s amazing, thanks a bunch, Rishi. :smile:

1 Like

Hereā€™s a snippet I use to make lists render as comma separated items instead of bullet points:

dv.table(
    ['File', 'Connections'],
    dv.pages('[[Foo]]').map((page) => {
        return [
            `[[${page.file.name}]]`,
            page.file.outlinks
                .reduce((acc, outlink) => {
                    acc += `[[${outlink.path.replace(/\.md$/, '')}]], `;
                    return acc;
                }, '')
                .trim()
                .replace(/,$/, '')
        ];
    })
);

Now this is very close to:

Table file.outlinks as Connections
From [[Foo]]

Except that the list renders differently. Obviously that is a lot of code to write just for a format change, but I like how the table looks quite a bit more with that.

Hereā€™s a slightly cleaner version with .map():

dv.table(
    ['File', 'Connections'],
    dv.pages('[[Foo]]').map((page) => {
        return [
            `[[${page.file.name}]]`,
            page.file.outlinks
                .map((link) => {
                    return `[[${link.path.replace(/\.md$/, '')}]]`;
                })
                .join(', ')
        ];
    })
);

But all are superseded by @Rishiā€™s suggestion of the undocumented join():

8 Likes

This should make them identical,

Table join(file.outlinks, ", ") as Connections
From [[Foo]]

This function needs to be added to the docs still but you can find the relevant discussion on Github.

4 Likes

Awesome!

Hi everyone!

Iā€™d like to implement a GAO (Got Aware Of) tracking system.

For any book or website, one note containing several records of type

GAO:
  When: [[2021-06-02]]
  What: [[Topic-name]]

Then, in each daily note, something like

```dataview
LIST GAO.What
FROM "Resources"
WHERE GAO.When = this.file.name

But this, unfortunately, doesnā€™t workā€¦

Is this possible somehow? Any idea?

Thanks in advance.

ā€“
silvio

Could you try,

WHERE GAO.When = this.file.link