update CGridView dataProvider as ajax

hi

i wanna to update CGridView dataProvider as ajax

in view file it’s my code:




<?php echo CHtml::Button('add to invoice',array(

'onClick' =>

CHtml::ajax(

	array(

	    'type'    => 'POST',

	    'url'     => CController::createUrl('Invoice/addtoinvoice') ,

	    'update'  => '#gridInvoice' ,

	    'success' => "function(data, textStatus, XMLHttpRequest){try{\$('#gridInvoice').yiiGridView.update('gridInvoice')}catch(e){}}"

		 

		

	)

    ),

));



and




if (!isset($dataProvider))

    $dataProvider= new CActiveDataProvider("Invoice") ;


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


	    'dataProvider'=>$dataProvider,

	    'template' => '{items}' ,

	    'id'	  => 'gridInvoice',

	    'ajaxUpdate' => 'true' ,

	    'columns' => array(

		    'id',

		    'visitorId',

		    'shopId',

		    'price',


		    array(

			    'class' => 'CButtonColumn',

			    'template' => '{delete}' ,


		    ),


	    )

	    )


    );



in controller:




public function actionAddToInvoice() {

	if (Yii::app()->request->isAjaxRequest) {




	    $model = new Invoice ;


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


		$model->visitorId        =  $_POST['Invoice']['visitorId'];

		$model->invoiceId	 =  $_POST['Invoice']['invoiceId'];

		$model->shopId           =  $_POST['Invoice']['shopId'];

		$model->productType      =  $_POST['Invoice']['productType'];

		$model->deliveryCount    =  $_POST['Invoice']['deliveryCount'];

		$model->referCount       =  $_POST['Invoice']['referCount'];

		$model->price            =  $this->getPrice();


		if (!$model->validate()) {

		}


		$model->price  =  $this->getPrice();

		model->save();

	    }


	    $dataProvider = new CActiveDataProvider("Invoice",

		    array(

			'criteria' => array(

			    'condition' => 'invoiceId = :invoiceid' ,

			    'params'    =>  array(':invoiceid' => $_POST['Invoice']['invoiceId'] ),

			    )

		     )

		    ) ;


		   

	    $this->renderPartial('_create' , array('model' => $model, 'dataProvider'=>$dataProvider)) ;

	}

    }



but dataProvider not influences the gridView … why :( ?

I may be wrong but you’re missing the “data” propperty in the ajax call.

In your view:

<?php echo CHtml::Button(‘add to invoice’,array(

‘onclick’ =>

CHtml::ajax(

    array(


        'type'    =&gt; 'POST',


        'url'     =&gt; CController::createUrl('Invoice/addtoinvoice') ,


        'update'  =&gt; '#gridInvoice' ,

[color="#FF0000"] ‘data’ => $(#invoiceRow).serialize();[/color]

        'success' =&gt; &quot;function(data, textStatus, XMLHttpRequest){try{&#092;&#036;('#gridInvoice').yiiGridView.update('gridInvoice')}catch(e){}}&quot;


    )


),

));

otherwise, how will your controller know the data of the new invoice to add?

Good Luck

p/s: probably the serialize()function is used badly, bat the point is: Send some data on the ajax call.

you may even put:

‘data’ => { InvoiceId: “$(InvoideIdField).val()”; ShopId : “$(ShopIdField).val()” , and so on … }