使用ajax从多个模型中获取下拉相关值

时间:2012-07-05 12:00:32

标签: php mysql yii

我在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('*'),
      ),
    );
  }

毕竟当我在萤火虫中看到时,我得到了错误。这是屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:1)

很容易。请阅读Creating a dependent dropdown 。希望它会回答您的所有疑问。

你也可以做点什么  在下面的示例中,看到我已调用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是需要更新的dropDown。

我几乎给了你所需要的一切 最好的运气