From d050cff3c67bde4eae6e12abdba697045911de0d Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Tue, 5 Aug 2014 17:48:33 +1000 Subject: [PATCH] Improved Grid widget Grid widget now supports custom toolbar Remove row is triggered as "data changed" --- modules/backend/widgets/Grid.php | 21 +++++++++++++------ .../widgets/grid/assets/js/datagrid.js | 11 ++++++---- .../widgets/grid/partials/_button_delete.htm | 6 ++++++ .../widgets/grid/partials/_button_insert.htm | 6 ++++++ .../widgets/grid/partials/_toolbar.htm | 14 ++----------- 5 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 modules/backend/widgets/grid/partials/_button_delete.htm create mode 100644 modules/backend/widgets/grid/partials/_button_insert.htm diff --git a/modules/backend/widgets/Grid.php b/modules/backend/widgets/Grid.php index b8d3cc05f..ec65235b8 100644 --- a/modules/backend/widgets/Grid.php +++ b/modules/backend/widgets/Grid.php @@ -100,10 +100,10 @@ class Grid extends WidgetBase */ public function prepareVars() { + $this->vars['toolbarWidget'] = $this->makeToolbarWidget(); $this->vars['columnHeaders'] = $this->getColumnHeaders(); $this->vars['columnDefinitions'] = $this->getColumnDefinitions(); $this->vars['columnWidths'] = $this->getColumnWidths(); - $this->vars['toolbarWidget'] = $this->makeToolbarWidget(); $this->vars['showHeader'] = $this->showHeader; $this->vars['allowInsert'] = $this->allowInsert; @@ -120,12 +120,14 @@ class Grid extends WidgetBase if ($this->disableToolbar) return; - $toolbarConfig = $this->makeConfig([ - 'alias' => $this->alias . 'Toolbar', + $defaultConfig = [ 'buttons' => $this->getViewPath('_toolbar.htm'), - ]); + ]; + $toolbarConfig = $this->makeConfig($this->getConfig('toolbar', $defaultConfig)); + $toolbarConfig->alias = $this->alias . 'Toolbar'; $toolbarWidget = $this->makeWidget('Backend\Widgets\Toolbar', $toolbarConfig); + $toolbarWidget->vars['gridWidget'] = $this; $toolbarWidget->vars['allowInsert'] = $this->allowInsert; $toolbarWidget->vars['allowRemove'] = $this->allowRemove; return $toolbarWidget; @@ -153,8 +155,15 @@ class Grid extends WidgetBase * Changes array, each array item will contain: * ['rowData' => [...], 'keyName' => 'changedColumn', 'oldValue' => 'was', 'newValue' => 'is'] */ - $changes = post('changes'); - $this->fireEvent('grid.dataChanged', [$changes]); + $changes = post('grid_changes'); + + /* + * Action will be either: + * "remove", "alter", "empty", "edit", "populateFromArray", "autofill", "paste" + */ + $action = post('grid_action'); + + $this->fireEvent('grid.dataChanged', [$action, $changes]); } public function onDataSource() diff --git a/modules/backend/widgets/grid/assets/js/datagrid.js b/modules/backend/widgets/grid/assets/js/datagrid.js index 4bc799640..7174be0a4 100644 --- a/modules/backend/widgets/grid/assets/js/datagrid.js +++ b/modules/backend/widgets/grid/assets/js/datagrid.js @@ -55,7 +55,7 @@ * [ [row, prop, oldVal, newVal], ... ]. * * source - is one of the strings: "alter", "empty", "edit", "populateFromArray", - * "loadData", "autofill", "paste". + * "loadData", "autofill", "paste", "remove". */ self.$el.trigger('datagrid.change', [changes, source]) }, @@ -87,7 +87,7 @@ * Event to update the data locker */ this.$dataLocker = $(this.options.dataLocker) - self.$el.on('datagrid.change', function(event, eventData) { + self.$el.on('datagrid.change', function(event) { if (!self.gridInstance) return self.$dataLocker.val(JSON.stringify(self.getData())) }) @@ -125,7 +125,7 @@ if (changeData.length > 0) { self.$el.request(self.options.changeHandler, { - data: { changes: changeData } + data: { grid_action: source, grid_changes: changeData } }) } }) @@ -279,6 +279,9 @@ if (!index && index !== 0) return + var changes = [[index, null, null, 'DELETE']] + this.$el.trigger('datagrid.change', [changes, 'remove']) + this.gridInstance.alter('remove_row', index) this.updateUi() } @@ -437,7 +440,7 @@ $el.append($btn); $btn.on('mouseup', function () { - instance.alter("remove_row", row); + instance.rootElement.dataGrid('removeRow', row) $(window).trigger('oc.updateUi') }); } diff --git a/modules/backend/widgets/grid/partials/_button_delete.htm b/modules/backend/widgets/grid/partials/_button_delete.htm new file mode 100644 index 000000000..5ca95b948 --- /dev/null +++ b/modules/backend/widgets/grid/partials/_button_delete.htm @@ -0,0 +1,6 @@ + + Delete Row + \ No newline at end of file diff --git a/modules/backend/widgets/grid/partials/_button_insert.htm b/modules/backend/widgets/grid/partials/_button_insert.htm new file mode 100644 index 000000000..a911c4828 --- /dev/null +++ b/modules/backend/widgets/grid/partials/_button_insert.htm @@ -0,0 +1,6 @@ + + Insert Row + \ No newline at end of file diff --git a/modules/backend/widgets/grid/partials/_toolbar.htm b/modules/backend/widgets/grid/partials/_toolbar.htm index 88a64a12e..60fd50f98 100644 --- a/modules/backend/widgets/grid/partials/_toolbar.htm +++ b/modules/backend/widgets/grid/partials/_toolbar.htm @@ -1,19 +1,9 @@
- - Insert Row - + makePartial('button_insert') ?> - - Delete Row - + makePartial('button_delete') ?>