Confuse On : CJSON ,with renderPartial

Hi…

Before I ask my question , let me describe my situation and my code first, thanks for your patient.

This is a snippet from my controller which is requested use AJAX request.

EXAMPLE ONE





echo CJSON::encode(array(

                            'status'=>'success',

                            'div'=>'Tambah Universitas Berhasil',

                    'ddContent'=>$this->renderPartial('universitiesData',array('data'=>$data),true),

                            ));







This is my universitiesData view, which is intended to fill a drop down that i want to.


echo CHtml::tag('option',array('value'=>-1), CHtml::encode('- - Pilih Universitas - -'),true);

    foreach($data as $value=>$name)

    {

        echo CHtml::tag('option',array('value'=>$value), CHtml::encode($name), true);

    }




EXAMPLE TWO





echo CJSON::encode(array(

                            'status'=>'success',

                            'div'=>'Tambah Universitas Berhasil',

                    'ddContent'=>$this->generateUniversitiesData(),  // <---  This is the point of difference !!!

                            ));







This is generateUniversitiesData() method 


private function generateUniversitiesData()

        {

            $univs = University::model()->findAll(array(

                'condition'=>'status=1',

            ));

            $data = CHtml::listData($univs,'id','name');

            echo CHtml::tag('option',array('value'=>-1), CHtml::encode('- - Pilih Universitas - -'),true);

            foreach($data as $value=>$name)

            {

                echo CHtml::tag('option',array('value'=>$value), CHtml::encode($name), true);

            }

        }



I have done successfully use example one approach. But I want to ask Why the second example can’t worked properly.

When I observed from firebug i got that drop down content is on the outside of the JSON object ( so the ‘ddContent’ had a null value, but if i follow the first approach the result is in the appropriate place ( inside JSON object )

Is there anyone could explained about this ?

Thanks a lot before

God Bless You

On the second approach… you are assigning the returned value of generateUniversitiesData() to ddcontent right?

But the problem is that this method does not return anything… as you are echoing the values… ;)

As said mdomba, you should collect all this output in a string and return.

For example like that:




private function generateUniversitiesData()

        {

            $univs = University::model()->findAll(array(

                'condition'=>'status=1',

            ));

            $data = CHtml::listData($univs,'id','name');

            $return= CHtml::tag('option',array('value'=>-1), CHtml::encode('- - Pilih Universitas - -'),true);

            foreach($data as $value=>$name)

            {

                $return.= CHtml::tag('option',array('value'=>$value), CHtml::encode($name), true);

            }

            return $return;

        }




But what for to do it when there is already renderPartial that does this job?

CJson::encode will take the array with the whole string and encode in a json array.

Btw: did you follow my wiki about ajax creation? Great!!

Hi Zac and mdomba…

Thanks a lot for your responds…

I understand now, :)

@Zac => yeah Zac, I follow your wiki =) it’s a great example , tutorial, and technique i was looking for

            Thanks Zac

Thanks a lot all

God Bless You