DataviewJS Snippet Showcase

Supergenial! Works like a charm - thanks a lot! :heart_eyes:
Now I enjoy my new TOC a lot. Way back, in Notion, I used a similar way to sort notes by date of modification. It just helps me to keep an eye on rarely used files.

1 Like

wow thank you so much for this, itā€™s exactly what Iā€™m looking for. Though Iā€™m not quite sure how to implement. Iā€™ve got dataview installed (have been using it for leaflet maps).

Can I have this function just look at a folder instead of using a searchterm? Or at least how do I identify a note as being ā€˜Peopleā€™ so that it will show up on the birthday list? Also, where do I put the dataviewjs code?

thanks so much :slight_smile:

Happy you like it!

For searchterm:, you can use all valid Dataview search terms. In this case, it looks in the folder ā€œPeopleā€ and its subfolders.

This DataviewJS code usually goes in an extra note, like ā€œUpcoming Birthdaysā€, but you could it include elsewhere of course.

HTH

2 Likes

Thank you so much for this script! Itā€™s making it much easier for me to set up my hotkeys and keeping them on the side as I figure out my workflow.

Is there an easy way to omit any of the commands not assigned with any hotkeys? (get rid of all the rows containing ā€œ-ā€?)

I donā€™t know any javascript. Managed to figure out some basics like swapping out Mac commands and removing the command ID column, but struggling to find a solution for the above.

Thanks for your kind words, appreciate it! You can use the ā€œCommands sorted by assigned hotkeyā€ part alone, it will only show the commands with hotkeys, not the complete command list. Or put the two parts in two notes :wink:

1 Like

Hey everyone! I just wanted to share that my plugin CustomJS is now available on the community store. It allows you to write and reuse javascript across Obsidian, and works on desktop and mobile. It pairs great with Dataview, which is my primary use case. See the Advanced Example section of the README for how Iā€™m using it with dataview to manage my tasks.

5 Likes

Hello guys, this is my snippet, its finds all my tasks from my notes on my work folder.

but i like to ask you a little help, because i dont know how to order the result by file name.

If any of you could help me i will be very gratefull.

`dv.taskList(dv.pages('"Notes/Work"').file.tasks
.where(t => !t.completed))`
1 Like

You need to sort before checking the tasks.

Personally, I like to sort on full path:

```dataviewjs
dv.taskList(dv.pages('"Notes/Work"')
.sort(p => p.file.path, 'asc')
.file.tasks
.where(t => !t.completed))
```

although the list can be a little confusing with tasks, because only the file name is shown but it is still sorted on full path name (including folder names).

If you want to sort on file name only, you can substitute the above sort line with:

.sort(p => p.file.name, 'asc')
3 Likes

Thanks @Moonbase59, helped me alot, works perfectly.

I never understand these parameters like p =>, t =>. I donā€™t found nothing on documentation, i know that t => is for tasks, and p => is for path?
What other parameters like those exist?

4 Likes

Those are not specific to dataview or Obsidian, those are javascript arrow functions. The parameter can be named anything you want, you could use banana => if you wanted to :grin:

2 Likes

lazy me, Iā€™d still use b => instead of banana => :rofl:

Hereā€™s a little explanation on the MDN for those who donā€™t use these often.

2 Likes

thank you again, this will help me to create my codes, i am not a programmer, sometimes itā€™s seems to be a little confusing to me.

I want to create a dataview to show me the notes that has mutual tags as my current file. The code below works for a note with two tags. But there are a few things that I canā€™t achieve:

  1. I do not want to see all the tags in the Tags column; I only want to see the mutual tags there.
  2. I want to have a generic code that can be applied to every note whether the note has one tag or two tags or 10 tags.
table Tags
Where contains(file.tags, this.file.tags[0]) or contains(file.tags, this.file.tags[1])
sort tags

And so I decided to venture into dataviewjs.

You guys are awesome and dataviewjsā€™ possibilities are enormous. I read through every single post here and tried so hard but I still couldnā€™t find out how to do what I want. Anyone is able to help me out? Thanks in advance!

1 Like

