Skip to content

Instantly share code, notes, and snippets.

@AJFaraday
Forked from anonymous/contact-to-table.js
Created September 4, 2016 19:31
Show Gist options
  • Save AJFaraday/7f77ae0b3cde798c08f795c7122a1c21 to your computer and use it in GitHub Desktop.
Save AJFaraday/7f77ae0b3cde798c08f795c7122a1c21 to your computer and use it in GitHub Desktop.
js data to table with jquery
contacts = [
{
firtst: 'joe',
last: 'bloggs',
address: '123 Fake St'
},
{
firtst: 'Penny',
last: 'Farthing',
address: '321 Fake St'
}
]
// $ is a shortcut to the jQuery library, and utility methods it provides
// $('.awesome_class') is a query on DOM elements in the HTML, in this case with the class 'awesome_class'
// $('<h1>') is a newly created DOM object, not yet in the HTML, note the angle brackets.
ContactTable = {
draw: function(contacts) {
// create a DOM object
table = $('table');
// add a class
table.addClass('contact_table');
//Add the HTML contents
// A standard header row
table.append(this.header_row());
// Iterate through the contacts
$.each(
contacts,
function(contact) {
// For each one, get the DOM returned by the data_row function
table.append(this.data_row(contact));
}
);
// Append to a known element in the HTML document
$('#placeholder').html(table);
},
header_row: function() {
// make DOM
row = $('<tr>');
row.addClass('header_row');
// Append cells made by header_cell function
row.append(this.header_cell('First'));
row.append(this.header_cell('Last'));
row.append(this.header_cell('Address'));
// return so it can be used up in the draw method
return row;
},
header_cell: function(content) {
// Make DOM
cell = $('<th>');
// Add content
cell.html(content);
// return for use in header_row
return cell;
},
// Follows the same pattern as header_row and header_cell
data_row: function(contact) {
row = $('<tr>');
row.addClass('data_row');
row.append(this.data_cell(contact.first));
row.append(this.data_cell(contact.last));
row.append(this.data_cell(contact.address));
return row;
},
data_cell: function(content) {
cell = $('<td>');
cell.html(content);
return cell;
}
}
<!DOCTYPE html>
<html>
<head>
<script src='./contact-to-table.js'/>
</head>
<body>
<h1>My womderful contact app!</h1>
<div id="placeholder"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment