2017-08-28 60 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新功能一起使用。 –

相关问题