I did this script that can sort columns in dataviewJS by clicking on them.
Hope that help!

// Render the table
dv.table(["Paper","Title", "Year", "šŸ“š", "āœ’ļø","šŸ§ ","Updated"], dv.pages("#LiteratureNote")
	.where(b=>b.file.folder == ("Zettelkasten"))
    .sort(b => b.file.mtime, 'desc')
    .map(b => [
	"[[" + b.file.name + "|" + (b.file.aliases[1] || b.file.aliases[0]) + "]]", 
	
b.file.aliases[0],
b.Year,
b.completion_reading,
b.completion_note,
b.paper_understanding,
b.file.mtime.toFormat('yyyy-LL-dd T')
]))
	
// Sort Script	
	
// Source https://stackoverflow.com/questions/14267781/sorting-html-table-with-javascript
const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;

const comparer = (idx, asc) => (a, b) => ((v1, v2) => 
    v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2,undefined, {numeric: true})
    )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));

// do the work...
document.querySelectorAll('th').forEach(th => th.style.cursor = "pointer");

document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => {
  const table = th.closest('table');
  const tbody = table.querySelector('tbody');
  Array.from(tbody.querySelectorAll('tr'))
    .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
    .forEach(tr => tbody.appendChild(tr) );
})));

13 Likes

What can I say? I have inserted the code into my table code. And: it works just like that! This is a really great thing, finally I have an overview of my very long tables. Thank you!

I created a small snippet to filter tasks on a page based on inclusion/exclusion of multiple tags. Itā€™s pretty generic, so I thought I might share it. :slight_smile:

%%
include:: tag1
exclude:: tag2
onlyUncompleted:: false
%%

```dataviewjs
const source = dv.current();
const include = source.include;
const exclude = source.exclude;
const onlyUncompleted = source.onlyUncompleted;
let tasks = source.file.tasks;

// include
if (include != undefined) {
    let includeTags = include.replace(/[,#]/g,"").split(" ");
    let includedTasks = [];
    includeTags.forEach (tag => {
        let filteredTasks = tasks.filter (t => t.text.includes ("#" + tag));
        includedTasks.push (...filteredTasks);
    });
    tasks = includedTasks;
}

// exclude
if (exclude != undefined) {
    let excludeTags = exclude.replace(/[,#]/g,"").split(" ");
    excludeTags.forEach (tag => {
        tasks = tasks.filter (t => !t.text.includes ("#" + tag));
    });
}

//completed
if (onlyUncompleted) tasks = tasks.filter (t => !t.completed)

dv.taskList(tasks, false);
```
6 Likes

Still trying to get the hang of DataView and DataViewJS is over my head. However, I am struggling to create a simple query of ā€œIncomplete Tasks in the current noteā€

I want to be able to identify all the tasks in one place that exist in the note I have open - no hardcoded values - just this note.

If this give me all incomplete tasks in all notes:

dv.taskList(dv.pages().file.tasks.where(t => !t.completed))

How do I get only the incomplete tasks in the current note?

1 Like

you can access the current file with dv.current() so just replace dv.pages() with dv.current().

1 Like

Thank you. However, with:

\```dataviewjs
dv.taskList(dv.current().file.tasks.where(t => !t.completed));
```\

I get an error:

Evaluation Error: TypeError: dv.current(...).file.tasks.where is not a function
    at eval (eval at <anonymous> (eval at <anonymous> (app://obsidian.md/app.js:1:1215379)), <anonymous>:1:75)
    at DataviewInlineApi.eval (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:12599:16)
    at evalInContext (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:12600:7)
    at eval (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:12611:36)
    at Generator.next (<anonymous>)
    at eval (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:26:71)
    at new Promise (<anonymous>)
    at __awaiter (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:22:12)
    at asyncEvalInContext (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:12606:12)
    at DataviewJSRenderer.eval (eval at <anonymous> (app://obsidian.md/app.js:1:1215379), <anonymous>:13133:23)

for some reason, where does not seem to work anymore, but the equivalent filter does:

dv.taskList(dv.current().file.tasks.filter(t => !t.completed));
2 Likes