DataviewJS Snippet Showcase

what’s your problem with it?

```dataviewjs
const table = document.createElement("table");
const rows = ["foo", "bar", "asdf", "bla"].map(text => {
	const tr = document.createElement("tr");
	tr.textContent = text;
	return tr;
});
table.append(...rows);
dv.container.append(table);
```

this appears to be working just fine.

Same error here for other snippets that I’m using. Usually it happens for a short time and then disappears…

Putting the code from @gilmar and @Moonbase59 (thank you both!) together for easier future reference:

/*
    previous/next note by date for Daily Notes
    Also works for other files having a `date:` YAML entry.
    MCH 2021-06-14
    https://forum.obsidian.md/t/dataviewjs-snippet-showcase/17847/21
*/
var none = '(none)';
var folder = app['internalPlugins']['plugins']['daily-notes']['instance']['options']['folder'] || dv.current().file.folder;
var p = dv.pages('"'+folder+'"').where(p => p.file.day).map(p => [p.file.name, p.file.day.toISODate()]).sort(p => p[1]);
var t = dv.current().file.day ? dv.current().file.day.toISODate() : dv.date("now").toFormat("yyyy-MM-dd");
var format = app['internalPlugins']['plugins']['daily-notes']['instance']['options']['format'] || 'YYYY-MM-DD';
var current = '(' + moment(t).format(format) + ')';
var nav = [];
var today = p.find(p => p[1] == t);
var next = p.find(p => p[1] > t);
var prev = undefined;
p.forEach(function (p, i) {
    if (p[1] < t) {
        prev = p;
    }
});
nav.push(prev ? '[[' + prev[0] + ']]' : none);
//nav.push(today ? today[0] : none);
nav.push(today ? today[0] : current);
nav.push(next ? '[[' + next[0] + ']]' : none);

//dv.list(nav);
//dv.paragraph(nav.join(" · "));
dv.paragraph(nav[0] + ' ← ' + nav[1] + ' → ' + nav[2]);
3 Likes

Thanks moonbase and gilmar!
I had to make some changes to get the code to work with my daily note template that’s based on dannb’s daily note template.
I changed the search to be based on tags, and filtered out the template so there aren’t links to it.
I also changed the code to get dates based on the YAML created tag that I am using.
Finally I made the variable names more descriptive.

/*
    previous/next note by date for Daily Notes
    Also works for other files having a `date:` YAML entry.
    MCH 2021-06-14
    https://forum.obsidian.md/t/dataviewjs-snippet-showcase/17847/21
*/
var noNotePlaceholder = '(none)'; 

var notes = dv.pages("#daily").where(note => note.created).map(note => [note.file.name, note.created]).sort(note => note[1]).filter(note => note[1] != "<% tp.file.creation_date() %>");

var currentNoteDate = dv.current().created ? dv.current().created : dv.date("now").toFormat("yyyy-MM-dd");
var dateFormat = 'YYYY-MM-DD';
var formattedCurrentDate = '(' + moment(currentNoteDate).format(dateFormat) + ')';

var navigationLinks = [];
var currentNote = notes.find(notes => notes[1] == currentNoteDate);

var nextNote = notes.find(notes => notes[1] > currentNoteDate);

var previousNote = undefined;
notes.forEach(function (notes) {
    if (notes[1] < currentNoteDate) {
        previousNote = notes;
    }
});

navigationLinks.push(previousNote ? '[[' + previousNote[0] + ']]' : noNotePlaceholder);
navigationLinks.push(currentNote ? currentNote[0] : formattedCurrentDate);
navigationLinks.push(nextNote ? '[[' + nextNote[0] + ']]' : noNotePlaceholder);

dv.paragraph(navigationLinks[0] + ' ← ' + navigationLinks[1] + ' → ' + navigationLinks[2]);
1 Like