subhash
subhash

Reputation: 167

How to provide selected rows to cgridview to display and at same time provide search for columns

Here i am using CActiveDataProvider to pass the selected rows by giving condition and in the view i am displaying in CGridView the columns working fine but at the same time i should provide the search for the columns I am unable to provide the search can any one help. here is my sample code

controller code

public function actionShow($id) 
{
    $model=new StudentResult('search');

    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['StudentResult']))
        $model->attributes=$_GET['StudentResult'];

    $dataProvider=new CActiveDataProvider('StudentResult', array(
        'criteria'=>array(
            'condition'=>"profileId=$id",
            ),
        'pagination'=>array(
            'pageSize'=>20,
        ),
    ));

    $this->render('show',array(
        'model'=>$model,
        'dataProvider' => $dataProvider,
    ));

}

view code

<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'=>'student-result-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'columns'=>array(
    'id',
    'moduleCode',
    'moduleTitle',
    'moduleCredits',
    'grade',
    'creditsObtain',
    'semester',
    /*
    'points',
    'profileId',

    'createdOn',
    'lastModifiedOn',
    'lastModifiedBy',
    */
    array(
        'class'=>'CButtonColumn',
    ),
),
)); ?>

so in view i am displaying the data by giving the 'dataProvider' => $dataProvider so values are displaying correctly but the search is not working if i provide 'dataProvider' => $model->search(); the whole values are displaying and search is working

Upvotes: 1

Views: 1785

Answers (1)

tnchalise
tnchalise

Reputation: 1583

Here you can pass your selected row to the action you want.

   <?php $this->widget('zii.widgets.grid.CGridView', array('id' =>'list-grid',
        'dataProvider'=>$dataProvider->search(),
        'filter' =>$dataProvider, 
        'columns'=>array(
        'id',
        'moduleCode',
        'moduleTitle',
        'moduleCredits',
        'grade',
        'creditsObtain',
        'semester',
         'action'=>array(
            'class'=>'CButtonColumn',
            'header' => 'Edit',
            'template' => '{Edit}',
            'buttons' => array(
                'Edit' =>array(
                'label' => 'Edit',
            'imageUrl'=>Yii::app()>request>baseUrl.'path/to/your/image',// you can use other options
                 'url' => 'Yii::app()->createUrl("/controller/action/id/".someencryption-to-hide-id($data->id))',),             
'visible' => TRUE,      ),
           ),
        ), )); ?>

Your controller action goes like this:

public function actionYourAction() {
        $dataProvider = new ModelName('search');
        $dataProvider -> unsetAttributes();
        // clear any default values
        if (isset($_GET['ModelName'])) {
            $dataProvider -> attributes = $_GET['ModelName'];
        }
        $this -> render('viewPage', array('dataProvider' => $dataProvider, ));

    }

And Model Criteria will be:

 public function search()
    {
        $criteria -> compare('id', $this -> id);
        ...
        return new CActiveDataProvider($this, array('criteria' => $criteria, ));
    }

Let me know am i getting you right.

Upvotes: 1

Related Questions