I have a note (note name: “test1”) where there are some tasks and also some outgoing links to other notes (“test2”, “test3”, etc). These other notes have some tasks of their own. And I want to see all the tasks that are in notes “test1”, “test2”, and “test3” (and other outgoing links) in the note (“test1”) with dataview. How can I do that?
A small table for example showing what I am doing.
Note Name
test1
test2
test3
…
Tasks
clean house
do dishes
do homework
…
Outgoing Links
test2, test3
-
-
…
Dataview in
here
-
-
…
So the desired output from the dataview is:
clean house
do dishes
do homework
Things I have tried
I used the TASK function with WHERE file.name = string(this.file.outlinks[0]). Here I tried with string() because this.file.outlinks returns an array and it seems file.name only accepts texts. I also understand that the array has many values so I tried seeing if I can get atleast one value converted to text but that didn’t work. The whole command looks like this:
```dataview
TASK
WHERE file.name = string(this.file.outlinks[0])
I tried seeing the output of inline dataview output of string(this.file.outlinks[0]) and it seems that the output is a link file. I think that is an issue? But then again, I feel like this still won’t give me the final answer that I am looking for which is seeing tasks from all the outgoing links. Can someone point me to the right direction?
I checked the task in “test3” as completed and now it shows this huge “Dataview: No results to show for task query” in place of “test3” block. It takes up a lot of space in the note. How can I remove this?
found this forum answer saying there is an option in the Dataview Settings where you can turn off the “Warn on Empty Results” universally for all notes in the vault.
Though I don’t want to do that for all notes. Is there a way I can use dataviewjs to not show the No result warning?
Update (found a way to find length but what I need is to find the length of not-completed tasks only):
So, it seems that what works to find length is: .length at the end of the value part (though would apperciate an explanation as to why that is. I just randomly did it and it worked). Checking the output of .length with an inline dataview js:
But that means it is showing the length of total tasks (i.e. completed and not-completed, together). So the “No result to show task query” still shows for “test3” note.
How can i modify the code to only show length of not-completed tasks?
Right now, my code looks likes:
```dataviewjs
const first = dv.current().file.link;
dv.taskList(dv.page(first).file.tasks
.where(t => !t.completed));
const notes = dv.current().file.outlinks;
let note = 'empty';
for (note of notes) {
if(dv.page(note).file.tasks.length > 0) {
dv.taskList(dv.page(note).file.tasks
.where(t => !t.completed));
}
else {
dv.paragraph("no result")
}
}
```
So to get the length of only not-completed tasks, we have to first write .where(t => !t.completed) and then check the length with .length. So, let’s check that in inline dataviewjs:
I have also changed the first line const first = dv.current().file.link to:
const first = dv.current().file.name
Because .file.link and .file.name are basically the same thing except they are of different data type. .file.link will output a link whereas .file.name will output a string.
Using const will make the variable constant. And so if you change the name of the note, then you will always have to reload the dataviewjs for the code to work. So, change all const to let.
The code looks like this now:
```dataviewjs
let first = dv.current().file.name;
let firstTask = dv.current().file.tasks.where(t => !t.completed)
if(firstTask.length > 0) {
dv.taskList(firstTask);
}
let notes = dv.current().file.outlinks.where(x => !x.path.endsWith("png"));
for (let note of notes) {
if(dv.page(note).file.name != first){
let ntask = dv.page(note).file.tasks.where(t => !t.completed);
if(ntask.length > 0) {
dv.taskList(ntask);
}
}
}
```
Note
added firstTask to prevent the “dataview: no result to show for task query” warning from showing for the first note.
Major update (fixed: the outlinks show duplicates of a note if you have wikilinked those notes twice or more):
If, lets say, you wikilinked a note “test2” twice in your current note, it will show up twice in your outlink output. so to get around that you can use a combination of filter(), findIndex(), and indexOf() functions to get unique elements.
(For an explanation on how the functions works, see here)
with filter() and findIndex() function:
let notes = dv.current().file.outlinks
.filter((value, index, self) => index === self.findIndex(t => t.path === value.path))
.where(x => !x.path.endsWith("png"));
with filter() and indexOf() function:
let notes = dv.current().file.outlinks
.map(x => x.path)
.filter((value, index, self) => index === self.indexOf(value))
.where(x => !x.endsWith("png") && x != dv.current().file.path);
with only indexOf() function:
// getting all outlinks without .png and current note
let notes = dv.current().file.outlinks.where(x => !x.path.endsWith("png") && x.path != dv.current().file.path);
// get only the unique outlinks
let paths = notes.map(x => x.path);
let unique = [];
paths.map(x => (unique.indexOf(x) === -1) && unique.push(x));
Editing and adding these lines will make the code work! Cheerio!
My current dataviewJS Code:
By the way, I further edited the code so now it looks like this:
```dataviewjs
// printing tasks from current note
let first = dv.current().file.name;
let firstTask = dv.current().file.tasks.where(t => !t.completed);
(firstTask.length > 0) && dv.taskList(firstTask);
// get unique outlinks without .png and current note
let notes = dv.current().file.outlinks
.filter((value, index, self) => index === self.findIndex(t => t.path === value.path))
.where(x => !x.path.endsWith("png") && x.path != dv.current().file.path);
// printing tasks from unique outlinks
let ntasks = notes.map(x => dv.page(x).file.tasks.where(t => !t.completed));
ntasks.map(x => x.length > 0 && dv.taskList(x));
```
A little bit of explanation of the changes:
the for loop is replaced with .map() function.
if statement is replaced with ‘and’ operator &&.
using filter() with findIndex().
renaming tasks to ntasks to avoid confusion with .file.tasks