Backgrid示例代码:这是backgrid网站中给出的代码,类似地,我必须从数据库中获取值,以在网格中填充select2cell选项值
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);
像这样,我必须从数据库中获取选项值
以下是官方文档中的一些示例。
Cell只是一个骨干视图,因此您可以访问模型。所以你可以做一些类似的事情:
var data = new Backbone.Collection([{number: 5, another: 1, options: optionsForNumber5}]);
var MySelect2Cell = Backgrid.Extension.Select2Cell.extend({
select2Options: {
return this.model.get("options");
}
});
您还可以访问列模型,因此可以执行以下操作:
var MySelect2Cell = Backgrid.Extension.Select2Cell.extend({
select2Options: {
if (this.column.get("name") === "number"){
return optionsForName;
}
if (this.column.get("name") === "another"){
return optionsForAnother;
}
}
这两者的结合可能会给你所需要的。