Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Readonly GridField Column


Go to End


1930 Views

Avatar
keyvan

Community Member, 1 Post

5 September 2013 at 6:26pm

I like to make a Column in a GridFiled readonly also in the following edit-form has same filed as readonly.
The idea is to show a list of cameras but name of camera and last frame were readonly.

Here is my code:

class CameraObject extends DataObject
{
public static $db = array(
'Publish' => "Enum('No, Yes', 'No')",
'Name'=>'Varchar(200)',
'Alias'=>'Varchar(200)',
);

public static $has_one = array(
'LastFrame'=>'Image',
);
}

class CameraPage_Controller extends Page_Controller
{

static $allowed_actions = array('index', 'AllCameras');

public function init()
{
parent::init();
}

public function index(SS_HTTPRequest $request)
{
$this->Content = $this->AllCameras();
return $this->render();
}

public function AllCameras()
{
$records = CameraObject::get();

$config = GridFieldConfig::create()->addComponents(
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDetailForm()
);

$gridField = new GridField('cameras', 'Cameras', $records, $config);

$dataColumns = $config->getComponentByType('GridFieldDataColumns');
$dataColumns->setDisplayFields(array(
'Publish' => 'Publish',
'Name' => 'Name',
'Alias' => 'Alias',
'LastFrame' => 'LastFrame',
));

$form = new Form($this, "AllCameras", new FieldList($gridField), new FieldList());

return $form;
}
}