AR update via AJAX & radioButtons and update view

Hello,

i have a gallery with images inside (2 tables). I want one of the images to be Main Gallery image, so i list all Gallery images in gallery/update view. I do this with help of radioButton():




<?php foreach($model->files as $n=>$image):?>

<div class="image-for-main">

<?php echo CHtml::radioButton('image',$model->image_main == $image['filename'] ? true : false, array(

    'value'=>$image['filename'],

    'ajax' => array(

    'type'=>'POST',

    'url'=>$this->createUrl('estyma_gallery/mainimage',array('gallery'=>$model->id_gallery)),

//    'update'=>'#images',

    'beforeSend' => 'function(){

                      $("#spinner").addClass("loadingon");

                  }',

    'complete' => 'function(){

                      $("#spinner").removeClass("loadingoff");

                  }',

    ))), CHtml::image(bu() . '/upload/gallery/thmb_' . $image['filename']) ?>

</div>

<?php echo ($n+1)%7 == 0 ? '<div class="clear" ></div>' : null; ?>

<?php endforeach;?>



This gives me a list of radio buttons with images by each. Then when i click on any radio button, it launches a controller action:




public function actionMainimage()

{

    $toChange = $_POST['image'];

    $gallery = $_GET['gallery'];


    $gal = a_gallery::model()->findByPk($gallery);

    $gal->obraz = $toChange;

    if($gal->save())

        echo 'saved';

}



All is ok, the filename of image ive chosen, writes to DB in proper way, but i dont know what to do to refresh the view. I tried to do redirect(), but it does nothing:


$this->redirect(array('a_gallery/update','id'=>$gallery),true);

(firebug says OK, but nothing changes, page doesn`t refresh)

I also tried $this->refresh(); but also no effect.

Anyone can help me with this?

You probably would want to call renderPartial(‘a_partial_view’) from the controller to replace the innerHtml of ‘image-for-main’.




  ajax=>array(

  ...

  'success'=>'function(html){jQuery("#item_id").html(html);'

  ...



Then it should work the first time you click the button (event handler attached on document.ready). When you click the next time there’s no event handler attached since you replaced the radioButton. One solution might be to use jQuery.live (in a separate script) for reattaching events to selected members of css class ‘image-for-main’ (you probably don’t want to attach handlers to every node).

I haven’t tested this all the way, but from previous feedback in the forum, I assume it works.

/Tommy

thanks, a lot, i`ll try that. In other hand i was looking for something like ActivePanel in PRADO. There i could render that element once again. As You can see i dynamicaly check if the image from gallery is the main one:


$model->image_main == $image['filename'] ? true : false

so if data would refresh, the checkbox would change its place also. But i`ll try with render partial :)