Add Element in Backgrid Column

479 Views Asked by At

Hello guys I am using backgrid to render my table. I can't figure out how to add a element in the column. Can somebody tell me how can I do that.

1

There are 1 best solutions below

4
Francois Borgies On

After see the docs/api here: http://backgridjs.com/ You can achieve that with somethings like :

// 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({});
});

You can probably add a row/element with this code : grid.collection.add({});

EDIT

So Backgrid.js use Backbone.js to add a row with this method : insertRow( model, collection, options) (http://wyuenho.github.io/backgrid/api/index.html#!/api/Backgrid.Body)

When called directly, it accepts a model or an array of models and an option hash just like Backbone.Collection#add and delegates to it. Once the model is added, a new row is inserted into the body and automatically rendered.

Then we have :

var ships = new Backbone.Collection;

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

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

Hopes this help you ;)