Create tasklist with DataviewJS in CustomJS?

HA!; Once again it has been proven that a problem can be solved just by putting it into words :slight_smile:

It was actually very simple: the dv (dataview) object created in the class function probably has no context to the page (at least that’s how I explain it to myself…)
Instead of passing dv.current(), I pass the whole dv into the function and create dv.current inside the object.

class ChapterLessons {
    getLessonTasks(dv) {               // <=====
		let chapter = dv.current()   // <=====
        
		//Create a table with all notes linked from "chapter" which are in the folder "lessons".
		var lessonfolder = "_RMKA/lessons"; 
		// all outgoing notes
		const olessons = 
			// all outgoing notes		
			chapter.file.outlinks
		   	// only those below the lession folder
			.filter(f => f.path.startsWith(lessonfolder))
			// create a page object from the link object
			.map(f => dv.page(f));
		for (let i = 0; i < olessons.length; i++) {
			let lesson = olessons[i]
			let tasks_total = lesson.file.tasks.length
			let tasks_open =  lesson.file.tasks.where(t => !t.completed).length
			let lesson_number = i + 1
			dv.header(3, "đź“– " + lesson_number + ". " + lesson.file.name)
			dv.paragraph(" (" + (tasks_total - tasks_open) + " von " + tasks_total + " erledigt)")
			dv.taskList(lesson.file.tasks, false);
		}	
    }
}

Inside the notes:

//Create ChapterLessons object from scripts/js/ChapterLessons.js (plugin: customJS)
const {ChapterLessons} = customJS;
ChapterLessons.getLessonTasks(dv);      // <=====

Thanks for giving me a place to develop my thoughts :laughing:

3 Likes