Cgridview And Ajax

Good Day YII!

I have a problem in using both cgriview and ajax. I wanted to update my Cgridview using ajax after submitting an input in the textbox. Currently, I have this code.

VIEW

<div class="row">

<?php echo 'Identifier Code : ?>

<?php echo CHtml::textField(‘account’,’’,array(‘size’=>60,‘maxlength’=>155,‘placeholder’=>‘Enter Account Here…’,‘style’=>‘width:20em;’)); ?>

</div>

<div class="row buttons">

<?php echo CHtml::button(‘Search…’, array(‘class’=>‘btn btn-primary’,‘style’=>‘margin-top:-8px;’,‘onclick’ => ‘searchCode()’)); ?>

</div>

<div class="account_details"></div>

<?php

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

‘id’ => ‘grid_id’,

‘dataProvider’ => $data2,

‘columns’ => array(

'id',


'payment',


'datesale',


'code',

),

));

?>

<script>

function searchCode()

{


	var account = &#036;(&quot;#account&quot;).val();


	&lt;?php


	echo CHtml::ajax(array(


			'url'=&gt;array('test'),


			'type'=&gt;'post',


			'data'=&gt;'js:{account:&#036;(&quot;#account&quot;).val()}',


			'success'=&gt;'function(data){		


				&#036;.fn.yiiGridView.update(&quot;grid_id&quot;);


				return false;					


			}',																		


		))			


	?&gt;		


}

</script>

CONTROLLER

public function actionTest() {

$model=new DATATicket2Media;

$rawData=Yii::app()->msdb->createCommand("

			 [left] SELECT 


				  DATA_Transaction.TransactionId AS id,


				  DATA_Payment.PaymentAmount AS payment,


				  DATA_Sale.SaleDate AS datesale,


				  DATA_MediaIdentifier.IdentifierCode AS code


			  FROM 


				  DATA_MediaIdentifier LEFT JOIN


				  DATA_Media ON DATA_MediaIdentifier.MediaId = DATA_Media.MediaId LEFT JOIN


				  DATA_Ticket2Media ON DATA_Media.MediaId = DATA_Ticket2Media.MediaId LEFT JOIN 


				  DATA_Ticket ON DATA_Ticket2Media.TicketId = DATA_Ticket.TicketId LEFT JOIN 


				  DATA_Account ON DATA_Ticket.AccountId = DATA_Account.AccountId LEFT JOIN


				  DATA_Reservation ON DATA_Reservation.AccountId = DATA_Account.AccountId LEFT JOIN						 


				  DATA_Sale ON DATA_Reservation.SaleId = DATA_Sale.SaleId LEFT JOIN


				  DATA_Transaction ON DATA_Sale.SaleId = DATA_Transaction.SaleId LEFT JOIN


				  DATA_Payment ON DATA_Transaction.TransactionId = DATA_Payment.TransactionId 


			  WHERE 


			  	  DATA_MediaIdentifier.IdentifierCode = 'EBE0K0XQO0008Q'&quot;)-&gt;queryAll();[/left]


	[left]&#036;data2=new CArrayDataProvider(&#036;rawData, array(


		'id'=&gt;'user',


		'sort'=&gt;array(


    		'attributes'=&gt;array(


         		'id', 'username', 'email',


    			),


			),


			'pagination'=&gt;array(


    			'pageSize'=&gt;10,


			),


	));[/left]


  &#036;this-&gt;render('create', array(


  'model' =&gt; &#036;model,


  'data2'=&gt;&#036;data2,


    'grid_id' =&gt; 'grid_id',


));

I just want to have some idea on how to update a cgridview using ajax and depending on the user it will bind the value to a query in the controller. Hope someone could help!

Thanks!

This is the way to updated cgridview after ajax




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


	'afterAjaxUpdate'=>'function(id, data){

		console.log($(".selected").text());

}',



Javascript to call update is





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

		data:$(this).serialize()

	});



Thanks for the reply!

But do I need to add render code in my controller after ajax Request to update the content of my CGridView?

Do you have any sample that I can refer to?

Thanks Chandran!

Hi try thishttp://www.yiiframework.com/forum/index.php/topic/31221-how-to-update-cgridview-with-ajax/