Silverstripe 3.2:如何动态添加和更新前端表单中的数据对象?
Silverstripe 3.2: How to add and update Dataobjects in a frontend form dynamically?
示例:我的会员(可以登录并更新他们的数据)拥有一项或多项资格。所以我有一个 DataObject 'Members' 和一个具有 has_one/has_many 关系的 DataObject 'Qualification'。
像这样:
class Qualification extends DataObject {
private static $db = array (
'Title' => 'Text',
'From' => 'Date',
'Till' => 'Date'
);
private static $has_one = array (
'Member' => 'Member',
);
...
class Member extends DataObject {
...
private static $has_many = array (
'Qualifications' => 'Qualification',
);
...
现在我想在前端构建一个表单,允许会员一次添加许多资格,并在同一个表单中更新现有资格。
它可能看起来像这样
Qualifikation One
Title: xxx (textfield) From: xxx (datefield) Till: xxx
(datefield)
Qualifikation Two
Title: xxx (textfield) From: xxx (datefield) Till: xxx
(datefield)
+ add qualifications
最好的方法是什么?
我可以使用 jQuery 动态添加字段,如下所示:http://jsfiddle.net/nzYAW/
但是我该如何处理更新并将它们添加到数据库中。我尝试的每件事都非常复杂和混乱,所以我想也许其他人有一个我现在看不到的想法。我们将不胜感激!
我用3dgoo的方案解决了我的问题。我在前端表单中使用 GridField,其中包含 GridField extension module 以及组件 GridFieldEditableColumns
和 GridFieldAddNewInlineButton
。这是一个例子:
public function MyForm() {
$config = GridFieldConfig::create();
$config->addComponent(new GridFieldButtonRow('before'));
$config->addComponent(new GridFieldEditableColumns());
$config->addComponent(new GridFieldAddNewInlineButton());
$gridField = GridField::create('Qualifications', 'Qualifications', Qualification::get()->filter(array('MemberID' => Member::currentUserID()))),$config);
$fields = new FieldList(
.... here goes some other Fields like Textfields ...
TextField::create('MyTextField'),
CheckboxField::create('MyCheckboxField'),
$gridField,
);
$actions = new FieldList(
FormAction::create('myAction','save'),
FormAction::create('myOtherAction','save and next')
);
$form = new Form($this, __FUNCTION__, $fields, $actions);
$form->loadDataFrom(Member::get()->byID(Member::currentUserID()));
return $form;
}
public function myAction($data, $form) {
$member = Member::get()->byId(Member::currentUserID());
$form->saveInto($member);
$member->write();
}
我还必须将 canView、canEdit、canCreate 和 canDelete 函数添加到 Qualification DataObject 以允许对其进行编辑和显示。
示例:我的会员(可以登录并更新他们的数据)拥有一项或多项资格。所以我有一个 DataObject 'Members' 和一个具有 has_one/has_many 关系的 DataObject 'Qualification'。
像这样:
class Qualification extends DataObject {
private static $db = array (
'Title' => 'Text',
'From' => 'Date',
'Till' => 'Date'
);
private static $has_one = array (
'Member' => 'Member',
);
...
class Member extends DataObject {
...
private static $has_many = array (
'Qualifications' => 'Qualification',
);
...
现在我想在前端构建一个表单,允许会员一次添加许多资格,并在同一个表单中更新现有资格。
它可能看起来像这样
Qualifikation One
Title: xxx (textfield) From: xxx (datefield) Till: xxx (datefield)
Qualifikation Two
Title: xxx (textfield) From: xxx (datefield) Till: xxx (datefield)
+ add qualifications
最好的方法是什么?
我可以使用 jQuery 动态添加字段,如下所示:http://jsfiddle.net/nzYAW/
但是我该如何处理更新并将它们添加到数据库中。我尝试的每件事都非常复杂和混乱,所以我想也许其他人有一个我现在看不到的想法。我们将不胜感激!
我用3dgoo的方案解决了我的问题。我在前端表单中使用 GridField,其中包含 GridField extension module 以及组件 GridFieldEditableColumns
和 GridFieldAddNewInlineButton
。这是一个例子:
public function MyForm() {
$config = GridFieldConfig::create();
$config->addComponent(new GridFieldButtonRow('before'));
$config->addComponent(new GridFieldEditableColumns());
$config->addComponent(new GridFieldAddNewInlineButton());
$gridField = GridField::create('Qualifications', 'Qualifications', Qualification::get()->filter(array('MemberID' => Member::currentUserID()))),$config);
$fields = new FieldList(
.... here goes some other Fields like Textfields ...
TextField::create('MyTextField'),
CheckboxField::create('MyCheckboxField'),
$gridField,
);
$actions = new FieldList(
FormAction::create('myAction','save'),
FormAction::create('myOtherAction','save and next')
);
$form = new Form($this, __FUNCTION__, $fields, $actions);
$form->loadDataFrom(Member::get()->byID(Member::currentUserID()));
return $form;
}
public function myAction($data, $form) {
$member = Member::get()->byId(Member::currentUserID());
$form->saveInto($member);
$member->write();
}
我还必须将 canView、canEdit、canCreate 和 canDelete 函数添加到 Qualification DataObject 以允许对其进行编辑和显示。