Yii cgrid视图排序

Yii cgrid视图排序,yii,Yii,我正在我的网站上的访客报告部分工作,该部分建立在yii中。 我的cgrid视图记录如下所示 HostName Visitors Counts Detail facbook.com 40 click here google.com 150 click here yahoo.com 90 click here 我只想随着更

我正在我的网站上的访客报告部分工作,该部分建立在yii中。 我的cgrid视图记录如下所示

HostName        Visitors Counts      Detail
 facbook.com     40                   click here   
 google.com      150                  click here  
 yahoo.com       90                   click here 
我只想随着更高的访问者在cgrid视图中向上移动,对这些数据进行排序。就像google.com为我提供了最高的访问量一样,它也会向上移动。 这怎么可能? 有什么建议吗。当数据显示在CGridview中时,我在运行时是Count visitor,我创建了一个函数并将其作为参数传递给hostname。我怎样才能把它分类

在CGridView中

array( 'name'=>'id',
        'value'=>'visits::count_VisitorsByHostName($data->hostname)',
        'header'=>'Visits'
   ),
在Model.php中

public function count_VisitorsByHostName($hostname){            

    $connection = Yii::app()->db;               
    $query = "SELECT COUNT(id) as Visitors FROM visits WHERE hostname = '$hostname'";
    $connection->createCommand($query)->queryAll();             
    $count_visitors =  $count_record[0]['Visitors'];                
    return $count_visitors;                     
}

只需在控制器中定义函数并调用gridview值

array('name'=>'id','value'=>array($this,'functionName')),
并在控制器中添加此操作:

public function functionName($data,$row){

//Your logic//
Return value;
}

只需在控制器中定义函数并调用gridview值

array('name'=>'id','value'=>array($this,'functionName')),
并在控制器中添加此操作:

public function functionName($data,$row){

//Your logic//
Return value;
}

您可以通过将访问者计数添加到初始查询中来实现这一点,然后访问者计数可以成为CGridView中的可排序列

这样你的控制器看起来就正常了

class VisitsController extends Controller
{
    ...

    public function actionIndex()
    {
        $model=new Visits('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Visits']))
            $model->attributes=$_GET['Visits'];
        $this->render('index',array(
            'model'=>$model,
        ));
    }

    ...
}
在您的模型中,您需要声明一个变量来保存count搜索,然后像这样将其添加到search()方法中

class Visits extends CActiveRecord
{
    /**
     * @var array Allows model filtering by visit count
     */
    public $visit_count;

    ...

    // Remember to declare it as safe
    public function rules()
    {
        return array(
            ...
            array(... ,'visit_count', ..., 'safe', 'on'=>'search'),
            ...
        );
    }

    ...

    // Give it a label if you wish
    public function attributeLabels()
    {
        return array(
            ...
            'visit_count' => 'Visitors Counts',
            ...
        );
    }

    ...

    // Now add to your search method
    public function search()
    {
        $criteria=new CDbCriteria;
        $visit_table = Visits::model()->tableName();
        $visit_count_sql = "(select count(*) from `$visit_table` where `$visit_table`.`hostname` = `t`.`hostname`)";
        $criteria->select = array(
            '*',
            $visit_count_sql . " as `visit_count`",
        );

        $criteria->compare($visit_count_sql, $this->visit_count);

        // Set the CActiveDataProvider to order by the visitor count
        $criteria->order = 'visit_count DESC';

        ...
        // All your other criteria code
        ...

        // And add to the CActiveDataProvider
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                'attributes'=>array(
                    'visit_count'=>array(
                        'asc'=>'visit_count',
                        'desc'=>'visit_count DESC',
                    ),
                '*',
                ),
            ),
            'pagination'=>array(
                'pageSize'=>100,
            ),
        ));
    }

    ...
}
然后可以将其添加到视图中的CGridView中

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'visit-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'hostname',
        'visit_count',
        array(
            // Whatever your detail column looks like!
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

我还没有测试过,但我已经从我自己的一个项目中的一个更复杂的模型中剥离了它,因此它可能需要一些编辑,变量名很可能需要为你的应用程序更改,但基本的流程是存在的。

你可以通过将访问者计数添加到初始查询中来实现这一点,然后,您的访问者计数可以成为CGridView中的可排序列

这样你的控制器看起来就正常了

class VisitsController extends Controller
{
    ...

    public function actionIndex()
    {
        $model=new Visits('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Visits']))
            $model->attributes=$_GET['Visits'];
        $this->render('index',array(
            'model'=>$model,
        ));
    }

    ...
}
在您的模型中,您需要声明一个变量来保存count搜索,然后像这样将其添加到search()方法中

