2015-04-20 69 views
1

我是Yii的新手。我有一个下拉列表和一个CGridView。这个想法是,我想根据用户在下拉列表中选择的内容来过滤gridview中显示的记录。我读过几篇教程,几乎所有的教程都很像this onefn.yiiGridView.update事件不发射

不幸的是,代码似乎并没有触发gridview更新事件。

这里是根据教程我的代码

控制器

public function actionIndex() 
{ 

     $criteria = (isset($_GET['id-dropdown'])) ? 
       array(
        'condition' => 'account = ' . $_GET['id-dropdown'], 
       ): array(); 

     $options = array(
      'criteria' =>$criteria, 
      'pagination' => array(
       'pagesize' => 100, 
      ), 
     ); 
     $modelAccount = new Account(); 
     $dataProvider = new CActiveDataProvider('Jurnal', $options); 

     $selected_account = (isset($_GET['id-dropdown'])) ? $_GET['id-dropdown']: '101'; //101 is the default 

    $this->render('index', array(
       //'modelCustom'=>$modelCustom, 
'modelAccount'=>$modelAccount, 
       'dataProvider'=>$dataProvider, 
       'selected_account' => $selected_account)); 
} 

这是我的看法

<?php 
Yii::app()->clientScript->registerScript('items_update', "$('#id-dropdown').change(function(){ 
     alert('ok'); //this works 
     $.fn.yiiGridView.update('jurnal-grid', { 
       type:'GET', 
       data: $(this).serialize(), 
       success=> 
        js:function() { $.fn.yiiGridView.update('jurnal-grid');} 
       } 
      } 
     ); 
    }); 
    return false;", 
    CClientScript::POS_READY); 
?> 
<h1>View Per Account</h1> 
<div class="form"> 
<?php 

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'menu-dropdown-form', 
    'enableAjaxValidation'=>true, 
)); 

echo $form->labelEx($modelAccount, $selected_account); 
$criteria = new CDbCriteria(); 
$criteria->order = 'id ASC'; 
$account = Account::model()->findAll($criteria); 
$accountlist = CHtml::listData($account, 'id', 'description'); 
echo CHtml::dropDownList('id-dropdown', '$selected_account', $accountlist); 
$this->endWidget(); 

?> 
</div> 
<?php 
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'jurnal-grid', 
    'dataProvider'=>$dataProvider, 
    'columns' => array(
     'tanggal', 
     'account', 
     array(
      'class' => 'CButtonColumn', 
     ), 
    ), 
)); 
?> 

请帮帮我,谢谢你提前

+0

我觉得你在这里的js'成功=> JS有错误:函数(){$ .fn.yiiGridView。更新('jurnal-grid');} }' – ineersa

回答

0

尝试更换

success=> 
    js:function() { $.fn.yiiGridView.update('jurnal-grid');} 

success=> "$.fn.yiiGridView.update('jurnal-grid');" 

无需使用js:function

+0

我以前也试过,还是不行 – user2552108

0

取而代之的是:

$.fn.yiiGridView.update('jurnal-grid', { 
    type:'GET', 
    data: $(this).serialize(), 
    success=> 
     js:function() { $.fn.yiiGridView.update('jurnal-grid');} 
    } 
}); 

试试这个:

$.fn.yiiGridView.update('jurnal-grid', { 
    data: $(this).serialize() 
});