2015-10-15 66 views
-1

我有一個相當簡單的骨幹視圖,它在通過_.each函數遍歷集合中的每個項目時呈現標記。通過backbone.js創建具有多個單元格的行

var SingleView = Backbone.View.extend({ 
    tagName:td, 
    //other stuff 
}); 

我有收集視圖以及

var CollectionView = Backbone.View.extend({ 
//other stuff 
this.collection.each(function(model ,i) { 
var item = new SingleView({model: model}); 
//alert(i); 
//maybe in here is where I need logic to wrap a row around a <td> tag ? 
$('.collection').append(item.render().$el); 

}); 
模板

然後:

<table class = "collection"> 
<!-- variables to render as html --> 
</table> 

什麼我希望做的是包裹在項目的給定數目表格行或<tr></tr>。獲得一個單元/行很容易。我試圖讓每行有2或3個或者多個單元格。我試過Jquery wrap功能,我猜我需要某種模塊化的部門,但我有點卡住了。我也是Backbone的新手,這是另一個障礙。

澄清 - 集合中的單個項目應該在每個<td>中,並且我試圖在每行中放置> 1 <td>

+2

你的問題我不清楚。預期什麼,你的結果是什麼?在視圖中Fyi'$(...)'是一個壞習慣,你應該使用'this。$(...)來縮小視圖的範圍。)' –

+0

我試圖做的是有辦法來控制表中的填充行的模型實例的數量,因此,例如'​​模型1視圖​​模型2視圖'或'也許​​模型1個視圖​​模型2視圖​​模型3視圖' – user3653270

+0

所以每行可具有可變列計數?你知道這張桌子看起來很奇怪,如果你這樣做,可能格式不正確?當計數不同時,您希望如何對齊每一行的列?新行開始時應該控制什麼? _如果這是你的問題,你可能想重新構思你的問題,並詢問如何改變執行方式(當且僅當你接受其他方案時)。# –

回答

0

我試圖創建一個工作示例儘可能靠近你的解釋成爲可能。它將一個集合呈現爲表單元格,我認爲你可以從那裏開始。

基本上,您不應該手動將td標記爲tr標記。根據數據流量分別創建TrViewTdView的實例。附加trtabletdtr

而且你可能想看看的CollectionView在木偶實施: http://marionettejs.com/docs/v2.4.3/marionette.collectionview.html

例子#1:

var collection = new Backbone.Collection([ 
 
    { title: 'Item 1' }, 
 
    { title: 'Item 2' }, 
 
    { title: 'Item 3' } 
 
]); 
 

 
var TableView = Backbone.View.extend({ 
 
    tagName: "table", 
 
    className: "collection", 
 
    render: function(){ 
 
    var rowView = new RowView({ collection: collection }); 
 
    this.$el.html(rowView.render().el); 
 
    return this; 
 
    } 
 
}); 
 

 
var RowView = Backbone.View.extend({ 
 
    tagName: "tr", 
 
    render: function(){ 
 
    this.collection.each(this.appendItem, this); 
 
    return this; 
 
    }, 
 
    appendItem: function(model){ 
 
    var tdView = new TdView({ model:model }); 
 
    this.$el.append(tdView.render().el); 
 
    } 
 
}); 
 

 
var TdView = Backbone.View.extend({ 
 
    tagName: "td", 
 
    render: function(){ 
 
    this.$el.text(this.model.get('title')); 
 
    return this; 
 
    } 
 
}); 
 

 
var tableView = new TableView(); 
 
tableView.render().$el.appendTo(document.body);
table { 
 
    border: 1px solid steelblue; 
 
} 
 
td { 
 
    border: 1px solid brown; 
 
    padding: 3px 5px; 
 
}
<script src='http://code.jquery.com/jquery.js'></script> 
 
<script src='http://underscorejs.org/underscore.js'></script> 
 
<script src='http://backbonejs.org/backbone.js'></script>

例2:

var collection = new Backbone.Collection([ 
 
    { title: 'Item 1' }, 
 
    { title: 'Item 2' }, 
 
    { title: 'Item 3' }, 
 
    { title: 'Item 4' }, 
 
    { title: 'Item 5' }, 
 
    { title: 'Item 6' } 
 
]); 
 

 
var TableView = Backbone.View.extend({ 
 
    tagName: "table", 
 
    className: "collection", 
 
    initialize: function(o){ 
 
    this.maxItemsInRow = o.maxItemsInRow? o.maxItemsInRow: 3; 
 
    }, 
 
    render: function(){ 
 
    this.collection.each(this.addItem, this); 
 
    return this; 
 
    }, 
 
    addRow: function(){ 
 
    var rowView = new RowView({ collection: collection }); 
 
    rowView.$el.appendTo(this.el); 
 
    this.currentRow = rowView; 
 
    this.currentRowItemsLength = 0; 
 
    }, 
 
    addItem: function(model){ 
 
    if(typeof this.currentRow === 'undefined') { 
 
     this.addRow(); 
 
    } 
 
    this.currentRow.addItem(model); 
 
    this.currentRowItemsLength++; 
 
    if(this.currentRowItemsLength >= this.maxItemsInRow){ 
 
     this.currentRow = undefined; 
 
    } 
 
    } 
 
}); 
 

 
var RowView = Backbone.View.extend({ 
 
    tagName: "tr", 
 
    addItem: function(model){ 
 
    var tdView = new TdView({ model:model }); 
 
    this.$el.append(tdView.render().el); 
 
    } 
 
}); 
 

 
var TdView = Backbone.View.extend({ 
 
    tagName: "td", 
 
    render: function(){ 
 
    this.$el.text(this.model.get('title')); 
 
    return this; 
 
    } 
 
}); 
 

 
var tableView = new TableView({ collection: collection, maxItemsInRow: 3 }); 
 
tableView.render().$el.appendTo(document.body);
table { 
 
    border: 1px solid steelblue; 
 
} 
 
td { 
 
    border: 1px solid brown; 
 
    padding: 3px 5px; 
 
}
<script src='http://code.jquery.com/jquery.js'></script> 
 
<script src='http://underscorejs.org/underscore.js'></script> 
 
<script src='http://backbonejs.org/backbone.js'></script>

+0

我試過這樣的方法 - 謝謝。所以我卡住的是讓我們說集合有6個模型,我想在表中有2行,每個3個單元格。我需要視圖以某種方式知道何時停止向每行添加單元格以及何時開始新行。 – user3653270

+0

您可以編程您的TableView直接接受項目並動態追加一個新的TrView,然後將項目附加到該TrView,除非它們在定義的數量內,那麼表格將附加另一個Tr並繼續添加項目到新的行,等等上。 – Yura

+0

我已經用第二個例子更新了我的答案,顯示瞭如何做到這一點。 – Yura

0

這樣的事情?

http://jsfiddle.net/9mjcmgpo/

var SingleView = Backbone.View.extend({ 
    tagName: 'tr', 
    template: _.template('<td><%=name%></td><td><%=age%></td><td><%=sex%></td>'), 
    render: function() { 
     this.$el.append(this.template(this.model.toJSON())) 
     return this 
    } 
}); 

var CollectionView = Backbone.View.extend({ 
     template: _.template('<tr><th>name</th><th>age</th><th>sex</th></tr>'), 
    initialize: function() { 
     this.$el = $('.collection'); 
     this.el = this.$el[0]; 

     // sudo collection 
     this.collection = new Backbone.Collection(); 
     this.collection.add({'name': 'test1', 'sex': 'f', age: 21}); 
     this.collection.add({'name': 'test2', 'sex': 'o', age: 25}); 
     this.collection.add({'name': 'test3', 'sex': 'm', age: 26}); 

     // hack 
     this.render(); 
    }, 
    render: function() { 
     this.$el.html(this.template()); 
     this.collection.each(_.bind(function(model) { 
      var item = new SingleView({model: model}); 
      this.$el.append(item.render().$el); 
     }, this)); 
     return this; 
    } 
}); 

new CollectionView() 
0

你可以像下面這樣。我將硬編碼爲3作爲行數。如有必要,您可以通過將其傳遞給視圖來使用可變數字。

var collection = new Backbone.Collection([{ 
 
    title: 'Item 1' 
 
}, { 
 
    title: 'Item 2' 
 
}, { 
 
    title: 'Item 3' 
 
}, { 
 
    title: 'Item 4' 
 
}, { 
 
    title: 'Item 5' 
 
}]); 
 

 
var TdView = Backbone.View.extend({ 
 
    tagName: "td", 
 
    initialize: function() { 
 
    this.render(); 
 
    }, 
 
    render: function() { 
 
    this.$el.text(this.model.get('title')); 
 
    return this; 
 
    } 
 
}); 
 

 
var TableView = Backbone.View.extend({ 
 
    tagName: "table", 
 
    className: "collection", 
 
    initialize: function() { 
 
    this.render(); 
 
    }, 
 
    render: function() { 
 
    var $tr = $('<tr/>'); 
 
    var count = 1; 
 
    this.collection.each(function(model, i) { 
 
     if ((i/3) >= count) { // end of current row 
 
     this.$el.append($tr); //append current row 
 
     $tr = $('<tr/>'); // create new row 
 
     count = this.$el.find('tr').length + 1; // refresh counter 
 
     } 
 
     $tr.append(new TdView({ // append td to current row 
 
     model: model 
 
     }).el); 
 
    }, this); 
 
    this.$el.append($tr); // append final row 
 
    return this; 
 
    } 
 
}); 
 

 

 
var tableView = new TableView({ 
 
    collection: collection 
 
}); 
 
tableView.$el.appendTo('body');
table { 
 
    border: 1px solid steelblue; 
 
} 
 
td { 
 
    border: 1px solid brown; 
 
    padding: 3px 5px; 
 
}
<script src='http://code.jquery.com/jquery.js'></script> 
 
<script src='http://underscorejs.org/underscore.js'></script> 
 
<script src='http://backbonejs.org/backbone.js'></script>

相關問題