1

I have a jQuery plugin that displays an organization chart based off a <ul> heirarchy.

This will allow my users to drag and drop employees around, and once dropped, an event fires telling me the DOM has changed. At this point I want to read the <ul>, and build a JSON class off it so I can send it to my server (ASP .NET MVC) for further processing.

Are there any libraries out there that can do this for me, and if not, how would I go about doing it myself?

An example of the html:

<ul id="org" style="display:none">
<li>
  Food
  <ul>
    <li>Beer</li>
    <li>Vegetables
      <ul>
        <li>Pumpkin</li>
        <li><a href="http://tquila.com" target="_blank">Aubergine</a></li>
      </ul>
    </li>
    <li>Bread</li>
    <li>Chocolate
      <ul>
        <li>Topdeck</li>
        <li>Reese's Cups</li>
      </ul>
    </li>
  </ul>
</li>
</ul>
Daniel Minnaar
  • 5,865
  • 5
  • 31
  • 52
  • Good question. I don't know of any libraries, and I *could* suggest a way of doing it with loops etc. But maybe a string replace would do the job actually. –  Oct 01 '15 at 07:19
  • A string replace? Like replacing the
      and
    • tags with something else? Hmmm. Now I'm wondering if I can't just parse this as xml?
    – Daniel Minnaar Oct 01 '15 at 07:24
  • If it *is* valid XML, you could use [Converting between JSON and XML](http://www.newtonsoft.com/json/help/html/convertingjsonandxml.htm). (The above sample passes an XML validation check.) – dbc Oct 01 '15 at 07:27
  • but this is not XML, it's HTML. I think string replace is good in this situation – Kien Chu Oct 01 '15 at 07:27
  • @kienct89 Ok, replace it with what? I'm unsure of what OP was trying to achieve... – Daniel Minnaar Oct 01 '15 at 07:29
  • Replace `
      ` with `[` and `
    • ` with `{`. Likewise, close them by replacing `
    ` with `]` and `` with `}`. Surround your inner text of each element with a surrounding `'`. Note: This doesn't cater for classes, ids or other attributes of the elements you're replacing (i.e. your replace won't match `
      ` if it's `
        `. This doesn't really matter if your example is accurate, since you don't have any (apart from the root, which you can just replace altogether by working with the `innerHTML` instead of `outerHTML` or `#org')`. I also didn't mention `:`.
    –  Oct 01 '15 at 07:31
  • try with this http://stackoverflow.com/questions/7993066/convert-dom-structure-to-json#answer-7993639 – Jacky Oct 01 '15 at 07:33
  • @JayMee Nice, that's a smart hack. Initially I was hoping to add an `id` for each `
  • ` representing an id in the database (to unpack at the server), but perhaps I could put this in the innerHtml somewhere. What do you suggest?
  • – Daniel Minnaar Oct 01 '15 at 07:36
  • You could do, and you could take the value of the id (using jQuery) and add it to the object that you'll now make from the id. This is beginning to sound pretty hacky though. You're approaching territory in which I usually like to look for alternative solutions. A massive chain of string replaces and moving ids around usually means a headache of some sort. –  Oct 01 '15 at 07:38
  • @JayMee Yeah, I can see myself working through some complicated regex to read between the lines. I think a recursive loop with a jQuery selector for the elements would be the most robust perhaps. – Daniel Minnaar Oct 01 '15 at 07:41