Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Styling Json with Jquery

Posted on 2013-11-07
9
Medium Priority
?
210 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
  • 5
  • 4
9 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 1500 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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