2

我正在使用 Yii 框架。

我使用以下方法在我的 cgridview 过滤器字段之一中设置了一个值:

这是我的 jQuery 为搜索字段赋值:

$('#gridviewid').find('input[type=text],textarea,select').filter(':visible:first').val('".$_GET['value']."');

这里是我调用 cgridview 的 PHP:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bills-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'cssFile'=>Yii::app()->baseUrl . '/css/gridview.css',
'pager'=>array(
    'class'=>'AjaxList',
    'maxButtonCount'=>25,
    'header'=>''
),
'columns' => $dialog->columns(),
'template'=>"<div class=\"tools\">".$dialog->link()."&nbsp;&nbsp;&nbsp;".CHtml::link($xcel.'  Export to excel', array('ExcelAll'))."</div><br />{items}{summary}<div class=\"pager-fix\">{pager}</div>",));

该值出现在搜索字段中,我的 cgridview 正常工作,没有任何问题,但我无法触发 cgridview 刷新或过滤。有谁知道在页面加载后使用预定义值触发 cgridview 过滤的人?

任何帮助将不胜感激,如果您需要更多信息,请告诉我。

谢谢你。

4

3 回答 3

9

您可以在不修改任何客户端代码的情况下解决问题。在您的控制器操作中,只需设置属性的默认值,如下所示

public function actionAdmin()
{
    $model = new Bills();
    $model->unsetAttributes();
    $model->attribute_name="default filter value";//where attribute_name is the attribute for which you want the default value in the filter search field
    if(isset($_GET['Bills'])){
        $model->attributes = $_GET['Bills'];
    }

    $this->render('admin',array('model'=>$model));
}
于 2012-05-11T12:49:59.183 回答
1

查看 gii 生成的“默认”索引操作:

public function actionIndex()
{
    $model = new Bills();
    $model->unsetAttributes();
    if(isset($_GET['Bills'])){
        $model->attributes = $_GET['Bills'];
    }

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

因此,如果您添加一行:$model->attribute = 'test';,您就完成了。“属性”当然是必须具有默认过滤器值的属性(在这种情况下,值是“测试”):)。所以你的代码看起来像:

public function actionIndex()
{
    $model = new Bills();
    $model->unsetAttributes();
    if(isset($_GET['Bills'])){
        $model->attributes = $_GET['Bills'];
    }

    if(!isset($_GET['Bills']['attribute']) {
        $model->attribute = 'test';
    }

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

当然,只要您不在其过滤器字段中键入任何内容,您的属性就会设置一个测试值(在过滤器中)。我希望这就是你要找的。您的过滤器应该像往常一样工作。

对不起,我的英语不好 :)

问候

于 2012-05-11T07:14:06.597 回答
0

你可以使用 Yii 的更新:

$.fn.yiiGridView.update('bills-grid', {
 type: 'GET',
 url: <?php echo Yii::app()->createUrl('controller/action') ?>"?Class[attribute]=<?php echo $_GET['value'] ?>
 success: function() {
  $.fn.yiiGridView.update('bills-grid');
 }
});

我就是这样做的,只需更改 URL,它应该是 gridview 的相同控制器操作,并将 URL 参数更改为其中表示的结构,应该像Bills[attribute]=value

于 2012-05-10T20:43:45.290 回答