class Visits extends CActiveRecord
{
    /**
     * @var array Allows model filtering by visit count
     */
    public $visit_count;

    ...

    // Remember to declare it as safe
    public function rules()
    {
        return array(
            ...
            array(... ,'visit_count', ..., 'safe', 'on'=>'search'),
            ...
        );
    }

    ...

    // Give it a label if you wish
    public function attributeLabels()
    {
        return array(
            ...
            'visit_count' => 'Visitors Counts',
            ...
        );
    }

    ...

    // Now add to your search method
    public function search()
    {
        $criteria=new CDbCriteria;
        $visit_table = Visits::model()->tableName();
        $visit_count_sql = "(select count(*) from `$visit_table` where `$visit_table`.`hostname` = `t`.`hostname`)";
        $criteria->select = array(
            '*',
            $visit_count_sql . " as `visit_count`",
        );

        $criteria->compare($visit_count_sql, $this->visit_count);

        // Set the CActiveDataProvider to order by the visitor count
        $criteria->order = 'visit_count DESC';

        ...
        // All your other criteria code
        ...

        // And add to the CActiveDataProvider
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                'attributes'=>array(
                    'visit_count'=>array(
                        'asc'=>'visit_count',
                        'desc'=>'visit_count DESC',
                    ),
                '*',
                ),
            ),
            'pagination'=>array(
                'pageSize'=>100,
            ),
        ));
    }

    ...
}
然后可以将其添加到视图中的CGridView中

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'visit-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'hostname',
        'visit_count',
        array(
            // Whatever your detail column looks like!
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

我还没有测试过,但我已经从我自己的一个项目中的一个更复杂的模型中剥离了它,因此它可能需要一些编辑,变量名很可能需要为你的应用程序进行更改,但基本流程已经存在。

嗨,斯图,首先非常感谢。我的问题是解决一些我在谷歌上搜索了4天但仍然没有找到任何解决方案的问题。如果可以,谢谢,我只想使用自定义排序,我的意思是哪个主机名可以提供足够的访问者,这个主机在CGridview中向上移动。可能需要一个自定义功能,但如何可以,我无法解决这个问题<代码>主机名访客计数详情facbook.com 40点击这里google.com 150点击这里yahoo.com 90点击这里我想谷歌移动到最上面一行,请问我该如何解决这个问题?我遇到了很多麻烦。有了上面的功能,它应该可以通过
访问\u count DESC
自动订购?你也可以点击访问次数栏标题,按该栏排序?嗨,斯图,首先非常感谢你。我的问题是解决我在谷歌搜索了4天但仍然没有找到任何解决方案的问题。如果可以,谢谢,我只想使用自定义排序,我的意思是哪个主机名可以提供足够的访问者,这个主机在CGridview中向上移动。可能需要一个自定义功能,但如何可以,我无法解决这个问题<代码>主机名访客计数详情facbook.com 40点击这里google.com 150点击这里yahoo.com 90点击这里我想谷歌移动到最上面一行,请问我该如何解决这个问题?我遇到了很多麻烦。有了上面的功能,它应该可以通过
访问\u count DESC
自动订购?您还应该能够单击访问次数列标题,按该列排序?