Merge pull request #260 from sleeper/slickgrid_data_change
[#255,views/slickgrid][s]: Update the grid when a record is changed - thx to @sleeper.
This commit is contained in:
@@ -39,6 +39,7 @@ my.SlickGrid = Backbone.View.extend({
|
|||||||
this.model.records.bind('add', this.render);
|
this.model.records.bind('add', this.render);
|
||||||
this.model.records.bind('reset', this.render);
|
this.model.records.bind('reset', this.render);
|
||||||
this.model.records.bind('remove', this.render);
|
this.model.records.bind('remove', this.render);
|
||||||
|
this.model.records.bind('change', this.onRecordChanged, this)
|
||||||
|
|
||||||
var state = _.extend({
|
var state = _.extend({
|
||||||
hiddenColumns: [],
|
hiddenColumns: [],
|
||||||
@@ -58,6 +59,19 @@ my.SlickGrid = Backbone.View.extend({
|
|||||||
events: {
|
events: {
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onRecordChanged: function(record) {
|
||||||
|
// Ignore if the grid is not yet drawn
|
||||||
|
if (!this.grid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's find the row corresponding to the index
|
||||||
|
var row_index = this.grid.getData().getModelRow( record );
|
||||||
|
this.grid.invalidateRow(row_index);
|
||||||
|
this.grid.getData().updateItem(record, row_index);
|
||||||
|
this.grid.render();
|
||||||
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
@@ -129,6 +143,15 @@ my.SlickGrid = Backbone.View.extend({
|
|||||||
}
|
}
|
||||||
columns = columns.concat(tempHiddenColumns);
|
columns = columns.concat(tempHiddenColumns);
|
||||||
|
|
||||||
|
// Transform a model object into a row
|
||||||
|
function toRow(m) {
|
||||||
|
var row = {};
|
||||||
|
self.model.fields.each(function(field){
|
||||||
|
row[field.id] = m.getFieldValueUnrendered(field);
|
||||||
|
});
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
function RowSet() {
|
function RowSet() {
|
||||||
var models = [];
|
var models = [];
|
||||||
var rows = [];
|
var rows = [];
|
||||||
@@ -142,16 +165,17 @@ my.SlickGrid = Backbone.View.extend({
|
|||||||
this.getItem = function(index) { return rows[index];}
|
this.getItem = function(index) { return rows[index];}
|
||||||
this.getItemMetadata= function(index) { return {};}
|
this.getItemMetadata= function(index) { return {};}
|
||||||
this.getModel= function(index) { return models[index]; }
|
this.getModel= function(index) { return models[index]; }
|
||||||
|
this.getModelRow = function(m) { return models.indexOf(m);}
|
||||||
|
this.updateItem = function(m,i) {
|
||||||
|
rows[i] = toRow(m);
|
||||||
|
models[i] = m
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
var data = new RowSet();
|
var data = new RowSet();
|
||||||
|
|
||||||
this.model.records.each(function(doc){
|
this.model.records.each(function(doc){
|
||||||
var row = {};
|
data.push(doc, toRow(doc));
|
||||||
self.model.fields.each(function(field){
|
|
||||||
row[field.id] = doc.getFieldValueUnrendered(field);
|
|
||||||
});
|
|
||||||
data.push(doc, row);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.grid = new Slick.Grid(this.el, data, visibleColumns, options);
|
this.grid = new Slick.Grid(this.el, data, visibleColumns, options);
|
||||||
|
|||||||
@@ -101,6 +101,31 @@ test('editable', function () {
|
|||||||
}, e, view.grid);
|
}, e, view.grid);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('update', function() {
|
||||||
|
var dataset = Fixture.getDataset();
|
||||||
|
var view = new recline.View.SlickGrid({
|
||||||
|
model: dataset,
|
||||||
|
state: {
|
||||||
|
hiddenColumns:['x','lat','title'],
|
||||||
|
columnsOrder:['lon','id','z','date', 'y', 'country'],
|
||||||
|
columnsWidth:[
|
||||||
|
{column:'id',width: 250}
|
||||||
|
],
|
||||||
|
gridOptions: {editable: true},
|
||||||
|
columnsEditor: [{column: 'country', editor: Slick.Editors.Text}]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('.fixtures .test-datatable').append(view.el);
|
||||||
|
view.render();
|
||||||
|
view.grid.init();
|
||||||
|
|
||||||
|
var zbefore = view.grid.getData().getItem(1)['z'];
|
||||||
|
// Change the model at row 1
|
||||||
|
dataset.records.at(1).set('z', zbefore + 1);
|
||||||
|
equal( zbefore + 1, view.grid.getData().getItem(1)['z']);
|
||||||
|
});
|
||||||
|
|
||||||
test('renderers', function () {
|
test('renderers', function () {
|
||||||
var dataset = Fixture.getDataset();
|
var dataset = Fixture.getDataset();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user