Facing Issues With Ajax Update On Page Load

Hi, I’m having troubles getting content displayed on page load using ajax. The ajax is calling the right action in the respective controller. The first part of the action code where i update the database is working fine. But the part where i’m calling renderPartial is not working.

Ok here is the controller action ::

[size="6"]EDIT[/size]





public function actionUpdateProductData() {

    Yii::import('application.components.DataScraper.*');

    require_once('GetProductData.php');

    

    $productRealTime = RealTime::model()->findAll();

    

    if (count($productRealTime) === 0) {


        $symbolData = new GetProductData();

        $symbolData->getAmazonProductData();

    } else {

        


        echo CJSON::encode( array(

            'status' => 'OK',

            'div' => $this->renderPartial('_productDataGrid', array(

                            'model' => $productRealTime), 

                            true, true ),

        ));


    }

}




The if part is working fine. But the else portion is not working.

Here is the view index.php::





<?php

       <?php

        /*

         * Include the ajax Stock update script

         * 

         */

        $baseUrl = Yii::app()->baseUrl;

        $cs = Yii::app()->getClientScript();

        $cs->registerScriptFile($baseUrl . '/js/ajaxProductDataUpdate.js');

?>


<div>

    <hr>

    <ul class="breadcrumb">

        <li>

            <a href="#">Home</a> <span class="divider">/</span>

        </li>

        <li>

            <a href="#">Product Info</a>

        </li>

    </ul>

    <hr>

</div>





<div class="span-10">


    <div id="section2">


    </div>


</div>










Here is the partial view file _stockDataGrid.php





<?php


$this->widget('bootstrap.widgets.TbGridView', array(

    'id' => 'real-time-grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'id',

        'name',

        'category',

        'price'

        'rating'


        array(

            'class' => 'bootstrap.widgets.TbButtonColumn',

        ),

    ),

));

?>






And here is the jQuery file which is making the ajax request





var productParameters = {

    ajaxUpdate: function() {

        $.ajax({

            url: "/ProductAnalysis/index.php/realTime/updateProductData",

            type: "GET",

            dataType:"json",

            error: function(xhr, tStatus, e) {

                if (!xhr) {

                    alert(" We have an error ");

                    alert(tStatus + "   " + e.message);

                } else {

                    alert("else: " + e.message); // the great unknown

                }

            },

            success: function(data) {

                $.fn.yiiGridView.update('real-time-grid', {

                    data: $(this).serialize()

                });

            }

        });

    }

};


$(document).ready(function() {

    productParameters.ajaxUpdate();


});



Upon loading the page /realTime/index.php i’m getting an error which says





else:

undefined




Obviously the ajax call is failing, but i don’t know how will i fix it. Also in Firebug, the echo date() function in the controller is working, but the Gridview is not working.

Please provide some help on how to solve this. Let me know where i’m doing wrong. I can’t seem to make any headway around this.

Thanks in advance,

Maxx

don’t know what you want to do ?

see lazy load CGridView

and your code seems no efficient :




  if (count(RealTime::model()->findAll()) === 0) {

            

            $symbolData = new GetStockData();

            $symbolData->getYahooStockData();

        } else {

            $stockRealTime = RealTime::model()->findAll();

        ...




RealTime::model()->findAll() may execute two times ! when the result counts is 0 !

try it this way :




   $realTimes = RealTime::model()->findAll(); // this may return a empty array!

   if(empty($realTimes)){

         ...

   }else{

          ..

   }




Hi, thanks for your response. Thats the thing you know, i know what i want to do, but not sure on how to do it exactly in Yii.

Oh, i completely missed that, findAll() problem. Thanks for the tip. Will fix it.

Yes i want something similar to the lazy load gridview. What i want is that upon page load the ajax script will run which will fetch the records from the database and render it in a gridview. So upon first page load only the first 50 records will be displayed in the grid, then upon clicking the > or the next page button in the pagination menu the next 50 records will be loaded through ajax.

Can you please tell me what should i do to achieve this? Any useful tips and tutorial will be helpful.

I mean i can do this easily using plain php, jquery… But how can i do this using Yii.?

look at this one

inifinite-scroll-pager

another way is extending the CLinkPager class (only retain the "next" pager functionality !)

Thanks that was really helpful.

Well it seems that the gridview widget won’t work with findall(). So i changed the dataprovider to simple model and it works now.

Here is the working code ::





public function actionUpdateStockData() {

    Yii::import('application.components.DataScraper.*');

    require_once('GetStockData.php');

    $productRealTime = new RealTime();




    if (count($productRealTime->model()->findAll()) === 0) {


        $symbolData = new GetproductData();

        $symbolData->getAmazonProductData();

    } else {




        echo CJSON::encode( array(

            'status' => 'success',

            'div' => $this->renderPartial('_productDataGrid', array(

                            'model' => $productRealTime), 

                            true, true ),

        ));


    }

}