Is It Possible To Render One Page Along With Json Data In Yii ?

Hi,

Is it possible to render one page along with json data in Yii?

or

Is it possible to render one page as json data, which will be parsed later?

Thanks,

Maclein

you simply can echo your json data in view, and it’s there when loading, like any other element in page

[color="#006400"]/* moved from Extensions forum */[/color]

But is it possible, when you are rendering the page? if so then how to render those data passed by json in view? how can I grab that? I have used that kind of json data rendering in renderPartial by using some callback function on "success". But here how this can be possible as the page is loading for the first time?

There are several extensions available which helps you implement that with Yii - take a look. :)

Dear Friend

If I have aptly understood, this is what you expect.

1.CDetailView




$jo='{"id":1,"name":"jack","age":10,"sex":"male"}';//JSON object.


$po=json_decode($jo);//tranforming into a php object.


$this->widget('zii.widgets.CDetailView', array(

	'data'=>$po,

	'attributes'=>array(

		'id',

		'name',

		'sex'

	),

));



2.CListView




$jo='[{"id":1,"name":"jack","age":10,"sex":"male"},{"id":2,"name":"jill","age":8,"sex":"female"},{"id":3,"name":"jhon","age":5,"sex":"male"}]'; 


$po=json_decode($jo); //brings array of objects.


$dataProvider=new CArrayDataProvider($po);


$this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); 



_view.php




<div class="view">


	<b><?php echo CHtml::encode('id'); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode('name'); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


    <b><?php echo CHtml::encode('age'); ?>:</b>

	<?php echo CHtml::encode($data->age); ?>

	<br />

	

	<b><?php echo CHtml::encode('sex'); ?>:</b>

	<?php echo CHtml::encode($data->sex); ?>

	<br />

</div>



3.CGridView




$jo='[{"id":1,"name":"jack","age":10,"sex":"male"},{"id":2,"name":"jill","age":8,"sex":"female"},{"id":3,"name":"jhon","age":5,"sex":"male"}]';


$po=json_decode($jo);

	

$dataProvider=new CArrayDataProvider($po);

	

$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'author-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'id',

		'name',

		'age',

		'sex',

		

	),

)); 



I hope I helped a bit.

Thanks for the reply but I was not looking for it. Right now I am passing arguments with render like this in controller




$this->render("view",array("model"=>$model));



Now I want to pass $model as json object to view when I am rendering this page (Note: I want to use render not render partial)

Is it possible?

It may sound obvious, but have you tried passing CJSON::encode($model) to the view? Now that you have the JSON $model in the view, you can play with it as you wish. To answer your specific question, Yii is an acronym from Yes It Is, so yes - it is possible.

Edit: If you would like to work with the JSON encoded $model on the view for in-built Yii functions like forms etc., you’ll need to decode the $model first and then use it as you normally do. But I don’t know why you would want to do that.

Dear Friend

The problem here is that you are in the situation of passing the json object to view and make use of that in view.

We can do it in the following way.

CONTROLLER.




public function actionOne()

{  $phpObject=User::model()->findByPk(1); 

		  

   $jsonObject=json_encode($user->getAttributes());//For convenience I have created the a json object from PHP object.

			

   $this->render("one",array("model"=>$jsonObject));

}



VIEW(one.php)




$model=json_decode($model);

//echo $model->name; you can access this way .Here I have used CDetailView.


$this->widget('zii.widgets.CDetailView', array(

        'data'=>$model,

        'attributes'=>array(

                'id',

                'name',

                'phone',

                'email',

                'address'

        ),

));



Regards.

Thanks Srinivasan, That ll surely solve the problem … I wanted this thing only… May be you can solve another problem regarding Json.

How can I get simply json object in CListView’s partial render “_view” file. Some elaboration ll clear the scenario.

Situation is something like that…

currently




$this->widget('zii.widgets.CListView', array(

        'id'=>'list',

        'dataProvider'=>$dataProvider,

        'itemView'=>'_view',

)); 



in _view.php for every element of the list I want to create json object, so that I can grab the object any time from js file and get those values. And it will be consistent even if I do use some




$.fn.yiiListViewUpdate("list");



to update the list.

Thanks for the previous answer once again. Hope you ll be able to rescue me. - Maclein.

Dear Friend

I am not completely able to comprehend your requirement.

Now at any _view just register the following code and look at the console.




<?php

Yii::app()->clientScript->registerSCript($data->id,'

console.log('.json_encode($data->getAttributes()).');


');

?>



I just transformed the data in individual view to json objects.

Now we can do anything witht that.

Is this your requirement?

Yea its almost right to the point, but…

what you have shown over here, is it possible to get those data from js file?

If so then let us assume that from CListView 10 items are generated at a time. Now If I do like this




<?php

Yii::app()->clientScript->registerSCript($data->id,'

console.log('.json_encode($data->getAttributes()).');


');

?>

<?php echo CHtml::link("link","javascript:void(0);",array("id"=>data->id)); ?>



in _view.php

and onClick event for that link, I want to get that individual JSON object in JS file, then how can I recognize that this is the object I wanted to get?

List is almost like that after rendering

when I am gonna click on $("a#2") , I will get only the data of 2List

and when I am gonna click on $("a#1"), I will get only the data of 1List

How can I recognize the individual JSON object from the whole list?

Dear Friend

Something similar to the following?




echo CHtml::link('Get this JSON at Console on Click',"#",array("id"=>$data->id));


Yii::app()->clientScript->registerScript($data->id,'

$("#'.$data->id.'").click(function(){

console.log('.json_encode($data->getAttributes()).');

return false;}

);


');



Nope not that way, rather up to this its okay. Now since I have 3 different items in my list how should I name those json objects uniquely so that can get access to those objects at any point of time by using either their name or id, by which they are uniquely identified.

And another thing, which is different from the previous context, If I use $.fn.yiiListViewUpdate(id) to update the list will the Json objects be generated for the updated values as well as the previous values?

The reason that I am asking this question as I have once used <script></script> tag inside _view. After updating the list by $.fn.yiiListViewUpdate(id) , there was no script at all.

Thanks,

Maclein

Does this help?




echo CHtml::link('Get JSON at Console',"#",array("id"=>$data->id));


Yii::app()->clientScript->registerScript($data->id,'

var ob'.$data->id.'='.json_encode($data->getAttributes()).';

$("#'.$data->id.'").click(function(){

console.log(ob'.$data->id.');

return false;}

);


');



Thanks Seevasan. Thats perfectly what I wanted. Thanks. Previously I had done the same thing using script tag. But it didn’t go well in long run. But now how I get those object in JS file as object is dynamically dependent upon data id. And If the list is updated will those objects will be updated

Like as here we got

will the ‘ob’ object gets updated automatically if I update the list.

Thanks

Maclein