How to convert Dataview table to DataviewJS?

What I’m trying to do

I have a table in regular dataview that functions as a CRM; it displays all notes (people) and does a simple calculation to display their age).

An example of how the person-note is formatted:

Here’s the code:

I would like to convert this code to dataview JS. I want to implement a button like this one that I could press to overwrite the “last-contacted” metadata key with the current date. This would help me track how long it has been since I last spoke to a given person.

I know almost zero javascript. Is it possible to 1) create a copy of the table above and 2) embed a button similar to the one I linked?

Thank you!!

It’s possible if you have enough free time to learn a bit of JS. And also if you pester chatGPT enough to spit out actual code…

function getAge(birthday) {
	var today = new Date();
	var diff = today - birthday;
	var years = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
	var months = Math.floor((diff % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));
	var age = `${years} yr ${months} mo`;
	return age;
}

function formatBirthday(birthday) {
	const date = new Date(birthday);
	const month = date.toLocaleString('default', { month: 'short' });
	const day = String(date.getDate()).padStart(2, '0');
	const year = date.getFullYear();
	return `${month} ${day}, ${year}`;
}

function formatURL(url) {
	try {
		const domain = new URL(url).hostname;
		return domain;
	} catch (error) {
		return url;
	}
}

const {createButton} = app.plugins.plugins["buttons"]
const {update} = app.plugins.plugins['metaedit'].api
const defer = async (file, key) => {
    var contacted = moment(DateTime.now()).format("YYYY-MM-DD")
    await update(key, contacted, file)
}

function isContactedThisMonth(contacted) {
  var currentDate = DateTime.now();
  var contactedDate = DateTime.fromISO(contacted);
  return currentDate.year === contactedDate.year && currentDate.month === contactedDate.month ? "YES" : "NO";
}

dv.table(
	["Bump","Contact?","Name", "Phone", "Email", "Birthday",, "Age", "Relationship", "Instagram", "LinkedIn", "URL"],
	dv.pages(`"Personal/Databases/People"`)
		.where(p => p.file.name != "People")
		.map(p => [
			createButton({app, el: this.container, args: {name: "Bump"}, clickOverride: {click: defer, params: [p.file.path, 'contacted']}}),
			isContactedThisMonth(p.contacted),
			p.file.link,
			p.phone,
			p.email,
			formatBirthday(p.birthday),
			getAge(p.birthday),
			p.relationship,
			p.instagram ? p.instagram.link(`https://www.instagram.com/${p.instagram}`) : "",
			p.linkedin ? p.linkedin.link(`https://www.linkedin.com/in/${p.linkedin}`) : "",
			p.url ? formatURL(p.url).link(`${p.url}`) : ""
		])
);

How to make a CRM in Obsidian using DataviewJS? Embed button into dataview table. Compare to the current date. Calculate age with dataview JS. How to add links to dataview table. Use metaedit in dataview.

1 Like

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