值在编辑时复制到克隆字段

值在编辑时复制到克隆字段

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

问题描述

我在 Yii2 项目中使用动态表单.我想复制克隆字段中的值.我观察到,如果我使用编辑,它会复制克隆字段上的值,但在我使用创建时不会复制.

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

我还观察到在 yii2-dynamic-form.js 中它包含如下代码:

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

是这段代码防止值被复制.但我也尝试注释掉相关代码,但没有成功.

该字段的相关html是这样的:

<?= $form->field($modelCustomBreakTime,"[{$i}]days")->dropDownList($days,['prompt'=>'select']) ?>

根据 Muhammad Omer Aslam 评论中的链接的建议代码

registerJs($script);?>
解决方案

你可以像下面这样绑定 afterInsert 事件

$('.dynamicform_wrapper').on('afterInsert', function (e, item) {//从所需的行/字段集中复制值的代码});

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

因此您需要将代码更改为以下内容,我无法通过运行来测试它,但希望它可以正常工作.

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

getShortName();$script=<<<JS$('.dynamicform_wrapper').on('afterInsert', function (e, item) {让 totalPanels = $(".item.panel.panel-default").length;让 curPanelIndex = totalPanels-1;让 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, yiiwebView::POS_READY);?>

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.

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.

The relevant html for the field is like this:

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

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);

?>
解决方案

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_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.

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, yiiwebView::POS_READY);
?>

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

09-05 03:03