Yii Framework Forum: Is It Possible To Render One Page Along With Json Data In Yii ? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Is It Possible To Render One Page Along With Json Data In Yii ? Rate Topic: -----

#1 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 13 January 2013 - 08:35 AM

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
0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 13 January 2013 - 10:47 AM

View PostMaclein, on 13 January 2013 - 08:35 AM, said:

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
0

#3 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 13 January 2013 - 12:13 PM

/* moved from Extensions forum */
"Less noise - more signal"
0

#4 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 13 January 2013 - 10:40 PM

View PostReza m, on 13 January 2013 - 10:47 AM, said:

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


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?
0

#5 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 14 January 2013 - 03:59 AM

There are several extensions available which helps you implement that with Yii - take a look. :)
"Less noise - more signal"
0

#6 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 14 January 2013 - 01:50 PM

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.
0

#7 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 16 January 2013 - 10:28 AM

View Postseenivasan, on 14 January 2013 - 01:50 PM, said:

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?
0

#8 User is offline   BCR 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 16 January 2013 - 11:11 AM

View PostMaclein, on 16 January 2013 - 10:28 AM, said:

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.
0

#9 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 16 January 2013 - 11:31 AM

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.
0

#10 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 24 January 2013 - 03:29 PM

View Postseenivasan, on 16 January 2013 - 11:31 AM, said:

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.
0

#11 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 24 January 2013 - 03:53 PM

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?
0

#12 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 25 January 2013 - 12:05 AM

View Postseenivasan, on 24 January 2013 - 03:53 PM, said:

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

Quote

1List. FirstName- John LastName- Maclein <a id="1">link</a>
2List. FirstName- seenivasan LastName- seenivasan <a id="2">link</a>
3List. FirstName- sally LastName- Snider <a id="3">link</a>


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?
0

#13 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 25 January 2013 - 12:41 AM

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;}
);

');

0

#14 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 25 January 2013 - 01:01 AM

View Postseenivasan, on 25 January 2013 - 12:41 AM, said:

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
0

#15 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 25 January 2013 - 01:18 AM

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;}
);

');

0

#16 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 25 January 2013 - 01:38 AM

View Postseenivasan, on 25 January 2013 - 01:18 AM, said:

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

Quote

'var ob'.$data->id


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

Thanks
Maclein
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users