Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Zauberfisch/5078232 to your computer and use it in GitHub Desktop.
Save Zauberfisch/5078232 to your computer and use it in GitHub Desktop.
GridField example inline/inrow Dropdown example (Changing a Persons Salutation)
<?php
class GridFieldPersonSalutationDropDown implements GridField_ColumnProvider, GridField_URLHandler {
public function augmentColumns($gridField, &$columns) {
$columns[] = 'Salutation';
}
public function getColumnsHandled($gridField) {
return array('Salutation');
}
public function getColumnContent($gridField, $record, $columnName) {
if ($record->canEdit()) {
// TODO exclude JS into external file
// TODO JS success and error messages
Requirements::customScript("
(function($) {
$('.ss-gridfield .saveSalutation').entwine({
onclick: function() {
var val = this.closest('td').find('select').val()
$.ajax({
url: this.data('url'),
data: { value: val },
type: 'POST',
success: function(response) { console.log('yay') },
error: function(response) { console.log('oh noez') }
})
return false
}
})
})(jQuery)
");
return DropdownField::create('Salutation', '', array(
'Male' => _t('Person.SalutationMale', 'Mr'),
'Female' => _t('Person.SalutationFemale', 'Mrs'),
))->FieldHolder() . sprintf(
'<button class="%s" data-url="%s">%s</button>',
'saveSalutation',
Controller::join_links($gridField->Link('changeSalutation'), $record->ID),
_t('GridFieldPersonSalutationDropDown.Save', 'Save')
);
} elseif ($record->canView()) {
// TODO return a readonly field
return '';
}
return '';
}
public function getColumnAttributes($gridField, $record, $columnName) {
return array('class' => 'col-Salutation');
}
public function getColumnMetadata($gridField, $columnName) {
return array('title' => _t('GridFieldPersonSalutationDropDown.ColTitle', 'Salutation'));
}
public function getURLHandlers($gridField) {
return array(
'changeSalutation/$ID' => 'changeSalutation',
);
}
public function changeSalutation(GridField $g, SS_HTTPRequest $r) {
$record = $g->getList()->byId((int)$r->param('ID'));
if (!$record->canEdit()) {
$this->httpError(403);
}
$record->Salutation = Convert::raw2sql($r->postVar('value'));
$record->write();
return Convert::raw2json(array(
'success' => true,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment