yii2中的DynamicFormWidget dropDownList onchange

时间:2018-10-16 20:36:55

标签: php yii yii2

当我单击id_pasal时,它会显示在它旁边。但是,如果我添加另一个id_pasal,则不会在其旁边的下拉菜单中显示优良的文章,而是在第一个下拉列表中出现。

如何在单击时使id_pasal在侧面下拉列表中显示罚款结果?

这是我的外观和代码,请帮助我

enter image description here

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use backend\models\Penduduk;
use backend\models\BarangJenisSitaan;
use backend\models\KendaraanJenis;
use backend\models\KendaraanMerek;
use backend\models\Pasal;
use backend\models\Denda;
use wbraganca\dynamicform\DynamicFormWidget;
/* @var $this yii\web\View */
/* @var $model backend\models\Tilang */
/* @var $form yii\widgets\ActiveForm */
?>



<?php $form = ActiveForm::begin(['class' => 'form-horizontal','id'=>'dynamic-form']); ?>

<div class="panel-body">
    <?= $form->field($model, 'no_tilang')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'no_ktp')->dropDownList(
        ArrayHelper::map(Penduduk::find()->all(), 'no_ktp','no_ktp'), 
        [
            'class'=>'form-control select',
            'prompt'=>'Masukan Nomor KTP',
            'data-live-search'=>'true',
            'onChange'=>'
                $.post("index.php?r=penduduk/listsnama&id='.'"+$(this).val(), function(data) {
                        $("select#tilang-nama_penduduk").html(data);
                    });
                $.post("index.php?r=penduduk/liststgllahir&id='.'"+$(this).val(), function(data) {
                        $("select#tilang-tgl_lahir_penduduk").html(data);
                    });
                $.post("index.php?r=penduduk/listsjekel&id='.'"+$(this).val(), function(data) {
                        $("select#tilang-jekel_penduduk").html(data);
                    });
                $.post("index.php?r=penduduk/listsalamat&id='.'"+$(this).val(), function(data) {
                        $("select#tilang-alamat_penduduk").html(data);
                    });
                '
        ]); 
    ?>

    <?= $form->field($model, 'nama_penduduk')->dropDownList(
        ArrayHelper::map(Penduduk::find()->where(['no_ktp'=>$model->no_ktp])->asArray()->all(), 'no_ktp','nama_penduduk'), 
        [
            'prompt'=>'Nama Penduduk',
        ]); ?>

    <?= $form->field($model, 'jekel_penduduk')->dropDownList(
        ArrayHelper::map(Penduduk::find()->where(['no_ktp'=>$model->no_ktp])->asArray()->all(), 'no_ktp','jekel_penduduk'), 
        [
            'prompt'=>'Jekel Penduduk',
        ]); ?>

    <?= $form->field($model, 'tgl_lahir_penduduk')->dropDownList(
        ArrayHelper::map(Penduduk::find()->where(['no_ktp'=>$model->tgl_lahir_penduduk])->asArray()->all(), 'no_ktp','tgl_lahir_penduduk'), 
        [
            'prompt'=>'Tgl Lahir Penduduk',
        ]); ?>

    <?= $form->field($model, 'alamat_penduduk')->dropDownList(
        ArrayHelper::map(Penduduk::find()->where(['no_ktp'=>$model->alamat_penduduk])->asArray()->all(), 'no_ktp','alamat_penduduk'), 
        [
            'prompt'=>'Tgl Lahir Penduduk',
        ]); ?>

    <?= $form->field($model, 'tgl_sidang')->textInput(['class'=>'form-control datepicker']) ?>
    <?= $form->field($model, 'id_barang_jenis_sitaan')->dropDownList(
        ArrayHelper::map(BarangJenisSitaan::find()->all(), 'id_barang_jenis_sitaan','nama_barang_jenis_sitaan'), 
        [
            'class'=>'form-control select',
            'prompt'=>'Pilih Barang Sitaan',
            'data-live-search'=>'true',
        ]); 
    ?>
    <?= $form->field($model, 'nomor_barang_sitaan')->textInput() ?>

    <?= $form->field($model, 'id_kendaraan_jenis')->dropDownList(
        ArrayHelper::map(KendaraanJenis::find()->all(), 'id_kendaraan_jenis','nama_kendaraan_jenis'), 
        [
            'class'=>'form-control select',
            'prompt'=>'Pilih Jenis Kendaraan',
            'data-live-search'=>'true',
        ]); 
    ?>

    <?= $form->field($model, 'id_kendaraan_merek')->dropDownList(
        ArrayHelper::map(KendaraanMerek::find()->all(), 'id_kendaraan_merek','nama_kendaraan_merek'), 
        [
            'class'=>'form-control select',
            'prompt'=>'Pilih Merek Kendaraan',
            'data-live-search'=>'true',
        ]); 
    ?>
    <?= $form->field($model, 'foto_tilang')->fileInput(["accept"=>"image/*"]); ?>
    <div id="mapcanvas"></div>
    <div class="col-md-6">
        <?= $form->field($model, 'longitude_tilang')->textInput(['id'=>'lng','readonly'=>'true']) ?>
    </div>
    <div class="col-md-6">
        <?= $form->field($model, 'latitude_tilang')->textInput(['id'=>'lat','readonly'=>'true']) ?>
    </div>
</div>

<div class="row">
    <div class="panel panel-default">
        <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Pasal yang dilanggar</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' => $modelPasal[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'id_pasal',
                    'denda',
                ],
            ]); ?>

            <div class="container-items"><!-- widgetContainer -->
            <?php foreach ($modelPasal as $i => $modelPasal2): ?>
                <div class="item panel panel-default"><!-- widgetBody -->
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left">Pasal</h3>
                        <div class="pull-right">
                            <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="panel-body">
                        <?php
                            // necessary for update action.
                            if (! $modelPasal2->isNewRecord) {
                                echo Html::activeHiddenInput($modelPasal2, "[{$i}]id");
                            }
                        ?>

                        <div class="row">
                            <div class="col-sm-6">

                                <?= $form->field($modelPasal2, "[{$i}]id_pasal")->dropDownList(
                                        ArrayHelper::map(Pasal::find()->all(), 'id_pasal','nama_pasal'), 
                                        [
                                            'class'=>'form-control select',
                                            'prompt'=>'Pilih Pasal',
                                            'data-live-search'=>'true',
                                            'onChange'=>'
                                                $.post("index.php?r=denda/lists&id='.'"+$(this).val(), function(data) {
                                                        $("select#'. 
                    Html::getInputID($modelPasal2, "[{$i}]denda").'").html(data);
                                                    });'
                                        ]); 

                                ?>

                            </div>
                            <div class="col-sm-6">


                                <?= $form->field($modelPasal2, "[{$i}]denda")->dropDownList(
                                        ArrayHelper::map(Denda::find()->where(['id_pasal'=>$modelPasal2->id_pasal])->asArray()->all(), 'denda_wilayah','denda_wilayah'), 
                                        [
                                            'prompt'=>'Denda',
                                        ]); 
                                ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
            <?php DynamicFormWidget::end(); ?>
        </div>
    </div>
</div>

<div class="panel-footer">
        <?= Html::submitButton('Simpan', ['class' => 'btn btn-success pull-right']) ?>
</div>

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

0 个答案:

没有答案