Skip to content

Commit

Permalink
Improve DataTable form widget, fixes vague error in RecordFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Mar 24, 2015
1 parent 6820b12 commit 3c2494a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
51 changes: 46 additions & 5 deletions modules/backend/formwidgets/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@
*/
class DataTable extends FormWidgetBase
{
//
// Configurable properties
//

/**
* {@inheritDoc}
* @var string Table size
*/
protected $defaultAlias = 'datatable';
public $size = 'large';

/**
* @var string Table size
* @var bool Allow rows to be sorted
* @todo Not implemented...
*/
public $rowSorting = false;

//
// Object properties
//

/**
* {@inheritDoc}
*/
protected $size = 'large';
protected $defaultAlias = 'datatable';

/**
* @var Backend\Widgets\Table Table widget
Expand All @@ -34,7 +48,11 @@ class DataTable extends FormWidgetBase
*/
public function init()
{
$this->size = $this->getConfig('size', $this->size);
$this->fillFromConfig([
'size',
'rowSorting',
]);

$this->table = $this->makeTableWidget();
$this->table->bindToController();
}
Expand Down Expand Up @@ -64,6 +82,23 @@ public function prepareVars()
$this->populateTableWidget();
$this->vars['table'] = $this->table;
$this->vars['size'] = $this->size;
$this->vars['rowSorting'] = $this->rowSorting;
}

/**
* {@inheritDoc}
*/
public function getLoadValue()
{
$value = (array) parent::getLoadValue();

// Sync the array keys as the ID to make the
// table widget happy!
foreach ($value as $key => $_value) {
$value[$key] = ['id' => $key] + (array) $_value;
}

return $value;
}

/**
Expand All @@ -82,6 +117,12 @@ public function getSaveValue($value)
$result += $records;
}

// We should be dealing with a simple array, so
// strip out the id columns in the final array.
foreach ($result as $key => $_result) {
unset($result[$key]['id']);
}

return $result;
}

Expand Down
15 changes: 2 additions & 13 deletions modules/backend/formwidgets/RecordFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public function init()
}

/**
* Returns the value as a relation object from the model,
* Returns the model of a relation type,
* supports nesting via HTML array.
* @return Relation
*/
protected function getRelationObject()
protected function getRelationModel()
{
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);

Expand All @@ -116,17 +116,6 @@ protected function getRelationObject()
]));
}

return $model->{$attribute}();
}

/**
* Returns the model of a relation type,
* supports nesting via HTML array.
* @return Relation
*/
protected function getRelationModel()
{
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
return $model->makeRelation($attribute);
}

Expand Down
11 changes: 6 additions & 5 deletions modules/backend/widgets/table/assets/js/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@

dataContainer.setAttribute('type', 'hidden')
dataContainer.setAttribute('data-container', 'data-container')
dataContainer.value = records[i][columnName] !== undefined ?
records[i][columnName] :
""
dataContainer.value = records[i][columnName] !== undefined
? records[i][columnName]
: ""

cellContentContainer.setAttribute('class', 'content-container')

Expand Down Expand Up @@ -391,9 +391,10 @@

Table.prototype.fetchRecords = function(onSuccess) {
this.dataSource.getRecords(
this.navigation.getPageFirstRowOffset(),
this.navigation.getPageFirstRowOffset(),
this.options.recordsPerPage,
onSuccess)
onSuccess
)
}

Table.prototype.updateScrollbar = function() {
Expand Down

0 comments on commit 3c2494a

Please sign in to comment.