1

私は Yii フレームワークの初心者です。ページを作成したいと思います。ドロップダウンリストが変更されると、リストビュー/グリッドビューはドロップダウン値によって変更されます。

これが私の見解です

<div class="row">
        <?php

        $records = Company::model()->findAll();
        $company_list = CHtml::listData($records, 'id', 'name');
        echo CHtml::dropDownList('company_id','', $company_list,
        array(
        'onchange'=>"$.fn.yiiListView.update('ajaxListView', {url: '".Yii::app()->createUrl('department/dynamicsectionlist')."?company_id='+$('#company_id option:selected').val()})",
        'prompt'=>'Please select a company',
        )); ?>
    </div>

<?php
$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view_section',
    'id'=>'ajaxListView',
));
?>

これはモデルです

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('p_id',$this->p_id);
        $criteria->compare('created',$this->created,true);
        $criteria->compare('updated',$this->updated,true);
        $criteria->compare('company_id',$this->company_id);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

これはコントローラーです

public function actionDynamicsectionlist()
    {       
        $company_id = $_POST['company_id'];
        $criteria=new CDbCriteria();
        $criteria->condition .= 't.id IN (SELECT t2.id, t2.name FROM department t2 WHERE t2.company_id = :company_id)';

        $criteria->params[':company_id'] = $company_id;
        $dataProvider = new CActiveDataProvider( 'Department', array( 'criteria' => $criteria, ) );
        $this->render( 'sectionlist', array( 'dataProvider' => $dataProvider ) );

    }

しかし、それは機能していません。私を助けてください。

ターソーに敬意を表します

4

1 に答える 1

1

私はそれを解決しました。

これがコントローラー

// Initial view (department/depatmentlist)
public function actionDepartmentlist()
    {
        $model=new Department('search');        
        $model->unsetAttributes();  // clear any default values     
        $model->p_id = 0;

        // $dataProvider->getData() will return a list of Post objects
        // $dataProvider=new CActiveDataProvider('Department');
        $this->render('list_department',array(
            'model'=>$model,
        ));

    }

// when the user selected the company from dropdown list
public function actionDynamicsectionlist()
    {
        $model=new Department('dsearch');
        $model->unsetAttributes();  // clear any default values
        $model->p_id = 0;
        if(isset($_GET['company_id']))
            $model->company_id = $_GET['company_id'];

        $this->render('sectionlist',array(
            'model'=>$model,
        ));
    }

これがモデルです(何も変わりません)

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('p_id',$this->p_id);
        $criteria->compare('created',$this->created,true);
        $criteria->compare('updated',$this->updated,true);
        $criteria->compare('company_id',$this->company_id);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

これはビュー(list_department.php)です

<h1>Departments List</h1>
<div class="row">
Company<br />
    <?php           
    $records = Company::model()->findAll();
    $company_list = CHtml::listData($records, 'id', 'name');
    echo CHtml::dropDownList('company_id','', $company_list,
    array('prompt'=>'Please select a company',)); ?>
</div>
<?php
/*
for ListView
$this->widget('zii.widgets.CListView', array(
    //'dataProvider'=>$dataProvider,
    'dataProvider'=>$model->search(),
    'itemView'=>'_view_section',
    'id'=>'ajaxListView',
));
*/
?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'department-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'name',
        //'p_id',
        'created',
        'updated',
        //'company_id',

    ),
)); 
?>


<?php    
Yii::app()->clientScript->registerScript('search',
    "$('#company_id').change(function(){
    var companyId = $('#company_id option:selected').val();                              
    $.fn.yiiGridView.update(
    'department-grid',  
    { type: 'GET', 
    url: 'http://localhost/mmaig_ceo/ceo-control-system/index.php?r=department/dynamicdepartmentlist&ajax=department-grid&company_id=' + companyId

    }
    );
    });
")
?>
于 2012-07-17T03:34:19.880 に答える