【问题标题】:Yii dependant dropDownListYii 依赖 dropDownList
【发布时间】:2014-09-12 08:12:35
【问题描述】:

我需要用 yii 创建依赖 dropDownLists。因此,我在控制器中创建了一个视图和一个动作,如下所示:

观点

<?php
echo "<div class='row'>";
echo $form->labelEx($model, 'id_structure');
echo $form->dropDownList($model, 'id_structure', GxHtml::listDataEx(StructureInformation::model()->findAllAttributes()), 
array('ajax' => array('type' => 'POST', 
    'url' => CController::createUrl('InfoComplementAAjouter/fillTypeList'), 
    'update' => '#typeDonnee', 
    'data' => array('id_structure' => 'js:this.value'))
));
echo $form->error($model, 'id_structure');
echo '</div>';
echo "<div class='row'>";
echo $form->labelEx($model, 'type');
echo $form->dropDownList($model, 'type', array(), array('prompt' => 'Sélectionner', 'id' => 'typeDonnee'));
echo $form->error($model, 'type');
echo '<div>';
?>

现在控制器InfoComplementAAjouterController中的动作

public function actionFillTypeList(){

        $id_struct = $_POST['InfoComplementAAjouter']['id_structure']; //I get the selected value
        $taille = StructureInformation::model()->find('id = ' . (int)$id_struct)->taille; //and then pass it to the StructureInformation model to obtain the attribute taille

        //I create two arrays which content will be the options of my dropDownList. 
        $un = array('text'=> 'Texte', 'radio' => 'Bouton radio', 'dropDownList' => 'Liste déroulante');
        $plusieurs = array( 'checkboxlist' => 'Choix multiple',);
        //If $taille == 1, I display the contents of $un; if $taille > 1, the content of $plusieurs will be displayed.
        if($taille == 1){
            foreach ($un AS $value => $name){
                $opt = array();
                $opt['value'] = $value;

                echo CHtml::tag('option', $opt, CHtml::encode($name), true);
            }
        }
       if($taille > 1){
            foreach ($plusieurs AS $value => $name){
                $opt = array();
                $opt['value'] = $value;

                echo CHtml::tag('option', $opt, CHtml::encode($name), true);
            }
        }
        die;
    }

我注意到该操作未执行。我不明白为什么。 有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: php ajax drop-down-menu yii


    【解决方案1】:

    我刚看到一个错字:

    'upedate' => '#typeDonnee', 
    

    应该是'update'

    先尝试修复它

    编辑:

    你有更多的小错误/错别字

    echo $form->dropDownList($model, 'id_structure', GxHtml::listDataEx(StructureInformation::model()->findAllAttributes(), 
    array('ajax' => array('type' => 'POST', 
        'url' => CController::createUrl('InfoComplementAAjouter/fillTypeList'), 
        'update' => '#typeDonnee', 
        'data' => array('id_structure' => 'js:this.value'))
    ));
    

    这里你需要关闭括号

    ================================================ ======

    StructureInformation::model()->findAllAttributes()
    

    CActiveRecord 中没有 findAllAttributes() 方法 也许你想使用findAllByAttributes(),但是你想传递一些属性。如果这是您的自定义方法,那么您应该解释它是如何工作的,或者粘贴代码

    编辑 2:

    $_POST['InfoComplementAAjouter']['id_structure']
    

    尝试访问这样的帖子:

    $_POST['id_structure']
    

    我认为您以该结构发送帖子

    复制你的情况对我来说不是那么容易,所以我有点在黑暗中射击

    【讨论】:

    • 谢谢,但这并不能解决问题。我必须相应地编辑我的帖子。
    • 好的,好的!写这篇文章时,我犯了一些错误,因为我没有复制和粘贴。
    • findAllAttributes()GxActiveRecord 的一个方法,因为我正在使用 Gii 扩展来生成我的模型、控制器和视图。
    • 不,任何错误。第二个 dropDownList 保持为空,即使在第一个中选择了另一个值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多