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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动