how to get option values for BackGrid Select2cell from database

1k views Asked by At

Backgrid example code: This is the code given in the backgrid website, similarly i have to get the values from the database to populate the select2cell option values inside the grid

 var data = new Backbone.Collection([{number: 5, another: 1}]);
    // Just like SelectCell, Select2Cell supports option lists and groups

    var numbers = [{name: 10, values: [
    [1, 1], [2, 2], [3, 3], [4, 4], [5, 5],
    [6, 6], [7, 7], [8, 8], [9, 9], [10, 10]
    ]}];
    var MySelect2Cell = Backgrid.Extension.Select2Cell.extend({
     select2Options: {

    // any options specific to `select2` goes here`enter code here`

    },

    optionValues: numbers

    });


    var grid = new Backgrid.Grid({

    columns: [{
    name: "number",
    cell: MySelect2Cell
      }, 

    {
      name: "another",
      cell: MySelect2Cell
     }],

    collection: data

     });


     $("#select2-cell-example-result").append(grid.render().el);

Like this i have to get the options values from the database

2

There are 2 answers

1
Y.H Wong On

Here are some examples from the official documentation.

0
christinedraper On

The Cell is just a Backbone View, and so you have access to the model. So you can do something like:

var data = new Backbone.Collection([{number: 5, another: 1, options: optionsForNumber5}]);

var MySelect2Cell = Backgrid.Extension.Select2Cell.extend({
  select2Options: {
    return this.model.get("options");
  }
});

You also have access to the column model, so you can do the following:

var MySelect2Cell = Backgrid.Extension.Select2Cell.extend({
  select2Options: {
    if (this.column.get("name") === "number"){
      return optionsForName;
    }
    if (this.column.get("name") === "another"){
      return optionsForAnother;
    }
  }

Some combination of these two may give you what you need.