From 24ee9fa0196cbd3f9e0f273df1eab1cf4562c4b7 Mon Sep 17 00:00:00 2001 From: Frederick Ros Date: Sun, 21 Oct 2012 01:56:02 +0200 Subject: [PATCH] Update the grid when a record is changed Fixes #255 --- src/view.slickgrid.js | 34 +++++++++++++++++++++++++++++----- test/view.slickgrid.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/view.slickgrid.js b/src/view.slickgrid.js index bc98f09a..d9f61deb 100644 --- a/src/view.slickgrid.js +++ b/src/view.slickgrid.js @@ -39,6 +39,7 @@ my.SlickGrid = Backbone.View.extend({ this.model.records.bind('add', this.render); this.model.records.bind('reset', this.render); this.model.records.bind('remove', this.render); + this.model.records.bind('change', this.onRecordChanged, this) var state = _.extend({ hiddenColumns: [], @@ -58,6 +59,19 @@ my.SlickGrid = Backbone.View.extend({ 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() { var self = this; @@ -129,6 +143,15 @@ my.SlickGrid = Backbone.View.extend({ } 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() { var models = []; var rows = []; @@ -142,16 +165,17 @@ my.SlickGrid = Backbone.View.extend({ this.getItem = function(index) { return rows[index];} this.getItemMetadata= function(index) { return {};} 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(); this.model.records.each(function(doc){ - var row = {}; - self.model.fields.each(function(field){ - row[field.id] = doc.getFieldValueUnrendered(field); - }); - data.push(doc, row); + data.push(doc, toRow(doc)); }); this.grid = new Slick.Grid(this.el, data, visibleColumns, options); diff --git a/test/view.slickgrid.test.js b/test/view.slickgrid.test.js index f3cf05f3..0ff639b1 100644 --- a/test/view.slickgrid.test.js +++ b/test/view.slickgrid.test.js @@ -101,6 +101,31 @@ test('editable', function () { }, 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 () { var dataset = Fixture.getDataset();