首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现backgrid的逐行删除

如何实现backgrid的逐行删除
EN

Stack Overflow用户
提问于 2013-07-12 17:53:29
回答 2查看 4.9K关注 0票数 4

我一直在尝试为背景网格行实现一个“删除”按钮。这里似乎描述了一种解决方案:

How to add a custom delete option for backgrid rows

然而,我似乎不能让它工作。这是我尝试过的:

代码语言:javascript
复制
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;
    }
});

然后像这样使用它

代码语言:javascript
复制
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
    }];

我得到的是一个错误:

代码语言:javascript
复制
TypeError: this.el.html is not a function
[Break On This Error]   

this.el.html(this.template());

有什么建议吗?

谢谢&干杯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-12 20:07:08

因为您试图在错误的视图属性上调用函数,所以会出现异常。Backbone视图有两种不同的方式来访问它的el,或者直接在DOM中访问,或者作为jQuery对象访问,如下所示:

  1. this.el -这是对DOM element.
  2. this.$el的直接引用- DOM元素的jQuery对象。

重要的是要记住,您需要在$el属性上调用像append()html()这样的函数,因为它们是jQuery函数。

票数 4
EN

Stack Overflow用户

发布于 2015-02-05 23:36:00

dcarson当然是正确的,但我要非常清楚地说明渲染函数的代码,我可以使用它来正确地工作,使用这个。$el替换了this.el:

代码语言:javascript
复制
render: function () {
    this.$el.html(this.template());
    this.delegateEvents();
    return this;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17612191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档