Duplicate grid rendering after filter search

Hi,

I get duplicate grid appended to the first grid after every search.Please help.

My yii version is 1.1.8

to get any help about this you need to post your relevant code… the controller action… the view

view file genre/admin.php




<?php

$this->breadcrumbs=array(

	'Genres'=>array('index'),

	'Manage',

);


$this->menu=array(

		array('label'=>'Create Genre', 'url'=>array('create')),

);


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('genre-grid', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>Manage Genres</h1>


<p>

You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

</p>


<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

)); ?>

</div><!-- search-form -->

 <div class="right">

          <?php echo CHtml::link(Yii::t('adminmembers', 'Add Genre'), array('genre/create'), array( 'class' => 'add_new' )); ?>

         

          </div>

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

	'id'=>'genre-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		

		'genre_name',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



GenreController.php


<?php


class GenreController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Genre;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Genre']))

		{

			$model->attributes=$_POST['Genre'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->genre_id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Genre']))

		{

			$model->attributes=$_POST['Genre'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->genre_id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			$this->loadModel($id)->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$model=new Genre('search');

		$model->unsetAttributes();  // clear any default values

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

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


		$this->render('admin',array(

			'model'=>$model,

		));

	}

 

	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Genre('search');

		$model->unsetAttributes();  // clear any default values

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

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


		$this->render('admin',array(

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Genre::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='genre-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}



I played around with the code and found that after replacing


 $(id).replaceWith($(id,'<div>'+data+'</div>'));

with


newid=$(id,'<div>'+data+'</div>');

$(id).replaceWith('<div id="'+v+'" class="grid-view">'+newid.html()+'</div>');

in the assets/yii.gridview.js I solved it. but I dont want to change the code in yiigridview’s js file.Can someone help me to do without this fix?

Note that I wrote “relevant code” and explicitely "controller action… you put here the whole controller - it’s very hard to help you when others need to search relevant code in your “big” post…

By first look this seems default code generated by Gii… did you do any modification to it ?

Try with the default code generated with Gii without adding anything first… so to see if it works there…

Sorry about the code.My first time actually.

Yes, it is the default code generated by Gii through CRUD .I tried generating the default code with gii 2 times already. I still get the same result.I also commented other javascripts , to make sure its not conflicting other non-yii js files I have added.

Ops… I forgot to welcome you to the forum :D

Very strange… until now I did not hear about this problem by the default code…

Did you add anything to the project?

Try to create a new webapp from start and then the model and CRUD for genre…

Yes the front end of the site is completed , I am starting on the control panel/admin panel.Its just having CRUD operations for db tables so I am integrating it in a existing yii website.Do you think the front end files would create the problem with replaceWith javascript? I have separate layout file main.php for admin section.I am just posting the head section of that file here as I have not included any js files anywhere else in the code.


<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title><?php echo $this->pageTitle ; ?></title>

<?php Yii::app()->clientScript->registerCssFile( Yii::app()->themeManager->baseUrl . '/css/style.css', 'screen' ); ?>


	<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>

		

		<script type="text/javascript">

		var themeUrl = '<?php echo Yii::app()->themeManager->baseUrl; ?>';

		var _languages = {

			'deletePrompt': '<?php echo Yii::t('adminglobal', 'Are you sure you want to delete this item?\nThis action cannot be undone!'); ?>',

			'deleteAborted': '<?php echo Yii::t('adminglobal', 'OK! Action Cancled.'); ?>'

		};

		</script>

                

                 <?php Yii::app()->clientScript->registerScriptFile( Yii::app()->themeManager->baseUrl . '/js/simpla.jquery.configuration.js', CClientScript::POS_END ); ?>

		<?php Yii::app()->clientScript->registerScriptFile( Yii::app()->themeManager->baseUrl . '/js/facebox.js', CClientScript::POS_END ); ?>

		<?php Yii::app()->clientScript->registerScriptFile( Yii::app()->themeManager->baseUrl . '/js/easyTooltip.js', CClientScript::POS_END ); ?>


</head>

Can’t help you without debuging your code… my best suggestion is to try what I wrote above… just create a new webapp and crud to check that the default code works for you… this way we will get the confirmation if the problem is in Yii or in some additional code…

I solved it.I had installed nlsclientscript extension.

protected/config/main.php




clientScript' => array(

'class' => 'application.extensions.nlsclientscript.NLSClientScript',

    'ignoredPattern' => '/\.tpl/i', //regex, files with matching paths won't be filtered

    //'processedPattern' => '/\.php/' //regex, only files with matching paths will be filtered

  ),

I commented it so now its working fine.I dont know why this created the problem.

#7098 is a known issue regarding the nlsclientscript extension.