在Backgrid列中添加元素

时间:2016-02-25 07:04:39

标签: javascript jquery backgrid

大家好我正在使用backgrid来渲染我的表格。我无法弄清楚如何在列中添加元素。有人能告诉我我该怎么做。

1 个答案:

答案 0 :(得分:0)

在此处查看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"}
]);

希望这能帮到你;)