Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="twocol">
	
	<div>
		<table id="contactsTable">
			<thead>
				<tr>
					<th>Last Name</th>
					<th>First Name</th>
					<th>Email</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
	</div>
	
	<div>
		<form>
			<input type="hidden" id="key">
			<p>
			<label>First Name <input type="text" id="firstname"></label><br/>
			<label>Last Name <input type="text" id="lastname"></label><br/>
			<label>Email <input type="email" id="email"></label><br/>
			</p>
			<p>
			<button id="saveBtn">Save</button>
			</p>
		</form>
	</div>
</div>
              
            
!

CSS

              
                .twocol {
	display: grid;
	grid-template-columns: 50% 50%;
}

table {
	border-collapse: collapse;
	border: 1px solid black;
	width: 100%;
	max-width: 500px;
}

th, td {
	border: 1px solid black;
	padding: 5px;	
}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', init, false);

let $saveBtn, $firstname, $lastname, $email, $key, $table;
let db;

async function init() {
	db = await initDb();
	$saveBtn = document.querySelector('#saveBtn');
	$saveBtn.addEventListener('click', saveContact, false);
	
	$firstname = document.querySelector('#firstname');
	$lastname = document.querySelector('#lastname');
	$email = document.querySelector('#email');
	$key = document.querySelector('#key');
	$table = document.querySelector('#contactsTable tbody');
	
	renderContacts();
}

// TODO: Validation
async function saveContact(e) {
	e.preventDefault();

	let contact = {
		firstname: $firstname.value,
		lastname: $lastname.value, 
		email: $email.value
	};
	
	if($key.value) contact.id = parseInt($key.value,10);
	console.log(contact);
	await persistContact(contact);
	
	$firstname.value = '';
	$lastname.value = '';
	$email.value = '';
	$key.value = '';
	
	renderContacts();
}

async function renderContacts() {
	let contacts = await getContacts();
	let s = '';
	contacts.forEach(c => {
			s += `
			<tr>
			<td>${c.lastname}</td>
			<td>${c.firstname}</td>
			<td>${c.email}</td>
			<td><button data-key="${c.id}" class="edit">Edit</button> <button data-key="${c.id}" class="delete">Delete</button></td>
			</tr>`;
	});
	$table.innerHTML = s;

	let buttons = document.querySelectorAll('td button.edit');
	buttons.forEach(b => {
		b.addEventListener('click', editContact, false);
	});
	
	buttons = document.querySelectorAll('td button.delete');
	buttons.forEach(b => {
		b.addEventListener('click', deleteContact, false);
	});
	
}

async function editContact(e) {
	let key = parseInt(e.target.dataset.key,10);
	let contact = await getContact(key);
	$firstname.value = contact.firstname;
	$lastname.value = contact.lastname;
	$email.value = contact.email
	$key.value = contact.id;
}

async function deleteContact(e) {
	let key = parseInt(e.target.dataset.key,10);
	await removeContact(key);	
	renderContacts();
}

/*
I create the db, the store, and the index. When done, I return the db
*/
async function initDb() {
	let db = new Dexie('contacts_dexie');
	db.version(1).stores({contacts:'++id'})
	return db;	
}

async function persistContact(contact) {
	await db.contacts.put(contact);
}

async function getContacts() {
	return await db.contacts.toArray();
}

async function removeContact(key) {
	return await db.contacts.delete(key);
}

async function getContact(key) {
	return await db.contacts.get(key);
}
              
            
!
999px

Console