Yii Framework Forum: update CGridView dataProvider as ajax - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

update CGridView dataProvider as ajax Rate Topic: -----

#1 User is offline   raminious 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 27-June 10
  • Location:tehran

Posted 04 July 2010 - 09:19 AM

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 :( ?
coding is my life, i love my life
0

#2 User is offline   maddes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-July 10

Posted 06 July 2010 - 03:42 AM

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' => 'POST',
'url' => CController::createUrl('Invoice/addtoinvoice') ,
'update' => '#gridInvoice' ,
'data' => $(#invoiceRow).serialize();
'success' => "function(data, textStatus, XMLHttpRequest){try{\$('#gridInvoice').yiiGridView.update('gridInvoice')}catch(e){}}"
)
),
));

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 ... }
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users