Suppose I have this in my controller:
public function actionEditEvent() /* ajax function */
{
$event = Event::model()->findByPk($_POST['id']);
$return_array = array(
'data'=>array(
'date'=>$event->date,
'time'=>$event->time,
'comments'=>$event->comments,
'id'=>$event->id,
),
);
echo json_encode($return_array);
}
And in my view I have this:
$('#list').on('click', '.edit-button', function(){
var dataId = $(this).attr('data-id');
$.ajax({
url: 'booking/editevent',
type: 'POST',
dataType: 'JSON',
data: {id: dataId},
success: function(result){
$('#add-event #Event_date').val(result.data.date);
$('#add-event #Event_time').val(result.data.time);
$('#add-event #Event_comments').val(result.data.comments);
$('#add-event #Event_id').val(result.data.id);
}
});
});
As you can see in my ajax ‘success’ handler I am having to manually assign the values from the JSON object. Is it possible for this to be done in one single assignment?
#add-event is a CActiveForm