CGridView sorting strangefully disappeared?

Hi,

I’ve read a little bit about CGridView sorting on this forum, as good as about enableSort, IDataProvider.sort etc. But the thing is that I wrote my fourth grid view in current project by copying and modifying code of one of last three and all three have sorting working like a charm (like previously) and the fourth one hasn’t got it (header titles not being links).

My grid view definition seems to be correct:


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

(

    	'summaryText'=>'',

    	'id'=>'users-grid',

    	'ajaxUpdate'=>false,

    	'selectableRows'=>1,

    	'emptyText'=>$emptyText,

    	'dataProvider'=>$model->search(),

    	'selectionChanged'=>'function(id){onGridViewSelectionChange($.fn.yiiGridView.getSelection(id))}',

    	'columns'=>array

    	(

    	...

    	)

)

As good as my search function defined in model and used above:


public function search()

{

    	$cfg = new Settings('listy');


    	$criteria = new CDbCriteria;

    	$criteria->condition = 'del = 0';


    	return new CActiveDataProvider(get_class($this), array

    	(

            	'criteria'=>$criteria,

            	'sort'=>array('defaultOrder'=>array('LOGN'=>false)),

            	'pagination'=>array('pageSize'=>$cfg->getValue('user.rec_no'))

    	));

}

So, I have some sort of problems figuring out what did I mess up during code copying/modification, that sorting of this grid view has disappeared? Can any one help or advice here?

BTW: Pagination works fine.

Being in the same topic, I would also like to ask about MVC. Digging through forum, I found out that many posters has search function or data provider, used in CGridView, declared in either controller or even view. Shouldn’t it be in model according to MVC, as I did? I think this is an integral part of model.

Well,

[s]After a deeper analysis, it turned out that sorting does not disappeared strangely. It is missing, because I’m using raw type columns in CGridView and I’m providing data to it through complex functions being evaluated in value parameter.

So the question now is, is there any workaround for this? Can we have CGridView sortable with custom data inside? Or is there a simple choose: either sorting or custom data, but not both at the same time?[/s]

Nope. That was to quick and wrong assumption. I have another view, with another CGridView and it is perfectly sortable, even though I’m using raw type columns and values provided by evaluated expression.

So, I’m back to a damn roots, having no idea, why above presented CGridView is simply not sortable? It must be something obvious, but I keep missing, what that is? :expressionless:

Cheers,

Trejder

As the filtering/sorting is relevant per column… how are your columns declared?

This the example (part of) column definition of my second CGridView, where sorting is not working:


'columns'=>array

(

    	array

    	(

            	'type'=>'raw',

            	'header'=>'Login',

            	'cssClassExpression'=>'GoController::getRowClass($data)',

            	'value'=>'GoController::getAJAXTooltipDiv($data["ID"], $data["LOGN"])'

    	),

    	array

    	(

            	'type'=>'raw',

            	'header'=>'Imię i nazwisko',

            	'cssClassExpression'=>'GoController::getRowClass($data)',

            	'value'=>'GoController::getAJAXTooltipDiv($data["ID"], $data["username"])'

    	),

    	...

It is a copied and modified version of following columns structure (CGridView definition) of another CGridView, where sorting is perfectly working (except of course first column, where it is forced to be blocked):


array

(

    	'type'=>'raw',

    	'header'=>'L. p.',

    	'sortable'=>false,

    	'htmlOptions'=>array('style'=>'width: 35px'),

    	'value'=>'CFR::generateAJAXZleceniaTooltipDiv($data->ID, $data->ROWNUMID)',

),

array

(

    	'name'=>'BCODE',

    	'type'=>'raw',

    	'htmlOptions'=>array('style'=>'width: 115px'),

    	'value'=>'CFR::generateAJAXZleceniaTooltipDiv($data->ID, $data->BCODE)',

),

array

(

    	'name'=>'DATR',

    	'type'=>'raw',

    	'htmlOptions'=>array('style'=>'width: '.$dateWidth),

    	'value'=>'CFR::generateAJAXZleceniaTooltipDiv($data->ID, $data->DATR)',

),

...

As you can see, the only main difference is that first CGridView is fed by CSqlDataProvider source, so it uses array-style access, while second one is fed by CActiveDataProvider, so it uses object-style access to values. But this should have absolutely no influence on whether grid view is or isn’t sortable. Am I right?

Before posting this post I checked four time CGridView definition and either I’m blind or they’re nearly identical. They only differ in id, summary text and selection changed functions and one of them (with sorting working) is using non-standard paginator (my own, custom class). But all these factors should not affect filtering. Again, am I right?

I believe this is something obvious, but I can’t find it myself! :expressionless:

Could be the "name" is missing - http://www.yiiframew…umn#name-detail

Yeap! That solves my problem!

I would say, that documentation text cited by you is a bit confusing. I’ve resigned from using name because I was pretty sure that I should use either value or name. And that using name hasn’t got much about sorting or filtering.

Maybe I’m wrong, but writing something like that: “If value is specified, this property will be ignored. If the column needs to be sortable or filtered, name property must be set, even if column value is evaluated using expression provided in value property.

I refrased that and removed the word “ignored” so that users do not think that it’s either value or name… but will be visible on the site only when the new version is released

http://code.google.com/p/yii/source/detail?r=3448

Many thanks! :]