rikardo  
          
              
                June 7, 2010, 11:18am
               
              1 
           
         
        
          I have a data model I want to present with a "grid view", I borrow some data from another table so I have to define the columns manually:
	'columns'=>array(
		array(
			'header' => 'Kundnummer',
			'name' => 'aux_kundnummer',
			'filter' => CHtml::activeTextField($model, 'aux_kundnummer'),
			'value' => '$data -> kund -> Kundnummer',
		),
                ...
I want that column to be sortable how can I do that?
Tried to add the ‘sortable’ => true to the array list but that don’t do it.
         
        
           
         
            
       
      
        
          
          
            zaccaria  
          
              
                June 7, 2010, 12:05pm
               
              2 
           
         
        
          You have to set the property "attribute" of $dataProvider->sort
         
        
           
         
            
       
      
        
          
          
            rikardo  
          
              
                June 7, 2010, 12:20pm
               
              3 
           
         
        
          How do you mean? Show me some code plz.
This is the widget code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'abonnemang-grid',
	'dataProvider' => $model -> search(),
	'filter'=>$model,
	'columns'=>array(
                ...
	),
)); ?>
 
        
           
         
            
       
      
        
          
          
            zaccaria  
          
              
                June 7, 2010, 12:25pm
               
              4 
           
         
        
          
<?php 
    $dataProvider=$model -> search()
    $dataProvider->sort->attributes=array(
        'all other field',
        'kund.kundnummer',
    );
    $dataProvider->criteria->with('kund');
    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'abonnemang-grid',
        'dataProvider' => $dataProvider,
        'filter'=>$model,
        'columns'=>array(
                ...
        ),
)); ?>
 
        
           
         
            
       
      
        
          
          
            rikardo  
          
              
                June 7, 2010,  1:07pm
               
              5 
           
         
        
          The title of the column becomes a link if everything look like this:
<?php
	$dataProvider = $model -> search();
	$dataProvider -> sort -> attributes = array(
		'kund.Kundnummer',
	);
	$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'abonnemang-grid',
		'dataProvider' => $dataProvider,
		'filter' => $model,
		'columns'=>array(
                        ...
			array(
				'name' => 'kund.Kundnummer',
			),
                        ...
		),
	));
?>
But the link does not sort the list so it’s of no use. And now that field is NOT searchable. If I want it to be searchable by the ‘kund.Kundnummer’ column, the code need to be changed to this:
<?php
	$dataProvider = $model -> search();
	$dataProvider -> sort -> attributes = array(
		'kund.Kundnummer',
	);
	$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'abonnemang-grid',
		'dataProvider' => $dataProvider,
		'filter' => $model,
		'columns'=>array(
                        ...
			array(
				'header' => 'Kundnummer',
				'name' => 'aux_kundnummer',
				'filter' => CHtml::activeTextField($model, 'aux_kundnummer'),
				'value' => '$data -> kund -> Kundnummer',
			),
                        ...
		),
	));
?>
But now the title is not even a link…
         
        
           
         
            
       
      
        
        
          hi
maybe this help you … or not 
in the model
	public function search()
	{
  $sort = new CSort();
  $sort->attributes = array(
   'date'=>array(
      'asc'=>'t.date',
      'desc'=>'t.date desc',
    ),
   'name'=>array(
      'asc'=>'t.name',
      'desc'=>'t.name desc',
    ),
   'id_sector'=>array(
      'asc'=>'sector.descripcion',
      'desc'=>'sector.descripcion desc',
    ),
);
		$criteria=new CDbCriteria;
                $criteria->with=array('sector'); 
		$criteria->compare('t.date',$this->date,true);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('sector.descripcion',$this->id_sector,true);
		return new CActiveDataProvider('Ticket', array(
			'criteria'=>$criteria,
                        'sort'=>$sort,
		));
	}
in the view
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'ticket-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'date',
		'name',
                array('name'=>'id_sector','sortable'=>'true','value'=>'$data->sector->descripcion'),
		//'id_sector',
		
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>
 
        
           
         
            
       
      
        
          
          
            rikardo  
          
              
                June 10, 2010,  8:51am
               
              8 
           
         
        
          THANKS! It helped me out, did like this:
In the search() method in the model
		$sort = new CSort;
		$sort -> attributes = array(
			'AktivStatus' => array(
				'asc'=>'AktivStatus',
				'desc'=>'AktivStatus desc',
			),
		
			'aux_kundnummer' => array(
				'asc'=>'kund.Kundnummer',
				'desc'=>'kund.Kundnummer desc',
			),
		);
		return new CActiveDataProvider('Abonnemang', array(
			'criteria'=>$criteria,
			'sort' => $sort,
		));
In the view
	$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'abonnemang-grid',
		'dataProvider' => $dataProvider,
		'filter' => $model,
		'columns'=>array(
			array(
				'header' => 'Kundnummer',
				'name' => 'aux_kundnummer',
				'filter' => CHtml::activeTextField($model, 'aux_kundnummer'),
				'value' => '$data -> kund -> Kundnummer',
			),
			
			'AktivStatus',
			
			array(
				'class'=>'CButtonColumn',
			),
		),
	));