Hi!
First question here, so please be patient with me
We are using the CGridView(s) for filtering and sorting data - the problem is, that the sort-information gets lost after the filter values change and I have no clue where to start looking for the problem, so perhaps someone of you can provide help.
I changed the blog-demo a little bit to demonstrate the problem (see code below, it contains a Belongs-To relation, and I am able to sort over the values - am proud of that, because the documentation of that is a bit weak in my opinion):
[list=4]
[*] Go to manage-post view
[*] Sort one column
[*] Enter filtervalue, e.g. ‘Wel’ in ‘Title’
[*] You will recognize, that the sort order is lost…
[/list]
You may modify the following two files to test the behaviour:
- views/post/admin.php:
<?php
$this->breadcrumbs=array(
'Manage Posts',
);
?>
<h1>Manage Posts</h1>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'title',
'type'=>'raw',
'value'=>'CHtml::link(CHtml::encode($data->title), $data->url)'
),
array(
'name'=>'status',
'value'=>'Lookup::item("PostStatus",$data->status)',
'filter'=>Lookup::items('PostStatus'),
),
[i]array(
'name'=>'authorName',
'type'=>'raw',
'value'=>'$data->author->username',
),[/i]
array(
'class'=>'CButtonColumn',
),
),
)); ?>
- models/Post.php:
<?php
class Post extends CActiveRecord
{
...
private $_oldTags;
[i]public $authorName;[/i]
...
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, content, status', 'required'),
array('status', 'in', 'range'=>array(1,2,3)),
array('title', 'length', 'max'=>128),
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
array('tags', 'normalizeTags'),
[i]array('title, status, authorName', 'safe', 'on'=>'search'),[/i]
);
}
...
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'title' => 'Title',
'content' => 'Content',
'tags' => 'Tags',
'status' => 'Status',
'create_time' => 'Create Time',
'update_time' => 'Update Time',
[i]'authorName' => 'Author',[/i]
);
}
...
/**
* Retrieves the list of posts based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the needed posts.
*/
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('title',$this->title,true);
$criteria->compare('status',$this->status);
[i]$criteria->compare('author.username', $this->authorName);
$sort = new CSort();
$sort->attributes = array(
'title',
'status',
'authorName' => array(
'asc'=>'author.username',
'desc'=>'author.username DESC',
),
);
$sort->defaultOrder = 'status, update_time DESC';
$criteria->with = array('author');
return new CActiveDataProvider('Post', array(
'criteria'=>$criteria,
'sort'=>$sort,
));[/i]
}
}
Does someone have an idea where to tackle the problem? Is it even possible to do in the current version [we are using trunk]?
Thanks in advance and kind regards,
Rudi