Yii Framework Forum: Add Item With Ajax (Renderpartial) - Yii Framework Forum

Jump to content

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

Add Item With Ajax (Renderpartial) Rate Topic: -----

#1 User is offline   Franker 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 02-January 13

Posted 17 October 2013 - 06:03 PM

Hey

I want to add a new Item with AJAX with renderPartial but it doesn't work. Thx for help

My CODE (short form):

Controller (AJAX)
public function actionCreatedestination($id)
	{       
       if(Yii::app()->request->isAjaxRequest){
          $destination = new Destination;  
          $destination->attributes = $_POST['Destination'];
          $trip->addDestination($destination);  
          }
          
          $this->renderPartial('_view', array(
	            'model'=>$trip,
                    'destination'=> $destination,
                    false,
                true
            ));
       } 


view:

echo CHtml::ajaxSubmitButton('Add', array('trip/createdestination', 'id' => $id), array(
        'type' => 'post', 
        ),
        array('id' => 'addDestination')); 

$this->renderPartial('_view', array(
        'model'=>$model,
        'destination'=> $destination,
    ));


_view:

<?php foreach($model->destinations as $i => $destination) { ?>
<?php echo $destination->id; ?>
<?php } ?>

0

#2 User is offline   martijnjonkers 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-June 13

Posted 18 October 2013 - 03:50 AM

You do a ajax request, but you do nothing with the result.
You need to add the html yourself:

CHtml::ajaxSubmitButton(
        'Add', 
        array('trip/createdestination', 'id' => $id), 
        array(
          'type' => 'post', 
          'success' => 'js: function(result) {
            if(result != "") {
              $("#partial-data").append( result ); //add to partial-data
            }
          }'
        ),
        array(
          'id' => 'addDestination'
        )
); 

<div id="partial-data">
$this->renderPartial('_view', array(
        'model'=>$model,
        'destination'=> $destination,
    ));
</div>


This is not tested, but it should work something like this.
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