I am trying to make a table grid with dynamic number of rows and column with headers using marionette.
I want a grid that looks like: http://jsfiddle.net/zaphod013/c3w61gf6/
So there are
columns = ['breakfast', 'lunch', 'dinner']
rows = ['carbs', 'proteins', 'fats']
and rest of the grid is checkboxes.
I have made the Views for column and row, but I am fairly lost at how to put them in the table, and then how to add the checkbox views.
Code I have is at http://jsfiddle.net/zaphod013/qkctrLxn/
html:
<div id="main-region"></div>
<script id="food-table" type="text/template">
<thead id="column-id">
</thead>
<tbody id="row-id">
</tbody>
</script>
<script id="food-col-item" type="text/template">
<th><%= col %></th>
</script>
<script id="food-row-item" type="text/template">
<td><%= row %></td>
</script>
script:
FoodManager = new Backbone.Marionette.Application();
FoodManager.addRegions({
mainRegion: "#main-region",
});
FoodManager.FoodLayout = Backbone.Marionette.Layout.extend({
template: "#food-table",
regions: {
colRegion:"#column-id",
rowRegion:"#row-id"
}
});
FoodManager.Col = Backbone.Model.extend({});
FoodManager.ColCollection = Backbone.Collection.extend({
model: FoodManager.Col
});
FoodManager.Row = Backbone.Model.extend({});
FoodManager.RowCollection = Backbone.Collection.extend({
model: FoodManager.Row
});
FoodManager.ColItemView = Marionette.ItemView.extend({
template: "#food-col-item",
tagName: "th",
});
FoodManager.ColView = Marionette.CompositeView.extend({
template: "#food-table",
tagName: "thead",
itemView: FoodManager.ColItemView
});
FoodManager.RowItemView = Marionette.ItemView.extend({
template: "#food-row-item",
tagName: "th",
});
FoodManager.RowView = Marionette.CompositeView.extend({
template: "#food-table",
tagName: "table",
itemView: FoodManager.RowItemView
});
FoodManager.on("initialize:after", function(){
var columns = new FoodManager.ColCollection([
{col: 'Breakfast'},
{col: 'Lunch'},
{col: 'Dinner'}
]);
var rows = new FoodManager.RowCollection([
{row: 'Carbs'},
{row: 'Protein'},
{row: 'Fats'}
]);
var colListView = new FoodManager.ColView({
collection: columns
});
var rowListView = new FoodManager.RowView({
collection: rows
});
var foodLayout = new FoodManager.FoodLayout();
foodLayout.render();
FoodManager.colRegion.show(colListView);
FoodManager.rowRegion.show(rowListView);
FoodManager.mainRegion.show(foodLayout);
});
FoodManager.start();
I will really appreciate some pointers on how to go about this.
Thanks for reading through.