Solved

Styling Json with Jquery

Posted on 2013-11-07
9
201 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:sevensnake77
  • 5
  • 4
9 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE 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:sevensnake77
ID: 39638814
Not working. matter a fact the alert is not even working.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 39638927
this is not really the problem or the solution but it is coming from the controller
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
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:sevensnake77
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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Expert Comment

by:Rob Jurd, EE MVE
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:sevensnake77
ID: 39640504
it worked. Yes thanks. I will correct the points for the correct answer
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39640572
Thanks, appreciate it
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 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