1

我是 Yii 框架的新手。我想创建一个页面。这是当下拉列表更改时,listview/gridview 将通过下拉值更改。

这是我的看法

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

    }

但它不起作用。请帮我。

Regads Tharsoe

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 回答