diff --git a/modules/backend/classes/TableDataSourceBase.php b/modules/backend/classes/TableDataSourceBase.php index 867747b54..21f5e273c 100644 --- a/modules/backend/classes/TableDataSourceBase.php +++ b/modules/backend/classes/TableDataSourceBase.php @@ -65,14 +65,14 @@ abstract class TableDataSourceBase /** * Returns a set of records from the data source. * @param integer $count Specifies the number of records to return. - * @return array Returns the records. + * @return array Returns the records. * If there are no more records, returns an empty array. */ public function readRecords($count = 10) { $result = $this->getRecords($this->offset, $count); $this->offset += count($result); - + return $result; } } \ No newline at end of file diff --git a/modules/backend/formwidgets/DataTable.php b/modules/backend/formwidgets/DataTable.php new file mode 100644 index 000000000..2b00ce688 --- /dev/null +++ b/modules/backend/formwidgets/DataTable.php @@ -0,0 +1,137 @@ +size = $this->getConfig('size', $this->size); + $this->table = $this->makeTableWidget(); + $this->table->bindToController(); + } + + /** + * @return Backend\Widgets\Table The table to be displayed. + */ + public function getTable() + { + return $this->table; + } + + /** + * {@inheritDoc} + */ + public function render() + { + $this->prepareVars(); + return $this->makePartial('datatable'); + } + + /** + * Prepares the list data + */ + public function prepareVars() + { + $this->populateTableWidget(); + $this->vars['table'] = $this->table; + $this->vars['size'] = $this->size; + } + + /** + * {@inheritDoc} + */ + public function getSaveData($value) + { + $dataSource = $this->table->getDataSource(); + + $result = []; + while ($records = $dataSource->readRecords()) { + $result += $records; + } + + return $result; + } + + /* + * Populate data + */ + protected function populateTableWidget() + { + $dataSource = $this->table->getDataSource(); + $records = $this->getLoadData() ?: []; + traceLog($records); + $dataSource->initRecords((array) $records); + } + + protected function makeTableWidget() + { + $config = $this->makeConfig((array) $this->config); + $config->dataSource = 'client'; + $config->alias = $this->alias . 'Table'; + + $table = new Table($this->controller, $config); + + $table->bindEvent('table.getDropdownOptions', [$this, 'getDataTableOptions']); + + return $table; + } + + /** + * Looks at the model for getXXXDataTableOptions or getDataTableOptions methods + * to obtain values for autocomplete field types. + * @param string $field Table field name + * @param string $data Data for the entire table + * @return array + */ + public function getDataTableOptions($field, $data) + { + $methodName = 'get'.studly_case($this->fieldName).'DataTableOptions'; + + if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getDataTableOptions')) { + throw new ApplicationException(Lang::get('backend::lang.model.missing_method', ['class' => get_class($this->model), 'method' => 'getDataTableOptions'])); + } + + if ($this->model->methodExists($methodName)) { + $result = $this->model->$methodName($field, $data); + } + else { + $result = $this->model->getDataTableOptions($this->fieldName, $field, $data); + } + + if (!is_array($result)) { + $result = []; + } + + return $result; + } + +} diff --git a/modules/backend/formwidgets/datatable/partials/_datatable.htm b/modules/backend/formwidgets/datatable/partials/_datatable.htm new file mode 100644 index 000000000..cb73e4851 --- /dev/null +++ b/modules/backend/formwidgets/datatable/partials/_datatable.htm @@ -0,0 +1,8 @@ +
+ + render() ?> + +
+