2

我有一个相当复杂的查询,我将 CSqlDataProvider 用于:

        $sql = "
          SELECT  c.id AS id,
                  c.enabled AS enabled,
                  s.store_name AS store_name,
                  s.store_url AS store_url,
                  c.verified AS verified,
                  r.rating_total AS rating
          FROM compete_settings c
          LEFT JOIN stores s
            ON c.compete_id = s.id
          LEFT JOIN (
            (SELECT store_id, rating_total FROM amazon_sellers)
            UNION
            (SELECT store_id, rating_total FROM ebay_sellers)
          ) AS r
            ON c.compete_id = r.store_id
          WHERE c.store_id = :store_id
        ";
        $itemCount = Yii::app()->db->createCommand("SELECT COUNT(id) FROM compete_settings WHERE store_id = $store_id")->queryScalar();
        return new CSqlDataProvider($sql, array(
            'totalItemCount' => $itemCount,
            'params' => array(
                'store_id' => $store_id,
            ),
            'sort' => array(
                'attributes' => array ( 'enabled, store_name, rating' ),
                'defaultOrder' => array('store_name'=>false)
            ),
            'pagination' => array('pageSize' => Yii::app()->user->getState('pageSize_cs', Yii::app()->params['defaultPageSize']),),
        ));

查询效果很好。但是,这些列不可排序。

我确保使用 CSqlDataProvider 遵循Yii CGridview 排序,但它们仍然不是可点击的列标题。

这是我的列数组:

'columns' => array(
    array(
        'header' => 'Enabled',
        'type'=>'raw',
        'name'=>'enabled',
        'htmlOptions'=>array('class'=>'enabled-column'),
        'value' => 'CHtml::checkbox("", $data["enabled"], array("id"=>"c{$data[\'id\']}", "class"=>"enable-switch"))',
    ),
    array(
        'header' => 'Name',
        'htmlOptions' => array('class' => 'account-name-column'),
        'type' => 'raw',
        'name'=>'store_name',
    ),
    array(
        'header' => 'Store Rating',
        'name'=>'rating',
    ),
),

帮助?):

4

1 回答 1

2

结果与我关注的问题中提到的不同,Yii 接受attributes参数作为值数组,而不是具有一个 CSV 值的数组。

所以:

'sort' => array(
    'attributes' => array ( 'enabled', 'store_name', 'rating' ),
    'defaultOrder' => array('store_name'=>false)
),

并不是:

'sort' => array(
    'attributes' => array ( 'enabled, store_name, rating' ),
    'defaultOrder' => array('store_name'=>false)
),
于 2013-07-03T11:57:51.920 回答