It took a bit of trial and error to work out the inner workings, but I was able to make metaedit work with dataviewjs. For my use case, I have a monthly note (named YYYY-MM.md) and daily note (named YYYY-MM-DD.md) that all live in the LogBook directory. The daily notes include metadata about when I start and end my day, and hours I worked. I created a file with the code below and itās embedded it in my monthly note.
Now, I just click on hours, startTime, endTime, or note cell, and can edit the value in the monthly note without leaving the page. The only downside is that the only way to clear a value is to type a space in the modal prior to hitting enter. Would love to be able to just hit enter to clear. editMetaElement doesnāt return the content on what was entered so thereās no way to know whether a value was entered or not - if it did, I could use the update operation to clear out the value on my own.
const app = this.app;
const metaedit = app.plugins.plugins["metaedit"];
const init_dom = (row) => {
const sel = `table.dataview.table-view-table td > span > a[data-href='${row.file.path}']`;
const tr = dv.container.querySelector(sel).closest("tr");
const labels = tr.querySelectorAll("._dataview-labels");
for (let i = 0; i < labels.length; i++) {
const data = labels[i].data
const file = app.vault.getAbstractFileByPath(row.file.path);
const prop = {key: data.name, content:data.actual, type: data.type};
labels[i].addEventListener('click', async (evt) => {
metaedit.controller.editMetaElement(prop, [], file);
});
}
}
const field = (row, name, type) => {
let data = {};
data.row = row;
data.name = name;
data.actual = row[name];
data.value = row[name];
data.type = type || 1;
const el = this.container.createEl('span', {"text": row[name] || "-"});
el.classList.add("_dataview-labels");
el.data = data;
return el;
};
try {
var workspace = dv.container.closest("div.workspace-leaf");
var title = workspace.querySelector("div.view-header-title");
var prefix = title.innerHTML;
let pages = dv.pages('"LogBook"')
.sort(t => t.file.name)
.where(t => t.file.name.startsWith(`${prefix}-`) && t["type"] == "logbook-daily");
dv.table(
["File", "Hours", "Day", "Start Time", "End Time", "Notes", ""],
pages
.map(t => [
t.file.link,
field(t, 'hours'),
t["weekday"],
field(t, 'startTime'),
field(t, 'endTime'),
field(t, 'notes')
])
);
setTimeout(function(){
for (let i = 0; i < pages.length; i++) {
init_dom(pages[i]);
}
}, 0);
} catch (err) {
window.console.info(err);
}
