Question

I have a rather complex query, that I'm using CSqlDataProvider with:

        $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']),),
        ));

The query works great. However, the columns are not sortable.

I've made sure to follow Yii CGridview sorting with CSqlDataProvider, but they are still not clickable column headers.

Here are my column arrays:

'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',
    ),
),

Help? ):

Was it helpful?

Solution

Turns out unlike what was mentioned in the question I followed, Yii accepts the attributes param as an array of values and not an array with one CSV value.

So:

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

And not:

'sort' => array(
    'attributes' => array ( 'enabled, store_name, rating' ),
    'defaultOrder' => array('store_name'=>false)
),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top