yii cgridview 排序不起作用 [英] yii cgridview sort not working

查看:28
本文介绍了yii cgridview 排序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的网格视图.当我单击电子邮件"标题时,标题旁边有一个箭头.但是 gridview 不会重新加载以对数据进行排序.
那么,什么是问题?我怎样才能找到它?
谢谢...

========================================================

这是我的观点:

widget('zii.widgets.grid.CGridView', array('id' =>'用户网格','数据提供者' =>$model->search(),'afterAjaxUpdate' =>'function(id, data) { var regex = data.match("data-counter=\"(.*)\"");$("#countUser").text(regex[1]);}','过滤器' =>$模型,'template'=>'{items}

这是我的控制器:

$model = new Users('search');$model->unsetAttributes();$this->render('index', array('模型' =>$模型,));

这是我的搜索代码:

公共函数搜索(){$criteria = 新的 CDbCriteria;$criteria->compare('id', $this->id);$criteria->compare('first_name', $this->first_name, true);$criteria->compare('last_name', $this->last_name, true);$criteria->compare('CONCAT(first_name, \' \', last_name)', $this->full_name, true);$criteria->compare('username', $this->username, true);$criteria->compare('email', $this->email, true);$criteria->compare('avatar', $this->avatar, true);$criteria->compare('home_town', $this->home_town, true);$criteria->compare('birthday', $this->birthday, true);$criteria->compare('gender', $this->gender);$criteria->compare('about_me', $this->about_me, true);$criteria->compare('password', $this->password, true);$criteria->compare('log_as', $this->log_as);$criteria->compare('token', $this->token, true);$criteria->compare('token_device', $this->token_device, true);$criteria->compare('notification_setting', $this->notification_setting, true);$criteria->compare('total_streams', $this->total_streams);$criteria->compare('total_punches', $this->total_punches);$criteria->compare('total_likes', $this->total_likes);$criteria->compare('status', $this->status);$criteria->compare('role_id', $this->role_id);$criteria->compare('long', $this->long);$criteria->compare('lat', $this->lat);$criteria->compare('social_id', $this->social_id);$criteria->compare('created_at', $this->created_at, true);$criteria->compare('updated_at', $this->updated_at, true);$criteria->addSearchCondition('concat(first_name, " ", last_name)', $this->full_name);$criteria->addCondition(array('condition'=>'role_id = 0'));///$criteria->condition = "role_id != 1";$criteria->order = '用户名';$sort = new CSort();$sort->attributes = array('full_name'=>数组('asc'=>'first_name ASC','desc'='=>'first_name DESC',),'*',//这会将所有其他列添加为可排序的);返回新的 CActiveDataProvider($this, array('标准' =>$标准,'排序' =>$排序,));}

解决方案

您的问题似乎是搜索功能中的 $criteria->order = 'username' 行.这表明结果应该如何排序.

如果你把它更新为 $criteria->order = Yii::app()->request->getParam('sort');(可能有一些验证和用户名"的默认值)那么你应该没问题.

This is my grid view. When I click "Email" header, there is a arrow beside the header. But the gridview not reload to sort data.
So, what is problem? How can I find it?
Thanks...

=======================================================

This is my view:

<?php $this->widget('zii.widgets.grid.CGridView', array(

            'id' => 'users-grid',

            'dataProvider' => $model->search(),
            'afterAjaxUpdate' => 'function(id, data) { var regex = data.match("data-counter=\"(.*)\""); $("#countUser").text(regex[1]); }',
            'filter' => $model,
            'template'=>'{items}<div class="nav-controller">{pager}</div>',
            'columns' => array(
                array(
                    'class' => 'CCheckBoxColumn',
                    'selectableRows' => '2',
                ),
                array(
                    'htmlOptions' => array('width'=>10),
                    'header' => 'No.',
                    'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
                ),
                array(
                    'name' => 'full_name',
                    'value' => '$data->first_name . \' \' . $data->last_name',
                ),
                'email',
                array(

                    'name' => 'gender',

                    'value' => '($data->gender == 1) ? Yii::t(\'app\', \'Male\') : Yii::t(\'app\', \'Female\')',

                    'filter' => array('1' => Yii::t('app', 'Male'), '0' => Yii::t('app', 'Female')),

                ),
                array(
                    'name' => 'birthday',
                    'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'model'=>$model, 
                        'attribute'=>'birthday', 
                        'language' => 'en',
                        'i18nScriptFile' => 'jquery.ui.datepicker-ja.js', // (#2)
                        'htmlOptions' => array(
                            'id' => 'datepicker_for_birthday',
                            'size' => '10',
                        ),
                        'defaultOptions' => array(  // (#3)
                            'showOn' => 'focus', 
                            'dateFormat' => 'yy-mm-dd',
                            'showOtherMonths' => true,
                            'selectOtherMonths' => true,
                            'changeMonth' => true,
                            'changeYear' => true,
                            'showButtonPanel' => true,
                        )
                    ), 
                    true), // (#4)
                ),
                'home_town',

                array(
                    'header' => 'Action',
                    'class' => 'CButtonColumn',
                ),

            ),

        )); 

        ?>

This is my controller :

$model = new Users('search');

        $model->unsetAttributes();

        $this->render('index', array(

            'model' => $model,


        ));

This my search code:

public function search() {

        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('CONCAT(first_name, \' \', last_name)', $this->full_name, true);
        $criteria->compare('username', $this->username, true);
    $criteria->compare('email', $this->email, true);    
        $criteria->compare('avatar', $this->avatar, true);
        $criteria->compare('home_town', $this->home_town, true);
        $criteria->compare('birthday', $this->birthday, true);
        $criteria->compare('gender', $this->gender);
        $criteria->compare('about_me', $this->about_me, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('log_as', $this->log_as);
        $criteria->compare('token', $this->token, true);
        $criteria->compare('token_device', $this->token_device, true);
        $criteria->compare('notification_setting', $this->notification_setting, true);
        $criteria->compare('total_streams', $this->total_streams);
        $criteria->compare('total_punches', $this->total_punches);
        $criteria->compare('total_likes', $this->total_likes);
        $criteria->compare('status', $this->status);
        $criteria->compare('role_id', $this->role_id);
        $criteria->compare('long', $this->long);
        $criteria->compare('lat', $this->lat);
        $criteria->compare('social_id', $this->social_id);
        $criteria->compare('created_at', $this->created_at, true);
        $criteria->compare('updated_at', $this->updated_at, true);
        $criteria->addSearchCondition('concat(first_name, " ", last_name)', $this->full_name); 
        $criteria->addCondition(array('condition'=>'role_id = 0'));
        ///$criteria->condition = "role_id != 1";
        $criteria->order = 'username';


        $sort = new CSort();
        $sort->attributes = array(
            'full_name'=>array(
                'asc'=>'first_name ASC',
                'desc'=>'first_name DESC',
            ),
            '*', // this adds all of the other columns as sortable
        );

        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'sort' => $sort,

        ));

    }

解决方案

Your problem seems to be the $criteria->order = 'username' line in your search function. This is what indicates how the results should be sorted.

If you update this to $criteria->order = Yii::app()->request->getParam('sort'); (with some verification and a default value of 'username' perhaps) then you should be fine.

这篇关于yii cgridview 排序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