CListView - sortableAttributes - How to set default order?

Hey,

i use the following CListView in my View.

How can I order the List by "Upload Date" per default (ASC or DESC)?

THX!


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

		'dataProvider'=>$dataProvider,

		'itemView'=>'_view',

		'enableSorting'=>1,

		'sortableAttributes'=>array(

			'dateCreate' => 'Upload Date',

			'views'=>'Views',

			'comments' => 'Comments',

			'bookmarks' => 'Bookmarks'

			)

	)); ?>


$dataProvider->sort->defaultOrder='dateCreate ASC';

YEAH!

that solved it THX

Thanks, I also needed this

That works but the user does not see the sorting arrow next to the "Sort by: " field,

like it happens if the user sorts it manually…

So the user might not be aware that a sort is in effect.

Any ideas on how to perform a default sort that will show in the ListView widget?

As the documentation say to show the arrow you need to use the array notation - http://www.yiiframework.com/doc/api/1.1/CSort#defaultOrder-detail

Thank you, that did it.

Just a note. Using the ‘=>CSort::SORT_DESC’ I got

Fatal error: Undefined class constant ‘SORT_DESC’

But using the constant directly without the class ‘=>SORT_DESC’

(hence using the php Core Predefined Constants), it worked.

Is there something missing in my main config, or something?

What yii version do you use?

These has been added to the 1.1.10 version… not sure why you are getting the error

I had an older version, downloaded it 2 weeks ago or something.

Thanks for the clarification. I updated the framework and indeed it is now included.

Thanks again.

Where does this go? I’m not quite clear on where I would call this (or what needs to go in the widget when it is called). Would it go in the controller after the widget is defined? In the view as part of the definition of the datacontroller? As a subsequent call?

I’m still not sure on where this particular syntax would go. But to partially answer my need:

You can add sort as a criteria in the controller: see here

I did this in my particular case by creating a criteria and then adding it to the dataprovider:




        // initialise criteria object

        $criteria=new CDbCriteria(array(

            'order'=>'start_date DESC,end_date DESC', // this uses two criteria: first start_date, then end_date.

        ));


...


        $dataProvider=new CActiveDataProvider('WorkflowInstance', array(

            'criteria'=>$criteria,

            'pagination'=>false,

        ));

 






mdomba… would you mind elaborating on the array notation in the documentation. Since the documentation says that is for CSort and I can’t seem to figure out how I would call it for the CListView where I am trying to use it.

Any help would be much appreciated.

You have to set it on your dataprovider object. The default CRUD creates this in the search() method of your model. So you can set it either inside this method or on the data provider object which it returns (that’s what the first post here implies).





public function search()

{

    $criteria=new CDbCriteria;


    /* ... your search criteria here ... */


    return new CActiveDataProvider($this, array(

        'criteria'=>$criteria,

        'sort'=>array(

            'defaultOrder'=> /* your default order here */

            'attributes'=>array(

                /* ... your sort attributes here ... */

            ),

        ),

    ));

}



Consider the data provider as the object which is also responsible for paginating and sorting of your data. It uses some other objects inside to achieve this. You should check out the manual of CActiveDataProvider.

Hi community

I am a novice yii user and I have read this thread, tried the suggestions, and I am using 1.1.13 version of framework but I get a CException with message "Property "CListView.defaultOrder" is not defined.". What does this mean and how do I fix it? I am trying to display in my index view a list of events by order of eventDate attribute not the primary key as does now. This is my code so far


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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

        'defaultOrder'=>array('eventDate'=>CSort::SORT_DESC,), //TRYING TO SORT THE LIST OF EVENTS

)); ?>

Is this where I should be editing or is it the loadModel() function in the controller?

@kingcharles just above your post @Mike explained it all - "You have to set it on your dataprovider object"

Thanks. I see it now

thanks .this is also useful for me :)