Yii Framework Forum: json to javascript - Yii Framework Forum

Jump to content

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

json to javascript Rate Topic: -----

#1 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 27 February 2012 - 12:09 AM

Thanks for your continuous help from all the yii members.
Here is the json output from one of my controllers
[{"attributes":{"item_name":"Ford Escape","city":"Frisco"},"relations":[]},{"attributes":{"item_name":"house","city":"Dallas"},"relations":[]},{"attributes":{"item_name":"Car","city":"Plano"},"relations":[]}] 

Here is the javascript code
function generateListElement(marker, attributes){
var ul = document.getElementById('sideContainer');
var li = document.createElement('li');
var aSel = document.createElement('a');
aSel.href = 'javascript:void(0);';
aSel.innerHTML = attributes.item_name;
aSel.onclick = function(){google.maps.event.trigger(marker, 'click')};
li.appendChild(aSel);
ul.appendChild(li);
}


My markers on the map works fine but on the sidebar <ul id="sideContainer" style></ul> the value that is being outputed is undefined and when I click on it shows the markers fine the only thing I need it the name of the it from the json array. Thanks for any help as soon as possible.
0

#2 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 27 February 2012 - 02:54 AM

View Postbonnie, on 27 February 2012 - 12:09 AM, said:

Here is the json output from the above code
[{"attributes":{"item_name":"Ford Escape","city":"Frisco"},"relations":[]},{"attributes":{"item_name":"house","city":"Dallas"},"relations":[]},{"attributes":{"item_name":"Car","city":"Plano"},"relations":[]}] 
]

Cause you hava an attributes "json array" (json.org) you cannot simply use attributes.item_name

For me it looks like that you have to loop over the json attributes array for creating the li-tags for each element (in your example 3)

For testing you can try
attributes[0].item_name; // use the first attributes element in the json array => item_name = Ford Escape

0

#3 User is offline   bonnie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 08-February 11

Posted 27 February 2012 - 01:52 PM

View Postkokomo, on 27 February 2012 - 02:54 AM, said:

Cause you hava an attributes "json array" (json.org) you cannot simply use attributes.item_name

For me it looks like that you have to loop over the json attributes array for creating the li-tags for each element (in your example 3)

For testing you can try
attributes[0].item_name; // use the first attributes element in the json array => item_name = Ford Escape


Thanks kokomo for reminding me about the loop. it's now working
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