Skip to content

Commit

Permalink
Fix datatable FormWidget unable to return save data
Browse files Browse the repository at this point in the history
f8459b4 broke the DataTable FormWidget as the formwidget previously processed data in the POST request in the form of "{fieldName}TableData[rowIndex]". This caused the referenced commit to skip over the datatable formwidget when processing the save data as it was not able to detect any data present in the request.

The TableData suffix was a holdover from the initial implementation of the Table widget a decade ago (c6eb544#diff-7c9bd4a62da5eb18d69a023cb1d7d8c04dd2958466c5a707b0c3901e4fd28cc6R554) when originally the POST data was sent using the widget's alias as its key (as the alias was set to the value of the field name). A later commit introduced a dedicated option for the fieldName (e9c7e6b), but retained the TableData suffix.

The suffix is not required, and this not only solves the current issue of the DataTable FormWidget not returning its values through the Form's getSaveData() method but also makes the request payload make more sense in general.
  • Loading branch information
LukeTowers committed Oct 26, 2024
1 parent 9e8d01c commit da053ec
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
8 changes: 1 addition & 7 deletions modules/backend/widgets/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,8 @@ public function init()
$this->dataSource = new $dataSourceClass($this->recordsKeyFrom);

if (Request::method() == 'POST' && $this->isClientDataSource()) {
if (strpos($this->fieldName, '[') === false) {
$requestDataField = $this->fieldName . 'TableData';
} else {
$requestDataField = $this->fieldName . '[TableData]';
}

// Use dot notation for request data field
$requestDataField = implode('.', HtmlHelper::nameToArray($requestDataField));
$requestDataField = implode('.', HtmlHelper::nameToArray($this->fieldName));

if (Request::exists($requestDataField)) {
// Load data into the client memory data source on POST
Expand Down
4 changes: 2 additions & 2 deletions modules/backend/widgets/table/assets/js/build-min.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ return}for(var i=0,len=this.options.columns.length;i<len;i++){var column=this.op
if(this.cellProcessors[column].onKeyDown(ev)===false){return}}if(this.navigation.onKeydown(ev)===false){return}if(this.search.onKeydown(ev)===false){return}}
Table.prototype.onFormSubmit=function(ev,data){if(data.handler==this.options.postbackHandlerName){this.unfocusTable()
if(!this.validate()){ev.preventDefault()
return}var fieldName=this.options.fieldName.indexOf('[')>-1?this.options.fieldName+'[TableData]':this.options.fieldName+'TableData'
data.options.data[fieldName]=this.dataSource.getAllData()}}
return}
data.options.data[this.options.fieldName]=this.dataSource.getAllData()}}
Table.prototype.onToolbarClick=function(ev){var target=this.getEventTarget(ev),cmd=target.getAttribute('data-cmd')
if(!cmd){return}switch(cmd){case'record-add':case'record-add-below':this.addRecord('below')
break
Expand Down
6 changes: 1 addition & 5 deletions modules/backend/widgets/table/assets/js/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,7 @@
return
}

var fieldName = this.options.fieldName.indexOf('[') > -1
? this.options.fieldName + '[TableData]'
: this.options.fieldName + 'TableData'

data.options.data[fieldName] = this.dataSource.getAllData()
data.options.data[this.options.fieldName] = this.dataSource.getAllData()
}
}

Expand Down

0 comments on commit da053ec

Please sign in to comment.