Hi I would like to set a sorting to all fields in a CGridView without having to write them down manually. Any ideas?

$this->widget('application.widgets.GridView', array(
                'dataProvider'=>new CArrayDataProvider($offers,array(
                    'sort'=>array(
                        'attributes'=> 'AUTOMATICALLY TAKE ALL',
                    ),
                    'pagination'=>array(
                            'pageSize'=>10,
                        ),
                    )
                ),
                'enableSorting'=>true,
                ...

Now I have to write all columns(=attributes) I want to sort on. But I just want all that are defined in the grid.

有帮助吗?

解决方案

You don't need asterisk feature, 'cause you can use keys from array:

$this->widget('application.widgets.GridView', array(
                    'dataProvider'=>new CArrayDataProvider($offers,array(
                        'sort'=>array(
                            'attributes'=> array_keys($offers[0]),
                        ),  
                        ...

其他提示

It should work only for CActiveDataProvider, see CSort.php source code, method resolveAttribute.

public function resolveAttribute($attribute)
{
    if($this->attributes!==array())
        $attributes=$this->attributes;
    elseif($this->modelClass!==null)
        $attributes=$this->getModel($this->modelClass)->attributeNames();
    else
        return false;
    foreach($attributes as $name=>$definition)
    {
        if(is_string($name))
        {
            if($name===$attribute)
                return $definition;
        }
        elseif($definition==='*')
        {
            if($this->modelClass!==null && $this->getModel($this->modelClass)->hasAttribute($attribute))
                return $attribute;
        }
        elseif($definition===$attribute)
            return $attribute;
    }
    return false;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top