Yii2 动态数据未加载到动态字段中的所有 DepDrop 上

发布于 2025-01-13 00:18:26 字数 7375 浏览 0 评论 0原文

我正在尝试为我的一个项目创建一个动态表单。我使用 wbraganca 的 Dynamic From 来实现这一点。在我的动态表单中,我有一个 DepDrop 来显示父类别中的子类别列表。
我的 _form.php

<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
    <div class="col-md-6">
        <?= $form->field($model, 'rq_job_no')->textInput() ?>
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'rq_req_date')->widget(\yii\jui\DatePicker::class, [
            //'language' => 'ru',
            'dateFormat' => 'php:Y-m-d',
            'clientOptions' => [
                'changeMonth' => true,
                'changeYear' => true,
                'showButtonPanel' => true,
                'yearRange' => '1990:2030',
                'minDate' => date('Y-m-d'),
            ],
            'options' => ['class' => 'form-control', 'readOnly' => true, 'placeholder' => 'Enter the Item Required Date'],
        ]) ?>
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'rq_priority_type')->dropDownList(['urgent' => 'Urgent', 'normal' => 'Normal'], ['prompt' => '--Select Priority Type--']) ?>
    </div>
    <div class="col-md-6">
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'rq_approval_type')->dropDownList([1 => ' Approved Vendor Needed', 2 => 'Approved Submitted Needed', 3 => 'Warranty Certificate to be Collected', 4 => 'Long Lead Material',], ['prompt' => '--Select Type of Approval--']) ?>
        <?php echo $form->field($model, 'rq_cat_type')->widget(Select2::class, [
            'data' => $catData,
            'options' => ['placeholder' => '--Select Request Type--', 'class' => 'form-control'],
            'pluginOptions' => [
                'allowClear' => true
            ],
        ]);
        ?>
    </div>
    <div class="col-md-12">
        <?= $form->field($model, 'rq_remarks')->textarea(['rows' => 6]) ?>
    </div>
</div>

<!-- code for dynamic form -->
<div class="panel panel-default">
    <div class="panel-heading">
        <h4><i class="glyphicon glyphicon-envelope"></i> Request Items</h4>
    </div>
    <div class="panel-body">
        <?php DynamicFormWidget::begin([
            'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
            'widgetBody' => '.container-items', // required: css class selector
            'widgetItem' => '.item', // required: css class
            'limit' => 10, // the maximum times, an element can be cloned (default 999)
            'min' => 1, // 0 or 1 (default 1)
            'insertButton' => '.add-item', // css class
            'deleteButton' => '.remove-item', // css class
            'model' => $modelsAddress[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'item_name',
                'item_qty',
                'item_unit',
            ],
        ]); ?>

        <div class="container-items">
            <!-- widgetContainer -->
            <?php foreach ($modelsAddress as $i => $modelAddress) : ?>
                <div class="item panel panel-default">
                    <!-- widgetBody -->
                    <div class="panel-heading">

                        <h4 class="panel-title pull-left">Items</h4>

                        <div class="pull-right">
                            <button type="button" class="add-item btn btn-success btn-xs"><i class="fa fa-plus"></i></button>
                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="panel-body">
                        <?php
                        // necessary for update action.
                        if (!$modelAddress->isNewRecord) {
                            echo Html::activeHiddenInput($modelAddress, "[{$i}]rt_id");
                        }
                        ?>
                        <?php //$form->field($modelAddress, "[{$i}]rt_item")->textInput(['maxlength' => true]) ?>
                        <?= $form->field($modelAddress, "[{$i}]rt_item")->widget(DepDrop::class, [
                            'options' => ['id' => 'reqitems-'.$i.'-rt_item'],
                            'pluginOptions' => [
                                'depends' => ['requests-rq_cat_type'],
                                'placeholder' => '--Select Location--',
                                'url' => Url::to(['/requests/subcat'])
                            ]
                        ]); ?>
                        <div class="row">
                            <div class="col-sm-6">
                                <?= $form->field($modelAddress, "[{$i}]rt_qty")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($modelAddress, "[{$i}]rt_unit")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>

        </div>
        <?php DynamicFormWidget::end(); ?>
    </div>
</div>





<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>

<?php ActiveForm::end(); ?>    

我的控制器操作加载子类别

 public function actionSubcat()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $cat_id = $parents[0];
                $out = self::getSubCatList($cat_id);
                return ['output' => $out, 'selected' => ''];
            }
        }
        return ['output' => 'No Category Available', 'selected' => ''];
    }
    public function getSubCatList($cat_id)
    {
        $output = [];
        $category = Item::find()->where(['item_category' => $cat_id])->orderBy('item_title')->all();
        if (empty($category)) {
            $output[] = ['id' => '0', 'name' => 'No Category Available'];
        } else {
            foreach ($category as $cat) {
                $output[] = ['id' => $cat->item_id, 'name' => $cat->item_title];
            }
        }
        return $output;
    }    

在此处输入图像描述

