gpt4 book ai didi

php - 使用 ajax 从多个模型中获取下拉依赖值

转载 作者:行者123 更新时间:2023-11-29 03:08:47 25 4
gpt4 key购买 nike

我在 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 | | | |
+------------------+--------------+------+-----+---------------------+----------------+

我已经为这两个模型制作了模型和 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'),
);
}

现在我将两个模型呈现在一个页面中,这样我就可以在一个表单中输入所有相应的字段。

 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'); ?>

现在对于这段代码,我从 Student 模型中获取了所有学生姓名。 所以我的问题是,当我从 下拉列表 中获取学生姓名并选择一个学生时,它还会获取学生的所有相应值,以在 _form.php 中呈现 无需单击保存按钮。这样用户就不必手动再次放置它。 我认为 ajax 和 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 中看到时,我得到了错误。这是屏幕截图 enter image description here

最佳答案

很简单。请阅读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;'
)
)?>

在我的 Controller 中我做了类似的事情

$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 是需要更新的下拉菜单。

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

关于php - 使用 ajax 从多个模型中获取下拉依赖值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11343904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com