GridField example inline/inrow Dropdown example (Changing a Persons Salutation)

  • Download Gist
GridFieldPersonSalutationDropDown.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?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,
));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.