prepare a json object in view to pass the controller
i have a tree开发者_开发百科 that has a ul li HTML structure .
In every li there is a span that contains the node name of the tree.
I have to make a json object after reading the tree it should look like some thing in this manner
this.orgStructureId = null;
this.name = null;
this.nodeList = null;
this.parent = null;
this.list = null;
i am not sure that it will be the structure but some thing similar.so how can i make this object do you need any more inputs for making the json object.
http://i.stack.imgur.com/9itqx.jpg Tree sample snap shot
var json = {};
$('li').each(function(){
json[this.innerHTML] = null;
})
Then you just need to serialize it
Tree is generated on the server
If your tree is generated on the server side, you could as well add its JSON string to your HTML and use that preprepared JSON data instead of generating in on the client side...
<ul id="TreeRoot" data="{ name: 'Some name', ... }">
<li>...</li>
...
</ul>
Then just call
var data = $.parseJSON($("#TreeRoot").attr("data"));
and you'll get your JSON data to send to the server. But there will be another obstacle... passing that data to controller action.
I suggest you read this blog post about sending JSON data, that will make your life even easier...
Tree is generated on the client
In case your tree is generated/edited on the client by users you will have no other option but either:
- traverse DOM elements of your hierarchy data and generate JSON
- generate JSON data while user creates the tree and have it prepared on the go and use the end result
I'd probably go with the second one, because you'll be manipulating data already anyway. So why not generate HTML nodes and populate variables as well.
精彩评论