[SOLVED] CGridView via renderPartial() inside of CJuiTabs

Hi Yii Master, i’m newbie in Yii…

I have problem about Cgridview which is rendered using renderPartial inside of CJuitabs.

Here’s the CJuitabs (index.php):




$prodi=$this->renderPartial('pages/_tabProdi', NULL, $return=true);

$aak=$this->renderPartial('pages/_tabAAK',NULL,$return=true);

$au=$this->renderPartial('pages/_tabAU',NULL,$return=true);

$this->widget('zii.widgets.jui.CJuiTabs', array(

    'tabs'=>array(

        'Program Studi (Prodi)'=>array('content'=>$prodi, 'id'=>'prodi'),

        'Administrasi Akademik & Kemahasiswaan (AAK)'=>array('content'=>$aak, 'id'=>'aak'),

        'Administrasi Umum (AU)'=>array('content'=>$au, 'id'=>'au'),

    ),

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>false,

    	'event'=>'mouseover',

    ),

));

i have 3 CGridview files (_tabProdi.php, _tabAAK.php and _tabAU.php)

and here’s one of them (_tabProdi.php):




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

		'id'=>'_tabProdi',

		'dataProvider' => $model->search(),    //error in here

		'filter' => $model,

		'summaryText' => 'Menampilkan {start} - {end} dari total {count} data',

		'pager' => array(

			'header' => 'Halaman ',

			'nextPageLabel' => 'Selanjutnya',

			'prevPageLabel' => 'Sebelumnya',

			'firstPageLabel' => 'Awal',

			'lastPageLabel' => 'Akhir',

			'maxButtonCount' => 3,

		),

		'columns' => array(

			array(

				'header' => 'NIK',

				'name' => 'NIK',

				'type' => 'raw',

				'value' => 'CHtml::encode($data->NIK)'

			),

			array(

				'header' => 'NAMA',

				'name' => 'NAMA',

				'type' => 'raw',

				'value' => 'CHtml::encode($data->NAMA)',

			),

			array(

				'header' => 'FAKULTAS',

				'name' => 'FAKUL_ID',

				'type' => 'raw',

				'value' => 'CHtml::encode($data->FAKUL_ID)',

			),

			array(

				'name' => 'EMAIL',

				'type' => 'raw',

				'value' => 'CHtml::encode($data->em->EMAIL)',

			),

		),

	));



here’s the model (User.php):




class User extends CActiveRecord

{

	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'KAR_MF';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('NIK, NAMA, FAKUL_ID, STATUS', 'required'),

		);

	}

	

	public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

        'em'=>array(self::HAS_ONE,'MailStaf','NIK'),

        'ua'=>array(self::HAS_ONE,'UserApp','NIK'),

        );

    }


	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.

		$notin='123456';

		$criteria=new CDbCriteria;

		$criteria->condition = "NIK NOT IN $notin";

		$criteria->compare('NIK',$this->NIK,true);

		$criteria->compare('NAMA',$this->NAMA,true);

		$criteria->compare('FAKUL_ID',$this->FAKUL_ID,true);

	

		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

			'sort'=>array(

				'defaultOrder'=>'NIK ASC',

			),

			'pagination'=>array(

				'pageSize'=>5

			),

		));

	}

}



in some threads, i have to create controller like this:




public function actionUser() {

		$model =new User('search');

		if(isset($_GET['User']))

			$model->attributes =$_GET['User'];

	

		$params =array(

			'model'=>$model,

		);

	

		if(!isset($_GET['ajax'])) $this->render('user', $params);

		else  $this->renderPartial('user', $params);

	}



but, it works if i call actionUser (user.php). How is about index.php (containing CGridView via renderPartial() inside of CJuiTabs)???

i’ve got error:


Fatal error: Call to a member function search() on a non-object in C:\xampp\....

Sorry for my poor English.

Thank you for your answer.

Look at this page:

http://www.yiiframework.com/doc/api/1.1/CJuiTabs


$this->widget('zii.widgets.jui.CJuiTabs', array(

    'tabs'=>array(

        'StaticTab 1'=>'Content for tab 1',

        'StaticTab 2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),

        // panel 3 contains the content rendered by a partial view

        'AjaxTab'=>array('ajax'=>$ajaxUrl),

    ),

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>true,

    ),

));

I would recommend using the AjaxTab method in this example. Create a separate controller action for each of your grid partial views. So you’ll have something like:




$this->widget('zii.widgets.jui.CJuiTabs', array(

    'tabs'=>array(

        'Program Studi (Prodi)'=>array('ajax'=>'/controller/prodi'),

        'Administrasi Akademik & Kemahasiswaan (AAK)'=>array('ajax'=>'/controller/aak'),

        'Administrasi Umum (AU)'=>array('ajax'=>'/controller/au'),

    ),

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>false,

        'event'=>'mouseover',

    ),

));



Currently, your’re trying to renderPartial within your view and you’re passing no variables into the partial, so there’s nothing to render.

Big Thanks for your reply BStep,

i have changed my index.php like this:




$prodi=$this->createUrl('admin/prodi'); //my controller file named AdminController.php

$aak=$this->createUrl('admin/aak');

$au=$this->createUrl('admin/au');


$this->widget('zii.widgets.jui.CJuiTabs', array(

    'tabs'=>array(

        'Program Studi (Prodi)'=>array('ajax'=>$prodi, 'id'=>'prodi'),

        'Administrasi Akademik & Kemahasiswaan (AAK)'=>array('ajax'=>$aak, 'id'=>'aak'),

        'Administrasi Umum (AU)'=>array('ajax'=>$au, 'id'=>'au'),

    ),

    // additional javascript options for the tabs plugin

    'options'=>array(

        'collapsible'=>false,

    	'event'=>'mouseover',

    ),

));



and here’s separate controller action for each of grid partial view (Prodi):




public function actionProdi() {

		$model =new User('search');

		if(isset($_GET['User']))

			$model->attributes =$_GET['User'];

	

		$params =array(

			'model'=>$model,

		);

	

		if(!isset($_GET['ajax'])) $this->render('pages/_tabProdi', $params);

		else $this->renderPartial('pages/_tabProdi', $params);

	}



but, the tab displays the contents with the layout, this is because of “$this->render(‘pages/_tabProdi’, $params);” rendering the layout.

i have changed




if(!isset($_GET['ajax'])) $this->render('pages/_tabProdi', $params);

else $this->renderPartial('pages/_tabProdi', $params);



to




$this->renderPartial('pages/_tabProdi', $params);



and the ajax don’t works…so, search and filter don’t work too…

any idea??

Thanks for helping Y!! newbie… ::)

You may try changing


$this->renderPartial('pages/_tabProdi', $params);

to


$this->renderPartial('pages/_tabProdi', $params, false, true);

and see if that helps.

Big Thanks for your reply macinville.

it works for everythings. search and filter work perfectly now.

Thanks for helping Y!! Newbie.

Glad to be of help, eazyval.

I have the same problem. Search & filter not work if 2 CGridViews on one page. See my post.

I followed this method, and tab-2’s filter works. Unfortunately, when I switched back to tab-1, the filter of tab-1 didn’t work.

Please help~

Thank you so much

I’ve solved my problem too

Terima kasih sdh berbagi code

Problem saya yg sama jg tuntas