Cgridview In Fronted. Sortable Without $Model->Search()

Hello :)

I don’t know if it is possible, but suppose it must be. I need to present some data in frontend view - the list of companies in tabular form:

Company name 1 - Country 1 - Website 1 - etc…

Company name 2 - Country 2 - Website 2 - etc…

etc…

I’m trying to use CGridView for this purpose.

My controller:


public function actionList()

{


    $sort = new CSort();

    $sort->attributes = array(

        'defaultOrder'=>'company_name DESC',

        'company_name'=>array(

            'asc'=>'company_name ASC',

            'desc'=>'company_name DESC',

        ),

        'country'=>array(

            'asc'=>'country ASC',

            'desc'=>'country DESC',

        ),

    );


    $criteria = new CDbCriteria();

    $criteria->order = "company_name DESC";

    $criteria->condition = 'approve = :approve';

    $criteria->params = array(':approve'=>1);       


    $dataProvider = new CActiveDataProvider('EuCompanies', array('criteria'=>$criteria,'sort'=>$sort));


    $this->render('list', 

    array('dataProvider'=>$dataProvider,

                                     ));

}

My view:


<?php

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

      'dataProvider' => $dataProvider,                                                             

      'columns'=>array(                                                                                                                                                                            

        'company_name',

        'country',

        'company_website',

        'economic_sector',

        'contact_person',

        'email',

        'phone_number'                                                                                                                                                                                  

      ),                                                                                                 

    ));

    ?>

The output is a nice grid, which takes into account the selection criteria, and clickable “Company name” and “Country”, but clicking on them doesn’t make the column sort. I guess it is because sort can’t be done without using $model->search() in dataProvider, or I’ve just done something wrong?

I think the $criteria->order is over-riding any other sorting.

Lol, indeed! I’ve spent all night on trying make it work, but the matter was in $criteria->order. Funny. Thank you again :D

Could I have another question?

I have:


'defaultOrder'=>'company_name ASC',

in Sort->attributes, but my data is not sorted by default at all, although clicking on column-headings does it’s job perfect. Where could it be set?

Thank you again

Done!


$sort = new CSort();

        $sort->defaultOrder = 'company_name ASC';

        $sort->attributes = array(

            'company_name'=>array(

                'asc'=>'company_name ASC',

                'desc'=>'company_name DESC',

            ),

            'country'=>array(

                'asc'=>'country ASC',

                'desc'=>'country DESC',

            ),

            

        );

:D

there you go. I’m fishing for up votes :huh: