Solved

Styling Json with Jquery

Posted on 2013-11-07
9
206 Views
Last Modified: 2013-11-11
I hav Json value that has multiple values separated by a comma, Is there a way to have that rendered in the dom as a selection dropdown input.

      $.getJSON('@Url.Action("my", "Home")', function(result) {
                        var ddl = $('#MyList');
                   ddl.empty();
                   $(result).each(function (i, item) {
                       alert(this.Nome);
                       ddl.append(
                           $('<option/>', {
                               value: this.Id

                           }).html(this.Nome)

                       );
                   });
               });
           });

Open in new window

0
Comment
Question by:Seven price
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 39633078
You can just use the split function on your result variable assuming it is a string.  If it is returned as a json object then I'd need to know the object property holding the value you speak of eg result.myval

I've assumed as such below with adapting your code

http://www.w3schools.com/jsref/jsref_split.asp

      $.getJSON('@Url.Action("my", "Home")', function(result) {
                        var ddl = $('#MyList');
                   ddl.empty();
                   res = result.myval.split(",");
                   $(res).each(function (i, item) {
                       alert(this.Nome);
                       ddl.append(
                           $('<option/>', {
                               value: this.Id

                           }).html(this.Nome)

                       );
                   });
               });
           });

Open in new window

0
 
LVL 9

Author Comment

by:Seven price
ID: 39638814
Not working. matter a fact the alert is not even working.
0
 
LVL 9

Author Closing Comment

by:Seven price
ID: 39638927
this is not really the problem or the solution but it is coming from the controller
0
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 43

Expert Comment

by:Rob
ID: 39639728
If that's the case there's no point closing the question.  

It could be a simple matter of what type of object is being returned to you in the json call.  I don't have access to that and would need your feedback.

Can you tell me what the structure of the "result" object is?  using "console.log(result) or console.dir(result)" will output the contents of the variable to the console (press F12 to get developer tools up.
0
 
LVL 9

Author Comment

by:Seven price
ID: 39640331
Name
	[Object { Selected=false, Text="12345", Value=null}
	
0
	Object { Selected=false, Text="56789", Value=null}
	
1
	Object { Selected=false, Text="987665", Value=null}
	
2
	Object { Selected=false, Text="345454", Value=null}
	
3
	Object { Selected=false, Text="232323", Value=null}

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 39640367
Ok, so it's returning an array of records with the following properties: Selected, Text and Value.  However there isn't a value "separated by commas".  What data are you expecting?
0
 
LVL 43

Expert Comment

by:Rob
ID: 39640426
http://jsbin.com/ugUmANe/1/edit

$(myjson).each(function (i, item) {
    alert(this.Text);
    ddl.append(
      $('<option/>', {
        value: this.Id
        
      }).html(this.Text)
      
    );
  });

Open in new window

0
 
LVL 9

Author Comment

by:Seven price
ID: 39640504
it worked. Yes thanks. I will correct the points for the correct answer
0
 
LVL 43

Expert Comment

by:Rob
ID: 39640572
Thanks, appreciate it
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question