Jquery Silverstripe 3.2:如何在前端表单中动态添加和更新数据对象?
示例:我有一些成员(可以登录并更新其数据)具有一个或多个资格。因此,我有一个DataObject“Members”和一个DataObject“Qualification”,具有has_one/has_many关系 大概是这样的:Jquery Silverstripe 3.2:如何在前端表单中动态添加和更新数据对象?,jquery,forms,silverstripe,data-objects,Jquery,Forms,Silverstripe,Data Objects,示例:我有一些成员(可以登录并更新其数据)具有一个或多个资格。因此,我有一个DataObject“Members”和一个DataObject“Qualification”,具有has_one/has_many关系 大概是这样的: class Qualification extends DataObject { private static $db = array ( 'Title' => 'Text', 'From' => 'Date',
class Qualification extends DataObject {
private static $db = array (
'Title' => 'Text',
'From' => 'Date',
'Till' => 'Date'
);
private static $has_one = array (
'Member' => 'Member',
);
现在,我想在前端构建一个表单,允许成员一次添加多个资格,并在同一表单中更新现有资格
它可能看起来像这样
Qualifikation One
标题:xxx(文本字段)从:xxx(日期字段)到:xxx
(日期字段)
资格认证二
标题:xxx(文本字段)从:xxx(日期字段)到:xxx
(日期字段)
+添加资格
最好的方法是什么
我可以使用jQuery动态添加字段,如下所示:
但是我如何处理更新并将它们添加到数据库中呢。我试过的每件事都非常复杂和混乱,所以我想也许有人有一个想法,我现在看不到。我们将感谢您的帮助 我用3dgoo解决了我的问题。我将前端表单中的GridField与组件
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函数添加到限定数据对象中,以允许编辑和显示它。我以前从未这样做过,但是我想尝试在前端使用
GridFieldEditableColumns
和GridFieldAddNewInlineButton
组件的GridField
。@iraia您只需要一个操作来读取post并将输入属性名称更改为可以作为数组读取的格式:。然后将“keys”数据保存到dataobject。当然,你可以有一个隐藏的字段来提交你的输入计数,然后连接起来得到一个字段,但是这样你就可以很容易地迭代提交的数据了。谢谢大家。我将尝试这两种方法,并在此处报告结果。:)好啊我从GridField扩展模块开始,它非常简单,并且按照预期工作,所以我一直保持这种方式。不过谢谢你们两位@iraia您能否提交自己的解决方案作为答案。或者至少是重要的部分:)
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();
}