如何使用Activeform在Yii2中创建依赖下拉?

时间:2016-08-11 19:21:06

标签: php model-view-controller yii2

我有一个表单,其中有2个下拉列表,从第一个下拉列表中选择批次,根据该批次,学生将出现在第二个下拉列表中,现在我正在获得批量下拉列表,但是没有获得使用批次的学生下拉列表< / p>

  

这是我的观点

<?php
 use yii\helpers\Html;
 use yii\widgets\ActiveForm;
 use yii\helpers\ArrayHelper;
 use common\models\TblBetch;
 use common\models\TblCompany;
 use common\models\TblCourse;
 use common\models\TblStudent;
 use common\models\TblStudentExpence;
 use kartik\widgets\DepDrop;
 use yii\db\Query;?><link rel="shortcut icon" href="<?php echo Yii::$app->params['global_theme_path'];?>images/logo1.png" type="image/png"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://iamrohit.in/lab/js/location.js"></script><div class="pageheader"><h2><i class="fa fa-money"></i>Expense</h2>
<div class="breadcrumb-wrapper">
    <span class="label">You are here:</span>
    <ol class="breadcrumb">
        <li><a href="dashboard">Westline Shipping</a></li>
        <li class="active">Add Expense</li>
    </ol></div></div><div class="contentpanel">
<?php
$session = Yii::$app->session;
if($session->hasFlash('success'))
{
    echo $session->getFlash('success');
}
if($session->hasFlash('error'))
{
    echo $session->getFlash('error');
}
$site = '';
?>
<div class="row">
    <div class="col-md-12">
        <div class="container-fluid-50">
            <div class="col-md-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <div class="panel-btns">
                            <a href="#" class="minimize">&minus;</a>
                        </div>
                        <h4 class="panel-title">Add Expense</h4>
                    </div>
                    <form class="panel-body" action="dashboard" method="post">
                        <div class="row">
                            <?php $form = ActiveForm::begin(); ?>
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label class="control-label"></label>
                                    <?php
                                    //$batch = new TblBetch();
                                    //$quer = new Query();
                                    //$getid = [''];
                                    //$getid = $quer->select('b_sn')->from($batch->tableName())->all();
                                    //echo $form->field($model,'se_b_id')->dropDownList($getid,['id'=>'se_b_id']);
                                    ?>
                                    <?=
                                    $form->field($model,'se_s_id')->dropDownList(
                                        ArrayHelper::map(TblBetch::find()->all(),'b_id','b_sn'),
                                        [
                                            'prompt'=>'Select Batch',
                                            'name'=>'s_b_id',
                                            'id'=>'s_b_id',
                                            'onchange'=>'$.post("student?id="+$(this).val(),function(data){$("select#tblstudentexpence-se_s_id").html(data);});'
                                        ]);
                                    ?>
                                </div>
                            </div><!-- col-sm-6 -->
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label class="control-label"></label>
                                    <?=
                                        $form->field($model,'se_s_id')->dropDownList(
                                        ArrayHelper::map(TblStudent::find()->all(),'s_id','s_fname'),
                                        [
                                            'prompt'=>'Select Student',
                                             'name'=>'se_s_id',
                                            'id'=>'se_s_id'
                                        ]);
                                    ?>

                                </div>
                            </div><!-- col-sm-6 -->
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label class="control-label">Payment Mode</label>
                                    <div class="radio"><label><input type="radio" name="num" value="cash" required> Cash </label></div>
                                    <div class="radio"><label><input type="radio" name="num" value="check" required> Check </label></div>
                                    <div class="radio"><label><input type="radio" name="num" value="dd" required> DD </label></div>
                                </div>
                            </div><!-- col-sm-6 -->
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label class="control-label">Check / DD No</label>
                                    <input type="text" name="num" id="num" class="form-control" placeholder="Enter your check or DD number" required/>
                                </div>
                            </div><!-- col-sm-6 -->
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label class="control-label">Amount</label>
                                    <input type="text" name="lastname" class="form-control" placeholder="Enter your Amount" required/>
                                </div>
                            </div><!-- col-sm-6 -->

                            <div class="col-sm-9">
                                <div class="form-group"> <br><br>
                                    <input type="submit" value="Submit" class="btn btn-primary">
                                    &nbsp;&nbsp;&nbsp;
                                    <input type="reset" value="Cancel" class="btn btn-primary">
                                </div></div>
                        </div><!-- row -->
                    </form>
                </div><!-- panel-body -->

            </div>
        </div>

    </div>              <div class="col-md-12">

    </div>

</div></div></div> <script>
$(function() {
    window.invalidate_input = function() {
        if ($('input[name=num]:checked').val() == "check" || $('input[name=num]:checked').val() == "dd")
            $('#num').removeAttr('disabled');
        else
            $('#num').attr('disabled', 'disabled');
    };

    $("input[name=num]").change(invalidate_input);

    invalidate_input();
});

  

这是我的控制器(sitecontroller.php)

public function actionStudent($id)
{
    //p($id);
        $student = new TblStudent();
        $queryobj = new Query();
        $data = [];
        $data = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->count();
        //p($data);
        $queryobj1 = new Query();
        $getid = [];
        $getid = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->all();
        //p($getid);
        if($data > 0)
        {
            foreach($getid as $gid)
            {
                echo "<option value='".$gid->s_id."'>".$gid->s_fname."</option>";
            }
        }
        else
        {
            echo "<option> - </option>";
        }
}
  

我在ajax调用后在firebug中收到此回复   enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在yii 2中更简单地实现依赖下拉,因为您可以使用可用的小部件。

在这里使用kartik小部件 - &gt; kartik dependent drop down