DataviewJS Snippet Showcase

Solved by @TfTHacker himself, thank you so much !
For those who would be interested, the solutions are :

let tasks = dv.array(dv.pages().file.tasks)
dv.header(6,tasks[0].["🗝"])

and even the yaml filter thing works,

---
filter : 🗝
---
var filter = dv.current().filter;
let tasks = dv.array(dv.pages().file.tasks)
dv.header(6,tasks[0].[filter])
4 Likes

Generally this works great but for some reason, when I make a new note today I get this error (but it does not happen in the notes I created for all days earlier in this week …):

Evaluation Error: TypeError: Cannot read properties of undefined (reading 'file')
    at eval (eval at <anonymous> (eval at <anonymous> (app://obsidian.md/app.js:1:1444405)), <anonymous>:7:36)
    at DataviewInlineApi.eval (eval at <anonymous> (app://obsidian.md/app.js:1:1444405), <anonymous>:15671:16)
    at evalInContext (eval at <anonymous> (app://obsidian.md/app.js:1:1444405), <anonymous>:15672:7)
    at asyncEvalInContext (eval at <anonymous> (app://obsidian.md/app.js:1:1444405), <anonymous>:15682:32)
    at DataviewJSRenderer.render (eval at <anonymous> (app://obsidian.md/app.js:1:1444405), <anonymous>:16267:19)
    at DataviewRefreshableRenderer.maybeRefresh (eval at <anonymous> (app://obsidian.md/app.js:1:1444405), <anonymous>:15995:26)
    at HTMLDivElement.i (app://obsidian.md/app.js:1:138855)

Oh, but of course a shutdown/re-open of Obsidian fixes this.

:thinking:

Just ignore the above error posting then …

Would love to have this snippet!

1 Like

I have not been able to get inline image to work with Blue Topaz theme. Do we have to specify anything to render images inline?

Hello everyone!

I created a dictionary that pulls all “terms” and “definitions” from my vault, but when there are more than one term and definition, they are all placed in the same table cell.

Here is my code

const pages = dv.pages().where(b => b.term)
dv.table(['Term','definition'],pages
		 .sort(a => a.term,'asc')
		 .map(a => [a.term,a.definition]))

and here is the output:

How can I break up that first row into individual rows?

7 Likes

I have this same question - it’s essentially like “flatten” in regular dataview, but I can’t for the life of me figure out how to do it into dataviewjs.

1 Like

Could you provide a small data example? I’m rather new to dataview (and Obsidian) and unaware how you could get a ‘term’ property on your pages and how the resulting data structure look like :x

General speaking, dv.pages() return a data array that again has a function flatMap. Maybe you get the wanted result when you use flatMap instead of map?

Also, for debugging purposes, it can be helpful if you assign pages.sort(a => a.term,'asc').map(a => [a.term,a.definition]) to a variable and then add a console.log(variableName) to your query. Hitting shift ctrl I in Obsidian opens the developer console whereas you’ll see your console.log call.
This way you can investigate how your resulting table data looks like and how the data differs when you get a single row per term/definion or one row with multiple term/definitions.

It could look like this then:

const pages = dv.pages().where(b => b.term)
const tableData = pages
		 .sort(a => a.term,'asc')
		 .map(a => [a.term,a.definition]);
console.log('tableData ->', tableData)
dv.table(['Term','definition'], tableData)

Hope that helps! :slight_smile:

1 Like

Hi! I made a DataviewJS snippet that automatically adds people’s birthdays in your daily notes upon creation, if it exists, like so:

It uses the file’s alias, but I added an option in the code to use the file name instead.

Credit goes to @Moonbase59’s code here, I only added small modifications, hope that’s okay!

Here’s the gist with setup and instructions:

6 Likes

This is very useful – thank you for sharing it. I already have a People folder, and in it the individuals have birthDate: in the yaml. Which bits of your template should I change to accommodate this?

Please tell me how can I learn about the obsidian dataviewjs plugin from the beginning. I am from a non-coding background and don’t know javascript. But I am interested in learning this plugin.

If you haven’t already read them, the docs are a great place to start: Dataview

Hi Saari,

Here is a scenario. I am reading a textbook and want to write down a definition. I write:

term:: "Name of Term"
definition::"Definition of Term"

In some notes I have multiple terms and definitions, and that is when they all seem to be placed in the same entry.

Thank you for showing me how to debug.

1 Like

Change all instances in the code from p.birthday to p.birthDate and it should work.

1 Like

YAS! ^this, plez

+1

Hey! Trying to figure out how to do a combination of:

  • Display taskLists with groupByFile = true
  • Group the files of the taskLists by a custom YAML field
  • Filter out files of the taskLists by a custom YAML field

What I thought would work is:

for (let group of dv.pages().where(p => p.category.contains("project") && p.status.contains("active")).groupBy(p => p.kr)) {
	dv.header(3, group.key);
	dv.taskList(group.rows
	.file
	.tasks
	.sort(t => t.completed))
	;
}

It only works if I put something inside dv.pages(), and limit the contains() statement to a single filter, e.g. only .where(p => p.status.contains("active")). The thing is, I don’t have tags on the pages I’m filtering on (only YAML fields), and I’d prefer to filter with contains() statements on YAML than by adding tags.

My files I’m intending to query here look something like this

---
category: project
status: active
kr: [[Fly to mars]]
---
# Project
- [ ] Task

Any suggestions for a fix?

Btw my error message is “Evaluation Error: TypeError: Cannot read property ‘contains’ of undefined”

1 Like

Hi, does anyone know how to use taskList, but have it not include sub-bullet-points located directly underneath the task? For example, I have tasks that are structured like this:

- [ ] Actual task that I want to appear in taskList
    - Example contextual note I included as sub-bullets
    - Note that these are not tasks / sub-tasks since they lack leading brackets
    - However, taskList still displays them and makes the view too busy

I’ve noticed that those sub-bullet records are stored in the data array with a value for real equaling ‘false’. So I tried to filter for only real == true in the where section, but no luck.

dv.taskList (dv.pages('#tag').file.tasks.where(t => !t.completed && t.real)

Its called “Heatmap Calendar”, search for it on the community plugin list in obsidian

Hi,

I got this code from Eleanor Konik.


//get all md files in vault

const files = app.vault.getMarkdownFiles()

//create an array with the filename and lines that include the desired tag

let arr = files.map(async(file) => {

const content = await app.vault.cachedRead(file)

//turn all the content into an array

let lines = await content.split("\n").filter(line => line.includes("- [?]"))

return ["[["+file.name.split(".")[0]+"]]", lines]

})

//resolve the promises and build the table

Promise.all(arr).then(values => {

console.log(values)

//filter out files without "ad-question" and the note with the dataview script

const exists = values.filter(value => value[1][0] && value[0] != "[[dataviewjs-testing]]")

dv.table(["file", "lines"], exists)

})

I am trying to customise it to show me results only from files tagged #learning. So far no luck. I unsuccessfully tried using dv.pages("#learning").

Can somebody help me, please?

Thanks

1 Like