CGridView with CArrayDataProvider Sorting

Yii ver.: 1.1.5

CGridView looks as follows:


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $dataProvider,

    'columns' => array(

        'customer',    // -> reference nr. 1

        array(         // -> reference nr. 2

            'value' => '$data["customer"]',

            'header' => 'Customer',

        ),

    ),

));

I can sort CGridView (by clicking table header) in case nr 1 and cannot in case nr 2 when column is set as an array.

$dataProvider is set in Controller as follows:


       

 $rawData = Project::model()->getAllProjects();

        $sort = new CSort;

        $sort->defaultOrder = 'customer DESC';

        $sort->attributes = array('customer');

        $dataProvider = new CArrayDataProvider($rawData, array('id' => 'projects', 'sort' => $sort));



I find it also odd that it is required to explicitly set

CSort::attributes to be able to sort by particular column (as opposing to CActiveDataProvider).

Since this is pretty high up on a Google search for sorting with CArrayDataProvider, I will provide a little bit of information:

Here is a pretty good SO question that has been answered to get a sort working on a CGridView with CArrayDataProvider: http://stackoverflow.com/questions/10944119/yii-cgrid-pagination-and-sorting-with-carraydataprovider-doesnt-work

Also, if anyone is using POSTed data to determine the contents of the grid view, i.e. a date range, here is a way to make sure that data is persisted on each sort request:

  1. Ensure the action in your controller can accept both POST and GET request data by using the $_REQUEST variable

  2. Use the $sort->params attribute to set the request data into the CSort object (This data will be appended to the request URL when sorting




  public function actionIndex()

  {

    $model = new PixelReport;

    $reportDataProvider = false;


    // If dates have been posted

    // get the commission data

    if (isset($_REQUEST['PixelReport'])) {

      // Validate the date range

      $model->attributes = $_REQUEST['PixelReport'];

      if ($model->validate()) {

        // Get your data from the model

        $data = $model->getReportData();

        

        // Create a new CSort object

        $sort = new CSort();

        

        // One attribute for each column of data

        $sort->attributes = array(

          'campaign_title',

          'unique_clicks',

          'leads',

          'total',

        );

        // Set the default order

        $sort->defaultOrder = array(

          'total'=>CSort::SORT_DESC,

        );

        

        // Persist the request by adding the dates to the sort params

        $sort->params = array(

          'PixelReport' => $_REQUEST['PixelReport'],

        );

  

        // Create data provider for the Grid View

        $reportDataProvider = new CArrayDataProvider($data, array(

          'id'=>'campaign-summary',

          'keyField'=>'campaign_id',

          'sort'=>$sort,

          'pagination'=>array(

            'pageSize'=>20,

          ),

        ));

      }

    }


    $this->render('index', compact('model', 'reportDataProvider'));


  }



[color="#008000"]/* Moved from "Bug Discussions" to "General Discussion for Yii 1.1.x */[/color]

This should work:




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $dataProvider,

    'columns' => array(

        'customer',    // -> reference nr. 1

        array(         // -> reference nr. 2

            'name' => 'customer',    // this is required

            'value' => '$data["customer"]',

            'header' => 'Customer',

        ),

    ),

));



http://www.yiiframework.com/doc/api/1.1/CDataColumn#name-detail