Dataview JS Getting The Tree Root

Optional addition:

You can format the output whatever you like. Since I prefer visual recognition, I use icons. Moreover, some optional description can be provided:

// Before:
const liEl = dv.el("li", liText, { container })

// After:
const icon = node?.icon ?? ''
const link = node?.path ?? node?.file?.link
const description = node?.description ?? ''
const liEl = dv.el("li", icon + link + description, { container })

You can even create pass an input function to make it more agnostic:

await dv.view("dataviews/treeview", {
  childrenKey: 'child',
  rootNames: [
    "File A",
    "File B",
    "File C"
  ],
  format: (node) => {
    const icon = node?.icon ?? ''
    const link = node?.path ?? node?.file?.link
    const description = node?.description ?? ''
    return icon + link + description
  }
});

And then:

// Before:
const liEl = dv.el("li", format(node), { container })
1 Like