Meta Bind within DataviewJS

The tasks are color-coded based on their status and the status dropdown updates the frontmatter on the relevant task note.

Sharing it here because it took me forever to figure out how to embed a Meta Bind field in a dataview table. Hoping it some day may help someone else.

The trick was the ${m.file.path} syntax which escapes the Meta Bind code properly and allows me to use the variables from DataviewJS.


const DOING    = "Doing";
const BLOCKED  = "Blocked";
const PLANNED  = "Planned";
const REJECTED = "Rejected";
const DONE     = "Done";

const priority={
	[DOING]   : 1,
	[BLOCKED] : 2,
	[PLANNED] : 3,
	[REJECTED]: 4,
	[DONE]    : 5
};

const style={
	[DOING]   : "doingTask",
	[BLOCKED] : "blockedTask",
	[PLANNED] : "plannedTask",
	[REJECTED]: "rejectedTask",
	[DONE]    : "doneTask"
};

function formatted(status, text){
	return "<span class=\""+style[status]+"\">"+text+"</span>"
 };

dv.table(["Task", "Project", "Status", "Deadline"], dv.pages('"Tasks"')
 .filter(f=>f.file.name !== dv.current().file.name)
 .sort(s => priority[s.status]+s.file.name)
 .map(m => [
formatted(m.status,m.file.link),
formatted(m.status,m.project ||""),
`\`INPUT[inlineSelect(option(${DOING}), option(${BLOCKED}), option(${PLANNED}),option(${REJECTED}),option(${DONE})):${m.file.path}#status]\``,
m.deadline ||""
]))
2 Likes

If I’m understanding this correctly, you made a Dataview table that allows you to change the property of one of your task notes in a separate file from the file containing the Dataview table? If so, that’s super neat and I’m going to swipe that idea to use, myself.

Yes, exactly. The task view was mostly a proof of concept for that idea.

For role-playing games, being able to change character hit points from a central game master screen note is another use case I had.

1 Like

I wanted to do something similar some time ago, but I didn’t want to depend on neither buttons nor meta bind plugins, so then I used an ordinary <input> with a script attached, which used the app.fileManager.processFrontMatter() to change the file(s) in question. This is illustrated in the post below:

Is this a better solution than yours, @Skallaturi ? Not really, but it’s an alternate approach which could be an option in some cases. What I really like about your solution is that it hides away all the logic to edit the other files. (Which kind of is also what I like about my variant)

It all depends on what you want, and how much coding you want to do. I just wanted to showcase that other variant alongside, for future prospects looking into stuff like this.

I wonder: Is it possible to use this in combination with pure dataview as well?

So if you create a folder in your vault, and insert the following code into it:

My test files:  [[t94626-1]] [[t94626-2]] [[t94626-3]] [[t94626-4]]
## Files in folder

```dataview
TABLE field1, field2, field3, why
WHERE file != this.file and file.folder = this.file.folder
```

## With meta-bind

```dataview
TABLE
  "`INPUT[inlineSelect(option(one), option(two), option(three), option(four)):"+file.path+"#field1]`" as field1select, 
  "`INPUT[toggle:"+file.path+"#field2]`" as field2toggle,
  "`INPUT[datePicker:"+file.path+"#field3]`" as field2datePicker
WHERE file.folder = this.file.folder AND file != this.file
```

You’re able to get an output like the following:

So I guess the answer to my question, is Yes, you can use this within a pure Dataview table
:smiley:

3 Likes

Curious. This is very close to the syntax I tried at first, which failed.

Also, that post would have saved me a lot of time if I had found it.