Solved

Styling Json with Jquery

Posted on 2013-11-07
9
198 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now