Update Gridview After Request Call

Hello,

I start work with yii framework a few days.

My problem is with ajax request. In my view I have a textfield that call controller via ajax when user key press… in controller I make a new search in database and i render the page. How I can refresh grid with new data?

My solution:

[color="#FF8C00"]view[/color]


<?php echo $form->textField($model,'search', array('id'=>'search','maxlength'=>128, 'placeholder'=>'filter by name',

								'onkeyup' => CHtml::ajax(array(

									'url' => CController::createUrl('/test/ajax'),

									'type' => 'POST',

									'success'=>"function(string){console.log('done'); [b]$.fn.yiiGridView.update('gridTest', {data: $(this).serialize()}) -> I think this is not correct or is not necessary is my case <-[/b];}")))); ?>

	...

	

	$this->widget('bootstrap.widgets.TbGridView', array(

				'id'=>'gridTest',

				'ajaxUpdate'=>true,

				'type'=>'striped bordered condensed',		

				'dataProvider'=>$dataProvider, 

				'template'=>"{items}\n{pager}",

				'columns'=>array(

						array('name'=>'testId', 'header'=>$model->getAttributeLabel('testId')),

						array('name'=>'name', 'header'=>$model->getAttributeLabel('name'))

				)

		));

[color="#FF8C00"]controller[/color]


public function actionIndex()

	{

		$model = new Test();	

		$data = $model->getTestCaseList();

		$this->render('index', array('model'=>$model, 'data'=>$data));

	}

	

	public function actionAjax()

	{

		if (isset($_POST['Test'])){

			$model = new Test();

			$model->setAttributes($_POST['Test']);

			

			$criteria = $model->search;

			

			$data = $model->getTestList($criteria);

			

			try with the two render's... the data is correct from the database

			//$this->renderPartial('index', array('model'=>$model, 'data'=>$data, false, true));

			echo $this->render('index', array('model'=>$model, 'data'=>$data));

		}

	}

I think you made some errors in your ajax request, try this




<?php echo $form->textField($model,'search', array('id' => 'search', 'maxlength' => 128, 'placeholder' => 'filter by name',

    'onkeyup' => CHtml::ajax(array(

        'url' => CController::createUrl('/test/ajax'),

        'type' => 'POST',

        'data' => array('Test' =>'js:$("input#search").val()'),

        'success' => "js:$.fn.yiiGridView.update('gridTest-grid')"))));

?>



EDIT: I forgot to change id to Test, should be correct now

Tks EdoFre, but will still not working…

I don’t need ‘data’ => array(‘Test’ =>‘js:$(“input#search”).val()’), because I have a form and the value of textfield appears fine in controller side. In debug I checked that everything is going as planned, this is, get the new values with the filter. But the grid is not updated :confused:

Hi PS86K,

what is exactly your issue?





Please, use the code snippet tags to place your code for others to better help you, because otherwise is very hard to read it. 





Instead of writing your event handler code in the element's onkeyup attribute, I would rather use jQuery event methods, 



f.e. $('#search').keyup(function() {...});



this assures you broader compatibility.

Also note that if the view is being rendered by the controller, you call 

$this->createUrl(...)

instead of CController::createUrl(), as there is no such class method AFAIK.

When you give the column names, Grid widgets use the attribute label as the default header, so you do not need to write it explicitly.





Is the controller action being called via AJAX? Is it the same action that first renders the grid? Does it look fine when it is first rendered?





I see you are using an extension (TbGridView), if every other thing goes wrong I would check if Yii's CGridView works fine to diagnose whether it is a TbGridView issue what you are having...

Hi clapas,

[color="#FF8C00"]Is the controller action being called via AJAX? Is it the same action that first renders the grid? Does it look fine when it is first rendered?

[/color]

Is the same action and the first time it’s fine, the grid appear with data.

[color="#FF8C00"]I see you are using an extension (TbGridView), if every other thing goes wrong I would check if Yii’s CGridView works fine to diagnose whether it is a TbGridView issue what you are having…[/color]

the behavior is the same…

The problem is not in event keyup or on CController::createUrl(), because the call to controller is made when I press a key.


public function actionAjax()

	{

		if (isset($_POST['Test'])){

			$model = new Test();

			$model->setAttributes($_POST['Test']);

			

			$criteria = $model->searchName;

			

			$data = $model->getTestCaseList($criteria);


[color="#008000"]//this point returns the expected data according to the name in the search

the problem is in render... i have another way to pass data?

[/color]




			//$this->renderPartial('index', array('model'=>$model, 'data'=>$data, false, true));

			$this->render('index', array('model'=>$model, 'data'=>$data));

		}

	}

Tks

Now I will work in other solution…

I send the value by json:


public function actionAjax()

	{

		if (isset($_POST['Test'])){

			$model = new Test();

			$model->setAttributes($_POST['Test']);

			

			$criteria = $model->searchName;

			

			$data = $model->getTestCaseList($criteria);

			//$this->renderPartial('index', array('model'=>$model, 'data'=>$data, false, true));

			//$this->render('index', array('model'=>$model, 'data'=>$data));


			header('Content-Type: application/json; charset="UTF-8"');

			echo json_encode($data);

		}

	}

In view, I get the values:


'success'=>"function(data){

												console.log(data); //I get the objects

$.fn.yiiGridView.update('gridTest', {data: $(this).serialize()}); //do nothing... :/

}")))); ?>

Now I need a way to update the grid with the values by javascript

If I use the grid filters the grid update well…

I don’t have solution for my issue yet :confused: