OctoberCMS:如何设置转发器小部件自动加载/填充新记录的值
OctoberCMS: how to set repeater widget autoload / populate value for new records
我可以将转发器小部件设置为在表单打开时自动加载吗?
它有最大项目属性,它能自动加载到最大项目数
所以用户不需要点击提示按钮
重新更新
这是我的fields.yaml
fields:
nowarta:
label: 'No. Warta'
oc.commentPosition: ''
span: auto
placeholder: 'format penulisan ''No. 34 Tahun XIX'''
type: text
tanggal:
label: Tanggal
oc.commentPosition: ''
mode: date
format: 'd - F - Y'
span: right
type: datepicker
ignoreTimezone: false
renungan:
label: 'Renungan Mingguan'
oc.commentPosition: ''
span: full
type: section
renungan[judul]:
label: 'Judul Renungan'
oc.commentPosition: ''
span: storm
type: text
cssClass: 'col-sm-6 col-sm-push-0'
renungan[bahanbaca]:
label: 'Bahan Bacaan'
oc.commentPosition: ''
span: storm
type: text
cssClass: col-sm-6
renungan[isi]:
label: Renungan
oc.commentPosition: ''
span: storm
cssClass: 'col-sm-12 col-sm-push-0'
type: richeditor
size: huge
tabs:
fields:
temakebum:
label: 'Kebaktian Umum'
oc.commentPosition: ''
span: full
type: section
tab: 'Kebaktian Umum & Komisi'
temakebum[tema]:
tab: 'Kebaktian Umum & Komisi'
label: Tema
oc.commentPosition: ''
span: storm
cssClass: col-sm-11
type: text
temakebum[bacaan]:
label: 'Bahan Bacaan'
oc.commentPosition: ''
span: storm
cssClass: col-sm-6
type: text
tab: 'Kebaktian Umum & Komisi'
temakebum[pujian]:
label: Pujian
oc.commentPosition: ''
span: storm
cssClass: col-sm-6
type: text
tab: 'Kebaktian Umum & Komisi'
kebum:
label: ''
oc.commentPosition: ''
prompt: 'Tambah Data'
maxItems: '3'
span: full
type: repeater
tab: 'Kebaktian Umum & Komisi'
form:
fields:
jeniskeb:
label: 'Jenis Kebaktian'
oc.commentPosition: ''
span: storm
cssClass: col-sm-4
type: dropdown
options: jenisKeb
khotbah:
label: Pengkhotbah
oc.commentPosition: ''
span: storm
cssClass: col-sm-4
placeholder: ''
type: text
dd($表单->字段)
array:6 [▼
"nowarta" => array:5 [▶]
"tanggal" => array:7 [▶]
"renungan" => array:4 [▶]
"renungan[judul]" => array:5 [▶]
"renungan[bahanbaca]" => array:5 [▶]
"renungan[isi]" => array:6 [▶]
]
这就是我在控制器中所做的,如您所愿..
class WartaRutin extends Controller
{
public $implement = ['Backend\Behaviors\ListController','Backend\Behaviors\FormController','Backend\Behaviors\ReorderController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Mismaiti.MyWarta', 'main-menu-item', 'side-menu-rutin');
}
public function formExtendFieldsBefore($form) {
// we are checking we are creating record or updating
// or even we can use $form->context here but I used $model->id
// if $form->context == 'update'
// if $form->context == 'create'
if(is_null($form->model->id)) {
// create time
$iteration = $form->fields['kebum']['maxItems'];
if(is_numeric($iteration) && $iteration > 0) {
$emptyFields = [];
while($iteration > 0) {
$emptyFields[] = ['jeniskeb' => ''];
$iteration--;
}
$form->model->kebum = $emptyFields;
}
}
}
}
它 return 当我尝试执行时出错——我已将所有 repeater_data 替换为 kebum
"Undefined index: kebum" on line 30 of C:\xampp\htdocs\mywarta\plugins\mismaiti\mywarta\controllers\WartaRutin.php
我是不是漏掉了什么..?
嗯,我们可以使用 one trick
,因为您可能只在 create time
需要它,或者您可以在 update time as well
、
扩展它
我用了字段repeater_data
你可以用你的字段替换它field
fields.yaml
fields:
... other fields ...
repeater_data:
label: Repeater
oc.commentPosition: ''
prompt: 'Add new item'
maxItems: '5'
span: auto
type: repeater
form:
fields:
title:
label: Text
oc.commentPosition: ''
span: auto
type: text
category:
label: Dropdown
oc.commentPosition: ''
options:
1: 'Item 1'
2: 'Item 2'
span: auto
type: dropdown
controller
在您的 controller
中,您需要添加此 method and code
public function formExtendFieldsBefore($form) {
// we are checking we are creating record or updating
// or even we can use $form->context here but I used $model->id
// if $form->context == 'update'
// if $form->context == 'create'
if(is_null($form->model->id)) {
// create time **logic**
$iteration = $form->fields['repeater_data']['maxItems'];
if(is_numeric($iteration) && $iteration > 0) {
$emptyFields = [];
while($iteration > 0) {
// here you need to provide at least one field
// which you used in repeater I used `title`
// and let it be empty or may be some default value
$emptyFields[] = ['title' => ''];
$iteration--;
}
// dummy fields assignment to current form model
$form->model->repeater_data = $emptyFields;
}
// **logic**
}
else {
// if you need to adjust data in update time
// you need to find difference and then need to add
// additional dummy data as we are adding above
}
}
有2个场景 create time
和update time
Create time
当用户创建记录时,我们将添加dummy fields
,以便转发器按原样显示它们already there
,这是使用maxItems property
完成的] field so its fully dynamic
,现在用户不需要按那个按钮。
Update time ( Suppose maxItems=5)
现在 2nd scenario
我们有 else 条件 如果你希望你可以添加一些逻辑并在那里做你的事情,假设 用户可以只添加一次 2 条记录 所以下次我们需要添加 3 dummy fields
(2 filled + 3 dummy = total 5 as maxItem) 所以你可以 calculate there
和 add it from else part
.
希望对您有所帮助,如果您发现任何困难,请发表评论。
我可以将转发器小部件设置为在表单打开时自动加载吗?
它有最大项目属性,它能自动加载到最大项目数
所以用户不需要点击提示按钮
重新更新
这是我的fields.yaml
fields:
nowarta:
label: 'No. Warta'
oc.commentPosition: ''
span: auto
placeholder: 'format penulisan ''No. 34 Tahun XIX'''
type: text
tanggal:
label: Tanggal
oc.commentPosition: ''
mode: date
format: 'd - F - Y'
span: right
type: datepicker
ignoreTimezone: false
renungan:
label: 'Renungan Mingguan'
oc.commentPosition: ''
span: full
type: section
renungan[judul]:
label: 'Judul Renungan'
oc.commentPosition: ''
span: storm
type: text
cssClass: 'col-sm-6 col-sm-push-0'
renungan[bahanbaca]:
label: 'Bahan Bacaan'
oc.commentPosition: ''
span: storm
type: text
cssClass: col-sm-6
renungan[isi]:
label: Renungan
oc.commentPosition: ''
span: storm
cssClass: 'col-sm-12 col-sm-push-0'
type: richeditor
size: huge
tabs:
fields:
temakebum:
label: 'Kebaktian Umum'
oc.commentPosition: ''
span: full
type: section
tab: 'Kebaktian Umum & Komisi'
temakebum[tema]:
tab: 'Kebaktian Umum & Komisi'
label: Tema
oc.commentPosition: ''
span: storm
cssClass: col-sm-11
type: text
temakebum[bacaan]:
label: 'Bahan Bacaan'
oc.commentPosition: ''
span: storm
cssClass: col-sm-6
type: text
tab: 'Kebaktian Umum & Komisi'
temakebum[pujian]:
label: Pujian
oc.commentPosition: ''
span: storm
cssClass: col-sm-6
type: text
tab: 'Kebaktian Umum & Komisi'
kebum:
label: ''
oc.commentPosition: ''
prompt: 'Tambah Data'
maxItems: '3'
span: full
type: repeater
tab: 'Kebaktian Umum & Komisi'
form:
fields:
jeniskeb:
label: 'Jenis Kebaktian'
oc.commentPosition: ''
span: storm
cssClass: col-sm-4
type: dropdown
options: jenisKeb
khotbah:
label: Pengkhotbah
oc.commentPosition: ''
span: storm
cssClass: col-sm-4
placeholder: ''
type: text
dd($表单->字段)
array:6 [▼
"nowarta" => array:5 [▶]
"tanggal" => array:7 [▶]
"renungan" => array:4 [▶]
"renungan[judul]" => array:5 [▶]
"renungan[bahanbaca]" => array:5 [▶]
"renungan[isi]" => array:6 [▶]
]
这就是我在控制器中所做的,如您所愿..
class WartaRutin extends Controller
{
public $implement = ['Backend\Behaviors\ListController','Backend\Behaviors\FormController','Backend\Behaviors\ReorderController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Mismaiti.MyWarta', 'main-menu-item', 'side-menu-rutin');
}
public function formExtendFieldsBefore($form) {
// we are checking we are creating record or updating
// or even we can use $form->context here but I used $model->id
// if $form->context == 'update'
// if $form->context == 'create'
if(is_null($form->model->id)) {
// create time
$iteration = $form->fields['kebum']['maxItems'];
if(is_numeric($iteration) && $iteration > 0) {
$emptyFields = [];
while($iteration > 0) {
$emptyFields[] = ['jeniskeb' => ''];
$iteration--;
}
$form->model->kebum = $emptyFields;
}
}
}
}
它 return 当我尝试执行时出错——我已将所有 repeater_data 替换为 kebum
"Undefined index: kebum" on line 30 of C:\xampp\htdocs\mywarta\plugins\mismaiti\mywarta\controllers\WartaRutin.php
我是不是漏掉了什么..?
嗯,我们可以使用 one trick
,因为您可能只在 create time
需要它,或者您可以在 update time as well
、
我用了字段repeater_data
你可以用你的字段替换它field
fields.yaml
fields:
... other fields ...
repeater_data:
label: Repeater
oc.commentPosition: ''
prompt: 'Add new item'
maxItems: '5'
span: auto
type: repeater
form:
fields:
title:
label: Text
oc.commentPosition: ''
span: auto
type: text
category:
label: Dropdown
oc.commentPosition: ''
options:
1: 'Item 1'
2: 'Item 2'
span: auto
type: dropdown
controller
在您的 controller
中,您需要添加此 method and code
public function formExtendFieldsBefore($form) {
// we are checking we are creating record or updating
// or even we can use $form->context here but I used $model->id
// if $form->context == 'update'
// if $form->context == 'create'
if(is_null($form->model->id)) {
// create time **logic**
$iteration = $form->fields['repeater_data']['maxItems'];
if(is_numeric($iteration) && $iteration > 0) {
$emptyFields = [];
while($iteration > 0) {
// here you need to provide at least one field
// which you used in repeater I used `title`
// and let it be empty or may be some default value
$emptyFields[] = ['title' => ''];
$iteration--;
}
// dummy fields assignment to current form model
$form->model->repeater_data = $emptyFields;
}
// **logic**
}
else {
// if you need to adjust data in update time
// you need to find difference and then need to add
// additional dummy data as we are adding above
}
}
有2个场景 create time
和update time
Create time
当用户创建记录时,我们将添加dummy fields
,以便转发器按原样显示它们already there
,这是使用maxItems property
完成的] field so its fully dynamic
,现在用户不需要按那个按钮。
Update time ( Suppose maxItems=5)
现在 2nd scenario
我们有 else 条件 如果你希望你可以添加一些逻辑并在那里做你的事情,假设 用户可以只添加一次 2 条记录 所以下次我们需要添加 3 dummy fields
(2 filled + 3 dummy = total 5 as maxItem) 所以你可以 calculate there
和 add it from else part
.
希望对您有所帮助,如果您发现任何困难,请发表评论。