DataviewJS Snippet Showcase

Hi DataviewJs Team,
I have a quick question:
When I use the “query” I get the paragraph that I put the #highlight tag on.

#highlight

But when I use “dataview” How can I get the paragraph that I put the #highlight tag on? Of course, all I get is the file name NOT the paragraph text I put the tag on. Thank you for any help you can provide:

table   
from #highlight 

Just another user here, but as far as I know dataview (or dataviewjs) will not give you that because it does not index entire files, just metadata.
Exception: if your “paragraph” is actually a list item or a task, you might be able to get the text.
But otherwise, stick with the built-in Query blocks?

Thank you @scholarInTraining I very much appreciate your message. The problem with using the “Query block” is that it shows every time I have the “Query block”. So I end up with blank paragraphs for every day. :cry: How can I stop these blank listings?

1 Like

I see if I use (tag:#highlight) it works perfectly. Thank you for your reply @scholarInTraining

I didn’t know that there is already “dv.current()”.

By replacingdv.pages() with dv.current() ,
I can get the dataview data for the inline fields in the current file.

4 Likes

A very weird behavior appears when a task-text have some number in front of a due date.

  • [ ] 21 :spiral_calendar:2022-08-31
    goes to ‘overdue’.

On the other hand,

  • [ ] 22 :spiral_calendar:2022-08-31
    goes to ‘upcoming’.

From my test, only tasks including a number between 22 and 68 go to ‘upcoming’. (Today is 2022-08-10)

For example,

  • [ ] abc 22 :spiral_calendar:2022-08-31 → goes to upcoming.
  • [ ] abc 69 :spiral_calendar:2022-08-31 → goes to overdue.

Is there anyone who can give me some clue? Without any number in a task-text, everything is O.K.

Hi, do you have perhaps an updated version for [the new version](DataviewJS Snippet Showcase - #10 by Rishi! ?)

I tried it myself but was not able to get it to work.

Topic

Summary

1.How to display uncompleted sub-tasks which are unscheduled or upcoming?


Test

Summary
  • dataview: v0.5.41

Input

Summary

dictionary files

  • filename : dic_20100301
---
Date: 2010-03-01
---
#Project/P03

- [ ] pp : test
- [ ] pp :spiral_calendar:2022-08-13
- [ ] 21 :spiral_calendar:2022-08-14
- [ ] 22 :spiral_calendar:2022-08-15
- [ ] abc 22 :spiral_calendar:2022-08-16
- [x] abc 69 :spiral_calendar:2022-08-17
    - [ ] abc 69.1 :spiral_calendar:2022-08-17
- [x] abc 69 :spiral_calendar:2022-08-18



  • filename : dic_20100401
---
Date: 2010-04-01
---
#Project/P03

- [ ] KK : test
- [ ] KK :spiral_calendar:2023-08-13
- [ ] 21 :spiral_calendar:2023-08-14
- [ ] 22 :spiral_calendar:2023-08-15
- [ ] abc 22 :spiral_calendar:2023-08-16
- [x] abc 69 :spiral_calendar:2023-08-17
    - [ ] abc 69.1 :spiral_calendar:2023-08-17
- [x] abc 69 :spiral_calendar:2023-08-18




DVJS10_use_fTasks_to_display_unscheduled_or_upcoming_tasks

Summary

Main DVJS

Code Name Data type Group By Purposes Remark
DVJS10_use_fTasks_display
_unscheduled_or_upcoming_tasks
file.tasks no 1.To hide completed sub-tasks

2.To show uncompleted sub-tasks which are unscheduled or upcoming
1.b_unscheduled = true : when task.text contains no due date string like “yyyy-MM-dd” at the end of the line.

2.b_upcoming = true : when task.text contains a due date string like “yyyy-MM-dd” at the end of the line and today is less than it.

Notes

Summary_notes

code DVJS10_use_fTasks_to_display_unscheduled_or_upcoming_tasks

Summary_code
title: DVJS10_use_fTasks_to_display_unscheduled_or_upcoming_tasks => 1.To hide completed sub-tasks  2.To show uncompleted sub-tasks which are unscheduled or upcoming 3.b_unscheduled = true : when task.text contains no due date string like "yyyy-MM-dd" at the end of the line. 4.b_upcoming = true : when task.text contains a due date string like "yyyy-MM-dd" at the end of the line and today is less than it.
collapse: close
icon: 
color: 
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"100_Project/01_dataviewjs/01_by_example/Q15_Tasks/Q15_test_data" and #Project')
    .where((page) => dv.func.contains(page.file.name, "dic_"));    


// M21. define tasks:
// #####################################################################
let tasks = pages.file.tasks
    .where((t) => !t.completed)
    .where((t) => show_unscheduled_or_upcoming(t));


// M51. output tasks:
// #####################################################################
dv.taskList(tasks);


// M90. define function: show unscheduled or upcoming tasks
// #####################################################################
// case_10: return true if b_unscheduled;
// case_20: return true if !b_unscheduled && dt_today < dt_due_date_in_text;
function show_unscheduled_or_upcoming(task) {
    // F13. define dt_today:
    // #####################################################################
    // let dt_today = dv.date("2022-08-18");//=>For debugging
    let dt_today = dv.date("today");

    // F21. define a_date_strings: get a_date_strings from the end of t.text
    // #####################################################################
    let a_date_strings = task.text.match(/\d{4}-\d{2}-\d{2}$/); //=>null or array

    // F25. define b_unscheduled:
    // false: when the end of task.text is /\d{4}-\d{2}-\d{2}$/
    // #####################################################################
    let b_unscheduled = true;
    if (a_date_strings) {
        b_unscheduled = false;
    }

    // F29. case_10: return true if b_unscheduled;
    // #####################################################################
    if (b_unscheduled) {
        return true;
    }

    // F31. define dt_due_date_in_text:
    // #####################################################################
    let dt_due_date_in_text;
    if (!b_unscheduled) {
        dt_due_date_in_text = dv.date(a_date_strings[0]);
    }

    // F41. define b_upcoming: 
    // case_20: true if !b_unscheduled && dt_today < dt_due_date_in_text;
    // #####################################################################
    let b_upcoming = false;
    if (!b_unscheduled && dt_today < dt_due_date_in_text) {
        b_upcoming = true;
    }

    // F80. return :
    // #####################################################################
    return b_upcoming;
}

```

Screenshots(DVJS10): (Today is 2022-08-18)


Unresolved links

The code was copied from Show unresolved links. All I did, was to improve the way the code ignore files on the search. Instead of type each file, all you have todo is to inform the folderPath using tags.

//how many links a non existing file should have at minimum
const count = 1;

//specify the full path here.
const filesPath = []
filesPath.push(
    ...dv.pagePaths("#dailynotes"),
    ...dv.pagePaths("#html"),
    ...dv.pagePaths("#helpers")
)

let ignoredExisting = []
for (let i = 0; i < filesPath.length; i++) {
    ignoredExisting+= '"' + filesPath[i] + '"' + ','
}

//keep these in lower case.
const ignoredNonExisiting = ["your non exisiting notes", "here is note that does not exist"];

let d = {};
function process(k, v) {
  Object.keys(v).forEach(function (x) {
    if(!ignoredNonExisiting.includes(x.toLowerCase())) {
        x = dv.fileLink(x);
        if (d[x]==undefined) { d[x] = []; }
        if(!ignoredExisting.includes(k)) {
            d[x].push(dv.fileLink(k));
        }
    }
  });
}

Object.entries(dv.app.metadataCache.unresolvedLinks)
    .filter(([k,v]) => Object.keys(v).length)
    .forEach(([k,v]) => process(k, v));
    
dv.table(["Non existing notes", "Linked from"],
         Object.entries(d)
         .filter(([k, v]) => v.length >= count)
	     .sort((a, b) => b[1].length - a[1].length)
         .map(([k,v]) => [k, v.join(" • ")]));
3 Likes

Hi there! Is it possible to group by a column on a already grouped query?

I have this code:

dataviewjs  
let pages = dv.pages('#Cainita') .where(p => !p.file.name.includes("Template"))

let groups = pages.groupBy(p => p.residencia);

for (let group of groups){
	dv.header(2, group.key);
	dv.table(["Clan", "Cainita", "Generacion", "Status"],
		group.rows
			.sort(b => b.clan, 'asc')
			.map(b => [b.clan, b.file.link, b.generacion, b.status])
			
	)
}

which gives me this:

That is pretty ok for my zero knowledge of JS… but I would really like to have the rows on each “table” group also by “Clan”.
Something more like this, where there is only one row for each different Clan:

After hours of looking and trial and error I am about to gave up, so I’m looking for some help here first! :raised_hands:.

Thanks!

3 Likes

Topic

Summary

1.How to group by a column?
2.How to group by a column on a already grouped query?


Test

Summary
  • dataview: v0.5.41

Input

Summary

dictionary files

  • location: “999_Test/Q16_test_data/0[1-7]”

  • filename : dic_20080101
---
Date: 2008-01-01
---
Residencia:: BUENOS AIRES
Clan:: Malkavian
Cainita:: Raguela
Generacion:: 9
Status:: Prominente


  • filename : dic_20080201
---
Date: 2008-02-01
---
Residencia:: BUENOS AIRES
Clan::  Tremere
Cainita:: Liandra Vespa
Generacion:: 10
Status:: Regente


  • filename : dic_20080301
---
Date: 2008-03-01
---
Residencia:: BUENOS AIRES
Clan:: Ventrue
Cainita:: Vortex
Generacion:: 6
Status:: En Desgracia


  • filename : dic_20080401
---
Date: 2008-04-01
---
Residencia:: BUENOS AIRES
Clan:: Ventrue
Cainita:: Maurtice Durant
Generacion:: 10
Status:: Neonato


  • filename : dic_20080501
---
Date: 2008-05-01
---
Residencia:: PARIS
Clan:: Toreador
Cainita:: Luis Bryon
Generacion:: 5
Status:: Principe


  • filename : dic_20080601
---
Date: 2008-06-01
---
Residencia:: PARIS
Clan:: Toreador
Cainita:: Chevalier d'Eglantine
Generacion:: 6
Status:: Lider Sheriff


  • filename : dic_20080701
---
Date: 2008-07-01
---
Residencia:: USA
Clan:: Toreador
Cainita:: Helena
Generacion:: 4
Status:: Incognita



DVJS10_groupBy_Residencia_and_TABLE

Summary

Main DVJS

Code Name Data type Group By Purposes Remark
DVJS10_groupBy_Residencia_and_TABLE string yes 1.To groupBy page.Residencia AS G1 (G1=group.rows)

2.To display each G1 as a table

Notes:

Summary
// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"999_Test/Q16_test_data"')
    .where(
        (page) =>
            page.file.name === "dic_20080301" ||
            page.file.name === "dic_20080401"
    );


// M21. define groups:
// To groupBy page.Residencia AS G1 (G1=group.rows)
// #####################################################################
let groups = pages.groupBy((page) => page.Residencia);

    
// M30. output groups: formatted by Prettier - Code formatter v9.5.0 in VScode
// #####################################################################
//dv.span("The following is the content of the `groups`.\n");
dv.span(JSON.stringify(groups, null, 2), "\n");

  • Here is a slice of the groups , where page.file.name is dic_20080301 or dic_20080401.
let slice_groups = {
    values: [
        {
            key: "BUENOS AIRES",
            rows: {
                values: [
                    {
                        file: {
                            path: "999_Test/Q16_test_data/04/dic_20080401.md",
                            folder: "999_Test/Q16_test_data/04",
                            name: "dic_20080401",
                            link: {
                                path: "999_Test/Q16_test_data/04/dic_20080401.md",
                                embed: false,
                                type: "file",
                            },
                            ctime: "2008-04-01T19:30:50.091+08:00",
                            cday: "2008-04-01T00:00:00.000+08:00",
                            mtime: "2008-04-03T19:30:50.091+08:00",
                            mday: "2008-04-03T00:00:00.000+08:00",
                            size: 126,
                            starred: false,
                            frontmatter: { Date: "2008-04-01" },
                            ext: "md",
                            day: "2008-04-01T00:00:00.000+08:00",
                        },
                        Date: "2008-04-01T00:00:00.000+08:00",
                        Residencia: "BUENOS AIRES",
                        Clan: "Ventrue",
                        Cainita: "Maurtice Durant",
                        Generacion: 10,
                        Status: "Neonato",
                        date: "2008-04-01T00:00:00.000+08:00",
                        residencia: "BUENOS AIRES",
                        clan: "Ventrue",
                        cainita: "Maurtice Durant",
                        generacion: 10,
                        status: "Neonato",
                    },
                    {
                        file: {
                            path: "999_Test/Q16_test_data/03/dic_20080301.md",
                            folder: "999_Test/Q16_test_data/03",
                            name: "dic_20080301",
                            link: {
                                path: "999_Test/Q16_test_data/03/dic_20080301.md",
                                embed: false,
                                type: "file",
                            },

                            ctime: "2008-03-01T19:30:50.091+08:00",
                            cday: "2008-03-01T00:00:00.000+08:00",
                            mtime: "2008-03-03T19:30:50.091+08:00",
                            mday: "2008-03-03T00:00:00.000+08:00",
                            size: 121,
                            starred: false,
                            frontmatter: {
                                Date: "2008-03-01",
                            },
                            ext: "md",
                            day: "2008-03-01T00:00:00.000+08:00",
                        },
                        Date: "2008-03-01T00:00:00.000+08:00",
                        Residencia: "BUENOS AIRES",
                        Clan: "Ventrue",
                        Cainita: "Vortex",
                        Generacion: 6,
                        Status: "En Desgracia",
                        date: "2008-03-01T00:00:00.000+08:00",
                        residencia: "BUENOS AIRES",
                        clan: "Ventrue",
                        cainita: "Vortex",
                        generacion: 6,
                        status: "En Desgracia",
                    },
                ],
                length: 2,
            },
        },
    ],
    length: 1,
};



code DVJS10_groupBy_Residencia_and_TABLE

Summary_code
title: DVJS10_groupBy_Residencia_and_TABLE => 1.To groupBy page.Residencia AS G1 (G1=group.rows) 2.To display each G1 as a table
collapse: close
icon: 
color: 
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"999_Test/Q16_test_data"')
    .where((page) => dv.func.contains(page.file.name, "dic_"));    


// M21. define groups:
// To groupBy page.Residencia AS G1 (G1=group.rows)
// #####################################################################
let groups = pages.groupBy((page) => page.Residencia);


// M81. output groups:
// #####################################################################
for (let group of groups) {
    dv.header(2, group.key);
    dv.table(
        ["Clan", "Cainita", "Generacion", "Status"],
        group.rows
            .sort((page) => page.Clan, "asc")
            .map((page) => [page.Clan, page.Cainita, page.Generacion, page.Status])
    );
}



```

Screenshots(DVJS10):


DVJS20_groupBy_Residencia_groupIn_Clan_and_TABLE

Summary

Main DVJS

Code Name Data type Group By Purposes Remark
DVJS20_groupBy_Residencia
_groupIn_Clan_and_TABLE
string yes 1.To groupBy page.Residencia AS G1 (G1=group.rows)

2.To groupIn page.Clan AS G2 (G2=group.rows.rows=G1.rows)

3.To display each G1 as a table

4.To display each G1 with each G2 which is grouped in G1 as a table

Notes:

Summary
// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"999_Test/Q16_test_data"')
    .where(
        (page) =>
            page.file.name === "dic_20080301" ||
            page.file.name === "dic_20080401"
    );


// M21. define groups:
// To groupBy page.Residencia AS G1 (G1=group.rows)
// To groupIn page.Clan AS G2 (G2=group.rows.rows=G1.rows)
// #####################################################################
let groups = pages
    .groupBy((page) => page.Residencia)
    .groupIn((page) => page.Clan);
    
    
// M30. output groups: formatted by Prettier - Code formatter v9.5.0 in VScode
// #####################################################################
//dv.span("The following is the content of the `groups`.\n");
dv.span(JSON.stringify(groups, null, 2), "\n");

  • Here is a slice of the groups , where page.file.name is dic_20080301 or dic_20080401.
let slice_groups = {
    values: [
        {
            key: "BUENOS AIRES",
            rows: {
                values: [
                    {
                        key: "Ventrue",
                        rows: {
                            values: [
                                {
                                    file: {
                                        path: "999_Test/Q16_test_data/03/dic_20080301.md",
                                        folder: "999_Test/Q16_test_data/03",
                                        name: "dic_20080301",
                                        link: {
                                            path: "999_Test/Q16_test_data/03/dic_20080301.md",
                                            embed: false,
                                            type: "file",
                                        },
                                        
                                        ctime: "2008-03-01T19:30:50.091+08:00",
                                        cday: "2008-03-01T00:00:00.000+08:00",
                                        mtime: "2008-03-03T19:30:50.091+08:00",
                                        mday: "2008-03-03T00:00:00.000+08:00",
                                        size: 121,
                                        starred: false,
                                        frontmatter: { Date: "2008-03-01" },
                                        ext: "md",
                                        day: "2008-03-01T00:00:00.000+08:00",
                                    },
                                    Date: "2008-03-01T00:00:00.000+08:00",
                                    Residencia: "BUENOS AIRES",
                                    Clan: "Ventrue",
                                    Cainita: "Vortex",
                                    Generacion: 6,
                                    Status: "En Desgracia",
                                    date: "2008-03-01T00:00:00.000+08:00",
                                    residencia: "BUENOS AIRES",
                                    clan: "Ventrue",
                                    cainita: "Vortex",
                                    generacion: 6,
                                    status: "En Desgracia",
                                },
                                {
                                    file: {
                                        path: "999_Test/Q16_test_data/04/dic_20080401.md",
                                        folder: "999_Test/Q16_test_data/04",
                                        name: "dic_20080401",
                                        link: {
                                            path: "999_Test/Q16_test_data/04/dic_20080401.md",
                                            embed: false,
                                            type: "file",
                                        },
                                        
                                        ctime: "2008-04-01T19:30:50.091+08:00",
                                        cday: "2008-04-01T00:00:00.000+08:00",
                                        mtime: "2008-04-03T19:30:50.091+08:00",
                                        mday: "2008-04-03T00:00:00.000+08:00",
                                        size: 126,
                                        starred: false,
                                        frontmatter: {
                                            Date: "2008-04-01",
                                        },
                                        ext: "md",
                                        day: "2008-04-01T00:00:00.000+08:00",
                                    },
                                    Date: "2008-04-01T00:00:00.000+08:00",
                                    Residencia: "BUENOS AIRES",
                                    Clan: "Ventrue",
                                    Cainita: "Maurtice Durant",
                                    Generacion: 10,
                                    Status: "Neonato",
                                    date: "2008-04-01T00:00:00.000+08:00",
                                    residencia: "BUENOS AIRES",
                                    clan: "Ventrue",
                                    cainita: "Maurtice Durant",
                                    generacion: 10,
                                    status: "Neonato",
                                },
                            ],
                            
                            length: 2,
                        },
                    },
                ],
                
                length: 1,
            },
        },
    ],
    
    length: 1,
};



code DVJS20_groupBy_Residencia_groupIn_Clan_and_TABLE

Summary_code
title: DVJS20_groupBy_Residencia_groupIn_Clan_and_TABLE => 1.To groupBy page.Residencia AS G1 (G1=group.rows) 2.To groupIn page.Clan AS G2 (G2=group.rows.rows=G1.rows) 3.To display each G1 as a table 4.To display each G1 with each G2 which is grouped in G1 as a table
collapse: close
icon: 
color: 
```dataviewjs
// M11. define pages: gather all relevant pages
// #####################################################################
//let pages = dv.current();
let pages = dv
    .pages('"999_Test/Q16_test_data"')
    .where((page) => dv.func.contains(page.file.name, "dic_"));    


// M21. define groups:
// To groupBy page.Residencia AS G1 (G1=group.rows)
// To groupIn page.Clan AS G2 (G2=group.rows.rows=G1.rows)
// #####################################################################
let groups = pages
    .groupBy((page) => page.Residencia)
    .groupIn((page) => page.Clan);


// M71. output groups: groupBy_Residencia
// #####################################################################
dv.header(2, "M71.output groups: groupBy_Residencia");
for (let group of groups) {
    dv.header(4, group.key);
    dv.table(
        ["Clan", "Cainita", "Generacion", "Status"],
        group.rows.rows
            .sort((page) => page.Clan, "asc")
            .map((page) => [page.Clan, page.Cainita, page.Generacion, page.Status])
    );
}

dv.span("-----");

// M81. output groups: groupBy_Residencia and groupIn_Clan
// #####################################################################
dv.header(2, "M81. output groups: groupBy_Residencia and groupIn_Clan");
for (let group of groups) {
    dv.header(4, group.key);
    dv.table(
        ["Clan", "Cainita", "Generacion", "Status"],
        group.rows
            .sort((G1) => G1.rows.Clan, "asc")
            .map((G1) => [
                G1.rows.Clan[0],
                G1.rows.Cainita,
                G1.rows.Generacion,
                G1.rows.Status,
            ])
    );
}



```

Screenshots(DVJS20):

Part 1/2:

Part 2/2:


3 Likes

Hi @rflmorais - in your description you have “any page with no outgoing links”, but in your code you’re looking at pages with unresolved links (i.e. links to pages which don’t exist).

If you’re specifically wanting just “no outgoing links”, you can simplify the code a lot:

const pages = dv.pages().where(x => !x.file.outlinks.length)

Thank you so much! Thank you for taking the time to make it so clear!
I feel like I really learnt something instead of just “copy-paste” an answear, and I hope this will also help others with the same use case!!

All my vampires are neatly grouped now!! Thanks again! :bat:

3 Likes

Great observation, I thing I dit wrong with the description

Hi, does somebody have some example on how to get all Values from a Specific Key from all notes with a specific tag?
Plus the values should be stored in an array with value, value (as I need it for templater suggester).

Possibly useful: you can use Dataview DQL queries inside dataviewjs if it is easier to express a query that way. e.g. dv.tryQuery('LIST WITHOUT ID SpecificKey FROM #tag').values should give you a list of the values you want, but there will be duplicates so you probably want to convert the result to a Javascript Set before mapping to the [value, value] array format that you want.

1 Like

I’m not getting there (lack of JS knowledge)

let pages = dv.pages("#content/code")
let allPrograms = pages.map(a => a.program);
let uniqPrograms = allPrograms.filter((item, index) => allPrograms.indexOf(item) === index)

creates a Proxy { values: Array(5)… in console, what I need is {key: value, key: value… in console. If I replace a.program with [a.program, a.program] I get arrays in arrays… :disappointed:

btw: 5 is correct… it is normally 6 but one duplicate.

So I have the following problems:
a) how to get the correct datastructure
b) how to remove duplicates from that
c) how to remove undefined from that

1 Like

Can anyone advise if its possible to populate a TaskList nested within a Table row? I’m trying to incorporate this, but when I attempt this, I’m left with fields within the actual table rows, and the TaskList rendered above the table.

Sample Markdown (codeblock tildes replaced with apostrophes):

'''dataviewjs
let md = dv.current().file.tasks
dv.table([" "], 
	md.map(m => [dv.taskList([m])])
	)
'''

- [ ] this task
- [ ] that task

Outcome:
image

i want to construct a dataviewjs table to show only the events coming in say september. i also have tags. Can someone help in the dataview query. i tried but was not able to construct based on september month

I also tried to do this via reactjs components but failed to get the data inside a md file. Hence now trying this via dataviewjs

below are my contents in md file:

Autumn starting | 2022-09-03 #event
Winter starting | 2022-11-03 #event
Birthday | 2022-12-03 #birthday
one more | 2022-10-23 #anniversary

You can have a look at my upcoming event calendar here, it should give you some pointers.

It creates a visual calendar view of all upcoming events: