大家好,我正在使用backgrid渲染表格。我不知道如何在列中添加元素。有人可以告诉我该怎么做。

最佳答案

之后在此处查看docs / api:http://backgridjs.com/
您可以通过以下方式实现此目的:

// Since 0.3.0, you can listen to the `backgrid:next` to see if a cell
// movement was out of bound, if yes, you can insert a new row.

// A movement is only out of bound when the user was trying to go beyond
// the last row.
grid.collection.listenTo("backgrid:next", function (i, j, outOfBound) {
  // this will add a row using the collection's model too
  if (outOfBound) grid.collection.add({});
});


您可能可以使用以下代码添加行/元素:grid.collection.add({});

编辑

因此Backgrid.js使用Backbone.js通过以下方法添加一行:insertRow( model, collection, options)http://wyuenho.github.io/backgrid/api/index.html#!/api/Backgrid.Body

直接调用时,它像Backbone.Collection#add一样接受模型或模型数组以及选项哈希,并委托给它。添加模型后,新行将插入到主体中并自动呈现。

然后我们有:

var ships = new Backbone.Collection;

ships.on("add", function(ship) {
  alert("Ahoy " + ship.get("name") + "!");
});

ships.add([
  {name: "Flying Dutchman"},
  {name: "Black Pearl"}
]);


希望对您有帮助;)

09-11 20:48