Dataview: List all fields in a page as a table (wide to long)

What I’m trying to do

I have a page with a bunch of fields like

[a:: x]
[b:: y]
[c:: z]

How do I use Dataview to turn this into the table

key value
a x
b y
c z

Things I have tried

Praying to the gods of Obsidian for guidance (they haven’t helped)

There is the “long way” which is to make a list:

- [key:: a]
  [value:: x]
...

but I’d rather not have to get that verbose. Is there a way to use fields of the form [a:: x]?

Whether you use YAML or inline fields shouldn’t affect how you make a table. Have you read the documentation? I don’t use DataView but I remember the documentation being pretty good when I looked at it.

Dataview queries notes, so a simple table will render as in the image below.

A list of keys and values is possible but doesn’t create a two-column table (someone else will probably have a better solution).

Sample queries in the code block below.

Perhaps possible with DataviewJS or CSS.

Would be nice to have the option switch rows and columns easily.

[a:: x], [b:: y], [c:: z]

Dataview queries notes, so a simple table will render as:

```dataview
TABLE
a, b, c
WHERE file.name = this.file.name
```

A list of keys and values is possible but doesn’t create a two-column table (someone else will probably have a better solution).

```dataview
TABLE WITHOUT ID
list(
"**a** " + a,
"**b** " + b,
"**c** " + c)
as "Fields"
WHERE file.name = this.file.name
```

Perhaps possible with [DataviewJS](https://forum.obsidian.md/t/how-to-switch-column-and-row-text-using-dataviewjs/58366) or [CSS](https://github.com/blacksmithgu/obsidian-dataview/issues/1177#issuecomment-1153026541).

Would be nice to have the option switch rows and columns easily.

https://github.com/blacksmithgu/obsidian-dataview/issues/1177

Whether it’s a better solution is for someone else to decide, but here is an alternate approach:

[a:: x], [b:: y], [c:: z]

```dataview
TABLE WITHOUT ID field as Field, row[field] as Value
FLATTEN list("a", "b", "c") as field
WHERE file = this.file
```

You still have to list all of the fields you want to display in your table.

If you want to automate it to display every field related to your current file (including normalised field names), you could try either of the following:

`$= let all = dv.current(); delete all.file; dv.span(all); console.log(all) `

`$= let all = dv.current(); delete all.file; dv.table(["Field", "Value"], Object.entries(all)) `

The first in list form, and the second in a table format. This could be further enhanced to eliminate either the normalised (or the original) form of the fields, if one is so inclined.

( Update: If your note links to other notes, or is linked to from other notes, or you’ve got tasks and/or lists defined in this note, you might need to delete these parts as well to get to the defined fields. )

1 Like

I of course had to give this an attempt, and I found a way, which partially works, but it might get into problems with emoji field names, but for those feeling adventurous try out the following in a note with multiple fields:

```dataviewjs

let page = dv.current()
delete page.file
// Locate sanity duplicates
let ignore = []
let allKeys = Object.keys(page)
for (let key of allKeys ) {
  const sane = sanitize(key)
  
  if ( sane != key && allKeys.includes(sane) )
    ignore.push(sane)

}

// Ignore the following, by uncommenting lines
// Note you need to have the letter case correct...
ignore.push("Tags")
ignore.push("aliases")

// List all not ignored
let saneFields = {}
for (const key of Object.keys(page).sort((a, b) => a.localeCompare(b)) ) {
  if ( !ignore.includes(key) )
    saneFields[key] = page[key]
}

dv.table(["Field", "Sanitized?", "Value"],
  Object.entries(saneFields).map(i  => [i[0], sanitize(i[0]), i[1] ] ) )

function sanitize(text) {
  return text
    .toLocaleLowerCase()
    .replace(/ /g, '-')
    .replace(/[^0-9\p{Letter}_-]+/gu, '')
    // This removes emoji-character stuff, which the original handles better

  // Original sanitizer from Dataview
  // P.regex(new RegExp(emojiRegex(), "")),
  // P.regex(/[0-9\p{Letter}_-]+/u).map(str =>  str.toLocaleLowerCase()),
  // P.whitespace.map(_ => "-"),
  // P.any.map(_ => "") 
}
```
An example file with ugly field names
---
Tags: f65436
aliases: "an alias"
---
questionUrl:: http://forum.obsidian.md/t//65436

[a:: x], [b:: y], [c:: z], [e f§!"#$%/.gf. h-i er:: multi], [æØå:: ÆØÅ]
[§Hi:: keeper?]
[↪⌚⌛↩:: Mean]
[↪left LARGE right↩:: Mean2]

Gave this output:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.