wbraganca/yii2-dynamicform值在编辑时复制到克隆字段,但在创建时不复制 [英] wbraganca/yii2-dynamicform values copied to cloned field on edit, but not on create

查看:120
本文介绍了wbraganca/yii2-dynamicform值在编辑时复制到克隆字段,但在创建时不复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Yii2项目中使用动态表格. 我想复制克隆字段中的值. 我观察到的是,如果我使用edit,它将复制克隆字段上的值,但是当我使用create时,它不会复制.

I am using dynamic form in my Yii2 project. I want to copy the values in the cloned field. What I observed that it is copying the values on the cloned field if I use edit, but doesn't copy when I am using create.

是这样设计的吗?我怎样才能做到这一点. 我要复制值的字段是一个选择字段.

is it designed like that. how I can achieve that. The field I want the values copied is a select field.

我还观察到,在yii2-dynamic-form.js中它包含类似以下的代码:

I also observed that in the yii2-dynamic-form.js it contains a code like:

$template.find('input, textarea, select').each(function() {
           $(this).val('');
        });

是此代码防止值复制的原因. 但是我也尝试注释掉相关代码,但没有成功.

is this code preventing the values from copying. but I also tried to comment out the relevant code, but no success.

该字段的相关html如下:

The relevant html for the field is like this:

<div class="col-sm-4">
<?= $form->field($modelCustomBreakTime, 
"[{$i}]days")->dropDownList($days,['prompt'=>'select']) ?>
</div>

根据穆罕默德·奥默·阿斯兰(Muhammad Omer Aslam)评论中的链接的建议代码

The suggest code as per the link in comment by Muhammad Omer Aslam

<?php 
$script= <<<Js
         $('select').each(function() {
        $('.dynamicform_wrapper').on('afterInsert', function (e, item) {
        $(this).clone(true);
        });
});
Js;
$this->registerJs($script);

?>

推荐答案

您可以像下面那样绑定afterInsert事件

You can bind afterInsert event like below

$('.dynamicform_wrapper').on('afterInsert', function (e, item) {
  //code to copy values from the desired row/field set
});

dynamicform_wrapperwidgetContainer小部件属性的值.并使用item保存当前插入的面板/行的引用.

dynamicform_wrapper is the value of widgetContainer widget property.And use the item which holds the reference of the currently inserted panel/row.

所以您需要将代码更改为以下代码,我无法通过运行对其进行测试,但希望它可以正常工作.

So you need to change the code to the following, i could not test it by running but hope it should work without problem.

我假设您的字段名称为days,并且生成的name属性将类似于CustomBreakTime[]['days']如有必要,请在下面的脚本中相应地更改模型名称或属性名称

I assume that your field name is days and the name attribute that is generated will be like CustomBreakTime[]['days'] change the model name or attribute name accordingly in the script below if necessary

<?php 

$rf=new ReflectionClass($modelCustomBreakTime);
$modelname=$rf->getShortName();

$script=<<<JS
        $('.dynamicform_wrapper').on('afterInsert', function (e, item) {
            let totalPanels         =   $(".item.panel.panel-default").length;
            let curPanelIndex       =   totalPanels-1;
            let prvPanelIndex       =   curPanelIndex-1;
            let curSelectInput      =   $(item).find('div.panel-body div.form-group select[name="{$modelname}['+curPanelIndex+'][days]"]');
            let prevSelectInput     =   $("select[name='{$modelname}["+prvPanelIndex+"][days]']");
            curSelectInput.val(prevSelectInput.val());
        });
JS;
$this->registerJs($script, \yii\web\View::POS_READY);
?>

这篇关于wbraganca/yii2-dynamicform值在编辑时复制到克隆字段,但在创建时不复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