在CGridView中的列链接

时间:2014-05-20 12:49:40

标签: php yii cgridview

我的代码中有2个问题: 1)你不能按标题进行排序"标题"当你点击它。 2)你可以点击标题,认为按字母顺序排序,出现一个箭头,但没有任何反应,没有排序。 enter image description here

网站/ index.php的

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
        'title' => array(
            'class'=>'CLinkColumn',
            'labelExpression' => '$data->title',
            'urlExpression'=>'Yii::app()->request->baseUrl."/page/".$data->id',
            'header'=>'Title',
            'headerHtmlOptions' => array('style'=>'width:250px;'),
        ),
        'created' => array(
            'name' => 'created',
            'value' => 'date("j.m.Y H:i", $data->created)',
            'headerHtmlOptions' => array('style'=>'width:90px;'),
        ),
        'category_id' => array(
            'name' => 'category_id',
            'value' => '$data->category->title',
            'filter' => Category::allCategory(),
            'headerHtmlOptions' => array('style'=>'width:150px; text-align:center;'),
        ),
        'subcategory_id' => array(
            'name' => 'subcategory_id',
            'value' => '$data->subcategory->title',
            'filter' => Subcategory::allCategory(),
            'headerHtmlOptions' => array('style'=>'width:150px; text-align: center;'),
        ),
        'condition_id' => array(
            'name' => 'condition_id',
            'value'=> '$data->condition->title',
            'filter' => Condition::allAttributes(),
            'headerHtmlOptions' => array('style'=>'width:90px;'),
        ),
    ),
));

SiteController

public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('Page', array(
            'criteria'=>array(
                'order'=>'id DESC',
            ),
            'pagination'=>array(
                'pageSize'=>30,
            ),
        ));

        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index', array('dataProvider'=>$dataProvider));
    }

此控制器没有型号。

1 个答案:

答案 0 :(得分:0)

使用类似这样的内容修改dataProvider

 $dataProvider=new CActiveDataProvider('Page', array(
            'criteria'=>array(
                'order'=>'id DESC',
            ),
            'sort' => array(
                'attributes' => array(
                    'title',
                ),
            ),
            'pagination'=>array(
                'pageSize'=>30,
            ),
        ));

CLinkColumn不支持排序,只有CDataColumn支持排序 请参阅CDataColumnCLinkColumn,您可以使用Raw类型来制作可排序的链接,如下所示

$this->widget('zii.widgets.grid.CGridView', array(
     'dataProvider' => $dataProvider,
     'enableSorting'=>true,
     'columns' => array(
            'title' => array(
                   'name'=>'title',
                   'headerHtmlOptions' => array('style'=>'width:250px;'),
                   'type'=>'raw',
                   'value'=>'CHtml::link($data->title,Yii::app()->request->baseUrl."/page/".$data->id)'
            ),