简体   繁体   中英

Yii CArrayDataProvider sorting all columns

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM