简体   繁体   English

Yii CArrayDataProvider排序所有列

[英]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. 嗨,我想为CGridView中的所有字段设置排序方式,而不必手动写下来。 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. 它仅适用于CActiveDataProvider,请参见CSort.php源代码 ,方法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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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