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、それぞれのフィールドすべてを 1 つのフォームに入力できるようになりました。

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

結局、firebugで見たときにエラーが発生しました。スクリーンショットは次のとおりです ここに画像の説明を入力

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 に答える