I'm building html tables dynamically depending on the data with Marionette CollectionViews and ItemViews. The structure of the data looks like:
var itemData = [
{
row: "row 1",
items: [
{
item: "row 1, item 1"
},
{
item: "row 1, item 2"
}
]
},
{
row: "row 2",
items: [
{
item: "row 2, item 1"
},
{
item: "row 2, item 2"
}
]
}
];
So I have a layout view for the table, a collection view for the rows, another collection view (child of the parent collection view), and item view for the table cells. My code is:
// A Grid Row
var TableDataView = Backbone.Marionette.ItemView.extend({
template: "#data-template",
tagName: "td",
initialize: function(){
console.log('in TableDataView and this.model is ');
console.log(this.model);
}
});
// The grid view
var TableRowView = Backbone.Marionette.CollectionView.extend({
tagName: "tr",
childView: TableDataView
});
// The grid view
var TableBodyView = Backbone.Marionette.CollectionView.extend({
childView: TableRowView,
childViewOptions: function(model, index) {
return {
collection: new Backbone.Collection(model.get('items'))
};
},
});
var LayoutView = Backbone.Marionette.LayoutView.extend({
template: "#table-template",
regions: {
'tableBody': '#table-body'
},
showChildren: function() {
this.getRegion('tableBody').show(new TableBodyView({
collection: this.collection,
}));
},
render: function() {
Backbone.Marionette.LayoutView.prototype.render.apply(this, arguments);
this.showChildren();
return this;
}
});
// ----------------------------------------------------------------
// Below this line is normal stuff... models, templates, data, etc.
// ----------------------------------------------------------------
var itemData = [
{
row: "row 1",
items: [
{
item: "row 1, item 1"
},
{
item: "row 1, item 2"
}
]
},
{
row: "row 2",
items: [
{
item: "row 2, item 1"
},
{
item: "row 2, item 2"
}
]
}
];
var Item = Backbone.Model.extend({});
var ItemCollection = Backbone.Collection.extend({
model: Item
});
var itemCollection = new ItemCollection(itemData);
var layoutView = new LayoutView({
collection: itemCollection
});
layoutView.render();
$("#table").html(layoutView.el);
The jsfiddle is here. As you can see, the table structure is incorrect because Backbone automatically wraps the TableBodyView
in a div
wrapper. This then breaks the table HTML. I know on can remove the div
from the DOM after render, but I was hoping for a nicer solution...
EDIT
Just to make it clearer, in the TableDataView and TableRowView, i specify tagName
to be <tr>
and <td>
respectively. If I was to do the same in TableBodyView i.e. specify the tagName
as <tbody>
, I'm still left with the same problem, as i'll have to put a <div id="table-body">
in the table template, which again means the table HTML will still be incorrect...