2

我搜索了所有文档Yii但没有得到答案。所以我终于来到了这里。我有以下架构

Table Schools
+------------------+--------------+------+-----+---------------------+----------------+
| Field            | Type         | Null | Key | Default             | Extra          |
+------------------+--------------+------+-----+---------------------+----------------+
| id               | int(10)      | NO   | PRI | NULL                | auto_increment |
| school_name      | varchar(100) | NO   |     |                     |                |
+------------------+--------------+------+-----+---------------------+----------------+

Table Students

+------------------+--------------+------+-----+---------------------+----------------+
| Field            | Type         | Null | Key | Default             | Extra          |
+------------------+--------------+------+-----+---------------------+----------------+
| id               | int(10)      | NO   | PRI | NULL                | auto_increment |
| school_id        | int(10)      | NO   | FK  |                     |                |
| student_name     | varchar(100) | NO   |     |                     |                |
| roll_no          | varchar(80)  | NO   |     |                     |                |
| class            | varchar(20)  | NO   |     |    |                |                |
| subjects         | varchar(100) | NO   |     |                     |                |
+------------------+--------------+------+-----+---------------------+----------------+

models and CRUD为这两个模型做了。在模型中我的关系是这样的

Students.php关系就像

  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'School' => array(self::BELONGS_TO,'Schools','school_id'),
    );
  }

Schools.php关系就像

public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'student' => array(self::HAS_MANY, 'Students', 'school_id'),
    );
  }

现在我做了,two models rendered in a single page以便我可以在一个表单中输入所有相应的字段。

 In the _form.php file of Students I have made some change in student_name like this
         <div class="row">
        <?php echo $form->labelEx($model,'student_name'); ?>
        <?php echo $form->dropdownList($model,'student_name', CHtml::listData(Students::model()->findAll(), 'id', 'student_name'), array('empty'=>array('Select'=>'--Select One---'))); ?>
        <?php echo $form->error($model,'student_name'); ?>

现在对于我得到的这段代码all the student name from the Student model。所以我的问题是当我从中获取学生姓名dropdown list并选择一个学生时,它还将获取学生的所有相应值以在_form.php不点击保存按钮的情况下呈现。这样用户就不必输入再次手动。我认为ajax and json encode会在这里工作,但不知道如何让他们在这里工作。

[更新]

这是StudentsController代码

 public function actionDisCoor() {
    $model = School::model()->findByPk($_POST['Students']['student_id']);
    $data=CHtml::listData($data,'id','name');
    foreach($data as $value=>$name)
    {
      echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
    }
  }

这是_form.php代码Students

  <div class="row">
    <?php echo $form->labelEx($model,'student_name'); ?>
    <?php  $List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
?>
    <?php echo $form->dropdownList($model,'student_name',$List,
                                array('onChange'=>CHtml::ajax(array(
                                'url' => CController::createUrl('DisCoor'),
                                'type' => 'POST',                     
                               'update'=>'#school_id',
                                )),'style'=>'width:180px;'
                                    )
                                )?>
    <?php echo $form->error($model,'student_name'); ?>
  </div>

这是代码accessRules()

  public function accessRules()
  {
    return array(
      array('allow',  // allow all users to perform 'index' and 'view' actions
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),
      array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('create','update'),
        'users'=>array('@'),
      ),
      array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('create','update','DisCoor'),
        'users'=>array('@'),
      ),
      array('allow', // allow admin user to perform 'admin' and 'delete' actions
        'actions'=>array('admin','delete'),
        'users'=>array('admin'),
      ),
      array('deny',  // deny all users
        'users'=>array('*'),
      ),
    );
  }

毕竟,当我在萤火虫中看到时,我得到了错误。这是屏幕截图 在此处输入图像描述

4

1 回答 1

1

这很简单。请阅读创建依赖下拉列表。希望它能回答你所有的疑问。

您也可以执行以下示例中的onChange操作

     <?php echo
 $List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
 $form->dropdownList($model,'student_name',$List,
                                array('onChange'=>
                                CHtml::ajax(array(
                                'url' => CController::createUrl('DisCoor'),
                                'type' => 'POST',                     
                               'update'=>'#school_id',
                                )),'style'=>'width:180px;'        
                                    )
                                )?>

在我的控制器中我做了类似的事情

$model = School::model()->findByPk($_POST['Jobs']['student_id']);
                 $data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
    echo CHtml::tag('option',
               array('value'=>$value),CHtml::encode($name),true);
}
            

现在#school_id 是需要更新的下拉菜单。

我已经给了你几乎所有你需要的东西 祝你好运

于 2012-07-05T12:05:45.437 回答