Build jQuery Nestable from JSON source.
Example of how to build jQuery Nestable from JSON.
Orignal plugin available at https://github.com/dbushell/Nestable
HTML
<script src="https://dl.dropboxusercontent.com/u/34573370/js/nestable/jquery.nestable.js"></script>
<strong>Build jQuery Nestable from JSON source.</strong> Orginal plugin source code at https://github.com/dbushell/Nestable
<br /><br />
<hr />
<div id="nestable">
<ul class="dd-list"></ul>
</div>
CSS
.cf:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
* html .cf {
zoom: 1;
}
*:first-child+html .cf {
zoom: 1;
}
html {
margin: 0;
padding: 0;
}
body {
font-size: 100%;
margin: 0;
padding: 1.75em;
font-family: 'Helvetica Neue', Arial, sans-serif;
}
h1 {
font-size: 1.75em;
margin: 0 0 0.6em 0;
}
a {
color: #2996cc;
}
a:hover {
text-decoration: none;
}
p {
line-height: 1.5em;
}
.small {
color: #666;
font-size: 0.875em;
}
.large {
font-size: 1.25em;
}
/**
* Nestable
*/
.dd {
position: relative;
display: block;
margin: 0;
padding: 0;
max-width: 600px;
list-style: none;
font-size: 13px;
line-height: 20px;
}
.dd-list {
display: block;
position: relative;
margin: 0;
padding: 0;
list-style: none;
}
.dd-list .dd-list {
padding-left: 30px;
}
.dd-collapsed .dd-list {
display: none;
}
.dd-item,
.dd-empty,
.dd-placeholder {
display: block;
position: relative;
margin: 0;
padding: 0;
min-height: 20px;
font-size: 13px;
line-height: 20px;
}
.dd-handle {
display: block;
height: 30px;
margin: 5px 0;
padding: 5px 10px;
color: #333;
text-decoration: none;
font-weight: bold;
border: 1px solid #ccc;
background: #fafafa;
background: -webkit-linear-gradient(top, #fafafa 0%, #eee 100%);
background: -moz-linear-gradient(top, #fafafa 0%, #eee 100%);
background: linear-gradient(top, #fafafa 0%, #eee 100%);
-webkit-border-radius: 3px;
border-radius: 3px;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
.dd-handle:hover {
color: #2ea8e5;
background: #fff;
}
.dd-item>button {
display: block;
position: relative;
cursor: pointer;
float: left;
width: 25px;
height: 20px;
margin: 5px 0;
padding: 0;
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
border: 0;
background: transparent;
font-size: 12px;
line-height: 1;
text-align: center;
font-weight:...
JavaScript
var obj = '[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5, "children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]';
function buildItem(item) {
var html = "<li class='dd-item' data-id='" + item.id + "' id='" + item.id + "'>";
html += "<div class='dd-handle'>" + item.id + "</div>";
if (item.children) {
html += "<ol class='dd-list'>";
$.each(item.children, function(index, sub) {
html += buildItem(sub);
});
html += "</ol>";
}
html += "</li>";
return html;
}
$.each(JSON.parse(obj), function(index, item) {
$('#nestable ul').append(buildItem(item));
});
$('#nestable').nestable();