我的问题是 DepDrop 仅加载动态表单第一个循环的子类别,而将其余循环留空。附上截图供您参考。
任何人都可以通过指出我所缺少的内容或如何实现我的要求来提供帮助。

I'm trying to create a dynamic form for one of my projects. I used wbraganca's Dynamic From to achieve this. In my dynamic form I have a DepDrop to show subcategories list from a parent category.
My _form.php

<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
    <div class="col-md-6">
        <?= $form->field($model, 'rq_job_no')->textInput() ?>
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'rq_req_date')->widget(\yii\jui\DatePicker::class, [
            //'language' => 'ru',
            'dateFormat' => 'php:Y-m-d',
            'clientOptions' => [
                'changeMonth' => true,
                'changeYear' => true,
                'showButtonPanel' => true,
                'yearRange' => '1990:2030',
                'minDate' => date('Y-m-d'),
            ],
            'options' => ['class' => 'form-control', 'readOnly' => true, 'placeholder' => 'Enter the Item Required Date'],
        ]) ?>
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'rq_priority_type')->dropDownList(['urgent' => 'Urgent', 'normal' => 'Normal'], ['prompt' => '--Select Priority Type--']) ?>
    </div>
    <div class="col-md-6">
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'rq_approval_type')->dropDownList([1 => ' Approved Vendor Needed', 2 => 'Approved Submitted Needed', 3 => 'Warranty Certificate to be Collected', 4 => 'Long Lead Material',], ['prompt' => '--Select Type of Approval--']) ?>
        <?php echo $form->field($model, 'rq_cat_type')->widget(Select2::class, [
            'data' => $catData,
            'options' => ['placeholder' => '--Select Request Type--', 'class' => 'form-control'],
            'pluginOptions' => [
                'allowClear' => true
            ],
        ]);
        ?>
    </div>
    <div class="col-md-12">
        <?= $form->field($model, 'rq_remarks')->textarea(['rows' => 6]) ?>
    </div>
</div>

<!-- code for dynamic form -->
<div class="panel panel-default">
    <div class="panel-heading">
        <h4><i class="glyphicon glyphicon-envelope"></i> Request Items</h4>
    </div>
    <div class="panel-body">
        <?php DynamicFormWidget::begin([
            'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
            'widgetBody' => '.container-items', // required: css class selector
            'widgetItem' => '.item', // required: css class
            'limit' => 10, // the maximum times, an element can be cloned (default 999)
            'min' => 1, // 0 or 1 (default 1)
            'insertButton' => '.add-item', // css class
            'deleteButton' => '.remove-item', // css class
            'model' => $modelsAddress[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'item_name',
                'item_qty',
                'item_unit',
            ],
        ]); ?>

        <div class="container-items">
            <!-- widgetContainer -->
            <?php foreach ($modelsAddress as $i => $modelAddress) : ?>
                <div class="item panel panel-default">
                    <!-- widgetBody -->
                    <div class="panel-heading">

                        <h4 class="panel-title pull-left">Items</h4>

                        <div class="pull-right">
                            <button type="button" class="add-item btn btn-success btn-xs"><i class="fa fa-plus"></i></button>
                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="panel-body">
                        <?php
                        // necessary for update action.
                        if (!$modelAddress->isNewRecord) {
                            echo Html::activeHiddenInput($modelAddress, "[{$i}]rt_id");
                        }
                        ?>
                        <?php //$form->field($modelAddress, "[{$i}]rt_item")->textInput(['maxlength' => true]) ?>
                        <?= $form->field($modelAddress, "[{$i}]rt_item")->widget(DepDrop::class, [
                            'options' => ['id' => 'reqitems-'.$i.'-rt_item'],
                            'pluginOptions' => [
                                'depends' => ['requests-rq_cat_type'],
                                'placeholder' => '--Select Location--',
                                'url' => Url::to(['/requests/subcat'])
                            ]
                        ]); ?>
                        <div class="row">
                            <div class="col-sm-6">
                                <?= $form->field($modelAddress, "[{$i}]rt_qty")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($modelAddress, "[{$i}]rt_unit")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>

        </div>
        <?php DynamicFormWidget::end(); ?>
    </div>
</div>





<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>

<?php ActiveForm::end(); ?>    

My Controller action to load the subcategory

 public function actionSubcat()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $cat_id = $parents[0];
                $out = self::getSubCatList($cat_id);
                return ['output' => $out, 'selected' => ''];
            }
        }
        return ['output' => 'No Category Available', 'selected' => ''];
    }
    public function getSubCatList($cat_id)
    {
        $output = [];
        $category = Item::find()->where(['item_category' => $cat_id])->orderBy('item_title')->all();
        if (empty($category)) {
            $output[] = ['id' => '0', 'name' => 'No Category Available'];
        } else {
            foreach ($category as $cat) {
                $output[] = ['id' => $cat->item_id, 'name' => $cat->item_title];
            }
        }
        return $output;
    }    

enter image description here

My problem is that DepDrop is only loading the subcategory for the first loop of the dynamic form and leaves the rest loop blank. Attached screenshot for your reference.
Can anyone help by pointing what I am missing or how can I achieve my requirement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文