我一直在尝试为backgrid行实现“删除”按钮。这里似乎描述了一个解决方案:
How to add a custom delete option for backgrid rows
然而,我似乎并没有让它发挥作用。这是我试过的:
var DeleteCell = Backgrid.Cell.extend({
template: _.template('<button>Delete</button>'),
events: {
"click": "deleteRow"
},
deleteRow: function (e) {
console.log("Hello");
e.preventDefault();
this.model.collection.remove(this.model);
},
render: function () {
this.el.html(this.template());
this.delegateEvents();
return this;
}
});
然后像
一样使用它var columns = [{
name: "id", // The key of the model attribute
label: "ID", // The name to display in the header
editable: false, // By default every cell in a column is editable, but *ID* shouldn't be
renderable: false,
// Defines a cell type, and ID is displayed as an integer without the ',' separating 1000s.
cell: Backgrid.IntegerCell.extend({
orderSeparator: ''
})
}, {
name: "weight",
label: "Hello",
cell: DeleteCell
},{
name: "datemeasured",
label: "DateMeasured",
// The cell type can be a reference of a Backgrid.Cell subclass, any Backgrid.Cell subclass instances like *id* above, or a string
cell: "datetime" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
},{
name: "weight",
label: "Weight",
cell: "number" // An integer cell is a number cell that displays humanized integers
}];
我得到的是一个错误:
TypeError: this.el.html is not a function
[Break On This Error]
this.el.html(this.template());
有什么建议吗?
谢谢&amp;欢呼声
答案 0 :(得分:4)
您遇到异常,因为您尝试在错误的视图属性上调用该函数。骨干视图有两种不同的方式来访问它的el,可以直接在DOM中,也可以作为jQuery对象,如下所示:
this.el
- 这是对DOM元素的直接引用。this.$el
- DOM元素的jQuery对象。重要的是要记住,您需要在append()
属性上调用html()
和$el
等函数,因为它们是jQuery函数。
答案 1 :(得分:0)
dcarson当然是正确的,但只是非常简单地拼出渲染函数的代码,我可以用它来使这个正常工作,用这个。$ el替代this.el:
render: function () {
this.$el.html(this.template());
this.delegateEvents();
return this;
}