2017-08-28 48 views
0

嘗試在具有主幹的表格內創建表格,但無法找到方法。任何人都可以通過一個例子來幫助我實現這個目標嗎?使用主幹JS創建子視圖(在另一個表內的表格)

我的收藏:

this.collection = new Backbone.Collection([ 
      { first: 'John', last: 'Doe', desig: 'E1', location: 'C' }, 
      { first: 'Mary', last: 'Jane', desig: 'E3', location: 'C' }, 
      { first: 'Billy', last: 'Bob', desig: 'E2', location: 'C' }, 
      { first: 'Dexter', last: 'Morgan', desig: 'E1', location: 'P' }, 
      { first: 'Walter', last: 'White', desig: 'E2', location: 'P' }, 
      { first: 'Billy', last: 'Bobby', desig: 'E1', location: 'B' } 
     ]); 

普通視圖:使用表格視圖實現了這個。參照那麼代碼here

first last desig location 
---------------------------------- 
Billy Bobby E1  B 
Walter White E2  P 
Dexter Morgan E1  P 
Billy Bob  E2  C 
Marry Jane  E3  C 
John  Doe  E1  C 

按位置要羣想呈​​現像下面

location first last desig  
---------------------------------- 
C   Billy Bob  E2 
      Marry Jane E3 
      John  Doe  E1 
P   Walter White E2 
      Dexter Morgan E1 
B   Billy Bobby E1 

使用下劃線我們可以做分組但在那之後,我努力呈現的對象作爲新的視圖以上視圖

_.groupby(this.collection, "location"); 

給我一個對象,它具有所需的結果。

+0

你只需要呈現一個集合視圖的collection視圖。第一個集合的輸入將是'_.groupby'的結果,子集合視圖的輸入將是一個特定的組,並且它的項目視圖的輸入將是特定的記錄。 –

+0

我試圖創建一個更多的視圖,同時創建第一個表的行視圖,我試圖創建子視圖,但沒有爲我工作。你能指出一個例子,以便我可以學習和實現嗎?https://jsfiddle.net/WebDev81/ddbf9ckv/2/ – user3625533

+0

在小提琴中,你只有2個視圖。請分享您的嘗試與3意見。所有你需要的是創建表體的另一個視圖 –

回答

-2

更新撥弄利用用戶輸入2層不同的觀點

jsfiddle.net/WebDev81/ddbf9ckv/10

讓我知道有人有更好的方法來實現這一目標。

+1

該問題與用戶輸入或設備的不同視圖無關。你的回答應該回答這個問題,就好像你是一個不瞭解你項目其他要求的人。這是一個問答網站。如果您覺得問答不會對其他人有用,只需刪除問題,而不是像這樣回答。 –

1

表中的每一行都應該在Backbone.View中表示。 您想要的分組主要是標準HTML表格的rowspan功能。

看到的片段:

var collection = new Backbone.Collection([{ 
 
    first: 'John', 
 
    last: 'Doe', 
 
    desig: 'E1', 
 
    location: 'Chennai' 
 
}, { 
 
    first: 'Mary', 
 
    last: 'Jane', 
 
    desig: 'E3', 
 
    location: 'Chennai' 
 
}, { 
 
    first: 'Billy', 
 
    last: 'Bob', 
 
    desig: 'E2', 
 
    location: 'Chennai' 
 
}, { 
 
    first: 'Dexter', 
 
    last: 'Morgan', 
 
    desig: 'E1', 
 
    location: 'Pune' 
 
}, { 
 
    first: 'Walter', 
 
    last: 'White', 
 
    desig: 'E2', 
 
    location: 'Pune' 
 
}, { 
 
    first: 'Billy', 
 
    last: 'Bobby', 
 
    desig: 'E1', 
 
    location: 'Bangalore' 
 
}]); 
 

 
var GroupRowView = Backbone.View.extend({ 
 
    tagName: 'tr', 
 
    initialize: function(options) { 
 
    this.groupBy = options.groupBy; 
 
    this.index = options.index; 
 
    this.total = options.total; 
 
    }, 
 
    render: function() { 
 
    this.$el.empty(); 
 
    if (this.index == 0) { 
 
     this.$el.append('<td rowspan="' + this.total + '">' + this.model.get(this.groupBy) + '</td>'); 
 
    } 
 
    _.each(this.model.omit(this.groupBy), function(value, key) { 
 
     this.$el.append('<td>' + value + '</td>'); 
 
    }, this); 
 
    return this; 
 
    } 
 
}); 
 

 
var SimpleRowView = Backbone.View.extend({ 
 
    tagName: 'tr', 
 
    render: function() { 
 
    this.$el.empty(); 
 
    //this.$el.append('<td>' + this.model.get('location') + '</td>') 
 
    _.each(this.model.values(), function(value) { 
 
     this.$el.append('<td>' + value + '</td>'); 
 
    }, this); 
 
    return this; 
 
    } 
 
}) 
 

 
var TableView = Backbone.View.extend({ 
 
    tagName: 'table', 
 
    render: function() { 
 
    /*var self = this; 
 
    self.$el.empty(); 
 
    self.collection.each(function(rowModel) { 
 
     self.$el.append(_.template('<tr><td><%= location %></td><td><%= first %></td><td><%= last %></td><td><%= desig %></td></tr>')(rowModel.attributes)) 
 
    });*/ 
 
    var self = this; 
 
    self.$el.empty(); 
 
    self.collection.each(function(model) { 
 
     var row = new SimpleRowView({ 
 
     model: model 
 
     }); 
 
     self.$el.append(row.render().el); 
 
    }); 
 
    return this; 
 
    }, 
 
    groupCollection: function() { 
 
    var self = this; 
 
    var groups = self.collection.groupBy('location'); 
 
    self.$el.empty(); 
 
    _.each(groups, function(group) { 
 
     var length = group.length; 
 
     _.each(group, function(model, i) { 
 
     var row = new GroupRowView({ 
 
      model: model, 
 
      groupBy: 'location', 
 
      index: i, 
 
      total: length 
 
     }); 
 
     self.$el.append(row.render().el); 
 
     }) 
 
    }) 
 
    } 
 
}); 
 
var table = new TableView({ 
 
    collection: collection 
 
}); 
 

 
$('#table-container').append(table.render().el); 
 
$('#sortBtn').click(function(e) { 
 
    table.groupCollection(); 
 
})
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script> 
 

 

 
<p>What the table should be:</p> 
 
<table border="1"> 
 
    <thead> 
 
    <tr> 
 
     <th>Location</th> 
 
     <th>First</th> 
 
     <th>Last</th> 
 
     <th>Desig</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td rowspan="3">C</td> 
 
     <td>Billy</td> 
 
     <td>Bob</td> 
 
     <td>E2</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Marry</td> 
 
     <td>Jane</td> 
 
     <td>E3</td> 
 
    </tr> 
 
    <tr> 
 
     <td>John</td> 
 
     <td>Doe</td> 
 
     <td>E1</td> 
 
    </tr> 
 
    <tr> 
 
     <td rowspan="2">P</td> 
 
     <td>Walter</td> 
 
     <td>White</td> 
 
     <td>E2</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Dexter</td> 
 
     <td>Morgan</td> 
 
     <td>E1</td> 
 
    </tr> 
 
    <tr> 
 
     <td rowspan="1">B</td> 
 
     <td>Billy</td> 
 
     <td>Bobby</td> 
 
     <td>E1</td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 

 
<p>What the resulting table is:</p> 
 

 
<button id="sortBtn">Sort!</button> 
 

 
<div id="table-container"> 
 

 
</div>

+0

您不需要'self'作爲'_。每個'採取可選的第三參數,這是回調的上下文:'_.each(list,callback,this)' –

+0

@EmileBergeron我知道,但我認爲這將是容易錯過的東西,而解釋。我個人喜歡使用'.bind(this)',因爲它引起了人們對這個Javascript「gottcha」的注意,並且你不使用不必要的變量。哎呀,即使es6胖箭頭功能會更優雅,但我很困惑:2017年,使用骨幹似乎堅持過去,我不應該打擾使用或解釋任何現代的Javascript。你對此有何看法? – vassiliskrikonis

+0

不要誤用函數,而應在寫答案時使用最佳實踐。如果你覺得_context_參數很容易被忽略,你可以添加一個註釋來指出。這個問題中的ES6將超出範圍。我不認爲Backbone堅持過去,我相信它可以與ES6新功能一起使用。 –

相關問題