각 편집 단추가있는 데이터가있는 테이블을 렌더링하는 데 핸들 막대를 사용했습니다. 두 번째 행의 편집 단추를 클릭하면 해당 행의 값을 가져 오는 방법을 알 필요가 있습니다.테이블에서 특정 행의 값을 가져오고 행을 삭제하려면
HTML은이
<table class="table table-bordered">
<thead>
<tr>
<th>Model</th>
<th>Brand</th>
<th>Year</th>
<th>Action</th>
</tr>
<tr>
<td><input class="form-control" id="modelname"></td>
<td><input class="form-control" id="brandname"></td>
<td><input class="form-control" id="yearname"></td>
<td><button class="btn btn-primary add">Add</button><button class="btn btn-primary update">Update</button></td>
</tr>
</thead>
<tbody class="product-list">
</tbody>
</table>
</div>
<script id="list-item" type="text/x-handlebars-template">
{{#each this}}
<div class="list-item">
<tr>
<td>{{ model }}</td>
<td>{{ brand }}</td>
<td>{{ year }}</td>
<td><button class="btn btn-info edit">Edit</button> <button class="btn btn-danger delete">Delete</button></td>
</tr>
</div>
{{/each}}
</script>
입니다 그리고 스크립트는
var Product = Backbone.Model.extend({
});
var ProductList = Backbone.Collection.extend({
model: Product
});
var ProductView = Backbone.View.extend({
el: '.table-bordered',
events: {
'click .add': 'create',
'click .edit':'edit',
'click .update':'update',
'click .delete':'delete'
},
initialize: function(){
// this.model=new Product();
this.collection = new ProductList();
this.listenTo(this.collection, "add", this.render, this);
this.listenTo(this.collection, "edit", this.render, this);
this.listenTo(this.collection, "change", this.render, this);
this.listenTo(this.model,"delete", this.render, this);
},
render: function(){
var source = $("#list-item").html();
var template = Handlebars.compile(source);
$('.product-list').html(template(this.collection.toJSON()))
},
create: function(){
//var product = new Product();
this.model=new Product();
var modelname= document.getElementById('modelname').value;
var brandname=document.getElementById('brandname').value;
var yearname= document.getElementById('yearname').value;
this.model.set({
'model': modelname,
'brand': brandname,
'year': yearname
})
this.collection.add(this.model);
$('#modelname').val("");
$('#brandname').val("");
$('#yearname').val("");
},
edit:function(e){
var jsondata=this.model.toJSON();
console.log(jsondata.model);
$('#modelname').val(jsondata.model);
$('#brandname').val(jsondata.brand);
$('#yearname').val(jsondata.year);
},
update:function()
{
// var product = new Product();
var modelname= document.getElementById('modelname').value;
var brandname=document.getElementById('brandname').value;
var yearname= document.getElementById('yearname').value;
this.model.set({
'model': modelname,
'brand': brandname,
'year': yearname
})
$('#modelname').val("");
$('#brandname').val("");
$('#yearname').val("");
},
delete:function()
{
console.log(JSON.stringify(this.model));
this.model.destroy();
}
});
var productView = new ProductView();
내 문제, 편집에 클릭에, 기록의 배열의 마지막 요소가 편집을위한 텍스트 상자에 채워지고입니다. 그리고 삭제조차도 작동하지 않습니다. 내가 어디 잘못 됐는지 모르겠습니다. 제발 수정하십시오.
모델에 직접 트리거 할 수 있기 때문에 컬렉션에 자동으로 버블 링됩니다. –
또한 'this.listenTo'는 이벤트를 키로, 콜백을 값으로 갖는 객체를 취할 수 있습니다. 'this.listenTo (this.collection, {add : this.add, edit : this.edit, ...}); ' –