[SOLVED]AjaxLink/renderPartial only loading action first time

I have an Ajax Issue…

Here’s the index…


//.......

<div class="container">

<div class="row">

<div class="span12" id="wsView" style="margin: 0 auto; width:100%; text-align: center;">

<?php

            if($dayOffset == 0){

                $this->renderPartial('_interval', array(

                    'prev' => $prev,

                    'next' => $next,

                    'dayOffset' => $dayOffset

                ),false,true);

            }

            ?>

</div>

/.......

Heres the action…


    function actionPrevWeek()

    {

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

            echo "loaded action PrevWeek-".rand();

            //To prevent loading files each request

            Yii::app()->clientScript->scriptMap = array("jquery.js" => false,

                                                        'bootstrap.min.js'=> false);

            $this->count--;

            // echo "<br>$this->count<br>";

            $currDay = myDateUtil::getDateInterval($this->count);

            var_dump($currDay);

            $dates = myDateUtil::getWeeklyInterval(TRUE, $currDay);

            $prev = $dates[0]->format("Y/m/d");

            $next = $dates[1]->format("Y/m/d");

            $this->renderPartial('_interval',

                array(

                    'prev' => $prev,

                    'next' => $next,

                    'dayOffset' => $this->count

                ), false, true);

        }

    }

and finally heres the view…


print_r($this->currDay);

echo "<br>";

echo "Offset=" . $dayOffset . "<br>";

$today = new DateTime();

$currDay = myDateUtil::getDateInterval($dayOffset);


var_dump($currDay);


echo "<br>" . $dayOffset . "<br>";


//HERE LIES THE AJAX CODE

echo CHtml::ajaxLink('Prev', array('PrevWeek'), array('replace' => '#wsView'),array('id' => 'send-link-' . uniqid()));

//echo CHtml::ajaxLink('Prev', $pUrl, array('update' => '#wsView'))

?>

<?php echo " " . $prev; ?> - <?php echo " " . $next . " ";

echo CHtml::ajaxLink('Next', array('NextWeek'), array('replace' => '#wsView'),array('id' => 'send-link-' . uniqid()));

the problem is that the page only refreshes the div the first time anytime after that, I can see the requests in firebug, but the div is not refreshed, and in fact the action isn’t even called

Just a guess as most of the code seems ok: Try “update” instead of “replace” when using ajax because on initial load you have a div with the id “wsView” and after that you replace the whole div with the rendered partial which doesn’t include any div with an id, thus all following requests won’t be able to replace anything.

Hope it helps

So I solved the problem and thought it would be helpful and perhaps promote discussion.

there were actually two issues:

  1. the $count variable wasn’t being properly tracked

  2. the AjaxLink implementation of jquery.live was resulting in multiple requests per click as shown by firebug

#2 was solved the easiest




<button title="Next">

    <?php

    echo CHtml::ajaxLink('Next', CController::createUrl('WeeklySchedule/NextWeek'), array('update' => '#wsView'), array('live' => false,));

    ?>

</button>



just set the ‘live’ variable to false, now we get 1 request per click EXCEPT on the first one.

see Here

#1 Was solved by using session variables which will remain intact across page loads




    function actionPrevWeek()

    {

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


            Yii::app()->clientScript->scriptMap = array("jquery.js"       => false,

                                                        'bootstrap.min.js'=> false);

            $this->myCount                      = Yii::app()->session['weekCount'];

            $this->myCount--;

            Yii::app()->session['weekCount'] = $this->myCount;

            $currDay                         = myDateUtil::getDateInterval($this->myCount);

            $dates                           = myDateUtil::getWeeklyInterval(TRUE, $currDay);

            $prev                            = $dates[0]->format("Y/m/d");

            $next                            = $dates[1]->format("Y/m/d");

            $shifts                          = myDateUtil::getShiftsBetween($prev, $next);


            $this->renderPartial('_interval',

                array(

                    'prev'      => $prev,

                    'next'      => $next,

                    'dayOffset' => $this->myCount,

                    'shifts'    => $shifts,


                ), false, true);

        }

        else throw new CException("Non Ajax Request");

    }



This will only work if you start a new session in actionIndex




        ......

        $session = new CHttpSession;

        $session->open();

        Yii::app()->session['weekCount'] = 0;

        ......



Hope this helps someone, seems that theres lots of posts with Ajax Questions…