dongyue3795 2014-05-20 12:49
浏览 41
已采纳

在CGridView中的列链接

I have a 2 problems in my code: 1) You cannot do a sort by header "Title" when you click on it. 2) You can click on the title, think that will sort alphabetically, an arrow appears, but nothing happens, no sorting. enter image description here

site/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));
    }

This controller has not model.

  • 写回答

1条回答 默认 最新

  • dql1978dql1978 2014-05-20 12:57
    关注

    Modify your dataProvider with something like this

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

    CLinkColumn does not support sorting, only CDataColumn does see CDataColumn and CLinkColumn, you can use Raw type to make sortable Links instead as below

    $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)'
                ),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面