Skip to content

Instantly share code, notes, and snippets.

@hbrysiewicz
Created January 21, 2014 17:56
Show Gist options
  • Save hbrysiewicz/8544783 to your computer and use it in GitHub Desktop.
Save hbrysiewicz/8544783 to your computer and use it in GitHub Desktop.
/* Put your CSS here */
html, body {
margin: 20px;
}
thead {
font-weight: 900;
}
td {
width: 100px;
padding: 10px;
border: 1px solid #ccc
}
.center {
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Simple Data-Table Ember.js Implementation" />
<meta charset="utf-8">
<title>Data Table Component</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
</head>
<body>
<script type="text/x-handlebars">
<h2>Data Table Component</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
{{data-table-component columnsBinding="columns" dataBinding="data"}}
</script>
<script type="text/x-handlebars" data-template-name="components/data-table-component">
<table class="table">
<thead>
{{#each headers}}
<td {{bindAttr class="class"}}>{{title}}</td>
{{/each}}
</thead>
<tbody>
{{#each rows}}
<tr>
{{#each}}
<td {{bindAttr class="class"}}>{{value}}</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.2.1.js"></script>
<script src="http://builds.emberjs.com/tags/v1.3.1/ember.js"></script>
</body>
</html>
App = Ember.Application.create();
App.IndexController = Ember.Controller.extend({
columns: [
{"title": "First Name", "data": "firstName"},
{"title": "Last Name", "data": "lastName"},
{"title": "Age", "class": "center", "data": "age"}
],
data: [
{"firstName": "Heather", "lastName": "Brysiewicz", "age" : 25},
{"firstName": "Eric", "lastName": "Benson", "age": 27}
]
});
App.DataTableComponentComponent = Ember.Component.extend({
headers: function() {
return this.get('formatted.headers');
}.property('formatted.headers'),
rows: function() {
return this.get('formatted.rows');
}.property('formatted.rows'),
formatted: function() {
var cols = this.get('columns'),
rows = this.get('data'),
formatted = {headers: [], rows: []};
formatted.headers = cols && cols.map(function(col) {
return col.data && {
"title": col.title,
"class": col['class']
};
});
formatted.rows = rows.map(function(row) {
var data = [];
cols.forEach(function(col) {
var item = {};
item.value = row[col.data];
item['class'] = col['class'];
data.push(item);
});
return data;
});
return formatted;
}.property('columns','data')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment