如果选择了下拉列表中的值,则不会显示

时间:2016-10-19 05:20:10

标签: php drop-down-menu yii yii2

我有两张桌子,彼此相关,一对一是丈夫和妻子。我已经制作了我的价值下拉列表,并从表格Husband中获取数据。我想当它的价值被选中时不会出现在他的下拉列表中。也许还有其他参考或可以帮助。有关代码,请参见下图。

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Husband;

/* @var $this yii\web\View */
/* @var $model app\models\Wife */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="wife-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'id_husband')->dropDownList(
        ArrayHelper::map(Husband::find()->where('status' => 0)->all(), 'id_husband', 'name'),
        ['prompt' => 'Pilih']    
    ) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'status')->dropDownList(['0' => 'Tidak Aktif', '1' => 'Aktif'],
    ['prompt'=>'--Pilih--', 'style' => 'width:380px']) ?> 

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

</div>

2 个答案:

答案 0 :(得分:0)

mungkin kau harus memakai javascript ini adalah contoh view saya dan controller

视图

ValueError: 'e' is a bad directive in format '%Y%m%e'

controlller

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use backend\models\TahunAjaran;
use backend\models\Mahasiswa;
use backend\models\Matakuliah;
/* @var $this yii\web\View */
/* @var $model backend\models\Dhs */
/* @var $form yii\widgets\ActiveForm
 */

?>

<div class="dhs-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'id_tahun_ajaran')->dropDownList(ArrayHelper::map(TahunAjaran::find()->all(),'id','tahun_ajaran')) ?>

    <?= $form->field($model, 'npm_mahasiswa')->dropDownList(ArrayHelper::map(Mahasiswa::find()->all(),'id','nama'),
    ['prompt'=>'Pilih Mahasiswa',
    'onchange'=>'
                $.post("index.php?r=mahasiswa/lists&id='.'"+$(this).val(),function(data)
                { $("select#dhs-user_id" ).html(data), $("select#nilai-1-nilai" ).html(data);
            });' ]
  ); ?>


  <?= $form->field($model, 'user_id')->dropDownList(ArrayHelper::map(Mahasiswa::find()->all(),'id','user_id'),
  ['prompt'=>'Pilih User Id',
   ]
); ?>

<?= $form->field ($model,'semester')->dropDownList(ArrayHelper::map(Matakuliah::find()->all(),'id','semester'),
['prompt'=>'Pilih Semester',
'onchange'=>'
$.post("index.php?r=matakuliah/lists&id='.'"+$(this).val(),function(data)
{
$("select#dhs-id_matakuliah").html(data);
});' ]
); ?>
    <?= $form->field($model, 'id_matakuliah')->dropDownList(ArrayHelper::map(Matakuliah::find()->all(),'id','id')) ?>

    <?= $form->field($model, 'nilai')->dropDownList(['A' => 'A','B'=>'B','C' => 'C','D'=>'D','E' => 'E',]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

</div>

答案 1 :(得分:0)

你忘了在where条件中提到数组。

使用->where(['status' => 0])" instead of "->where('status' => 0)

所以它是:

<?= $form->field($model, 'id_husband')->dropDownList(
        ArrayHelper::map(Husband::find()->where(['status' => 0])->all(), 'id_husband', 'name'),
        ['prompt' => 'Pilih']    
    ) ?>