I have a tree structure that I'm trying to sort with a jQuery nestedSortable plugin.
Here you can see my code and a working example:
$(document).ready(function(){
$('.sortable').nestedSortable({
handle: 'div',
items: 'li',
toleranceElement: '> div',
change: function(){
console.log('moved!');
console.log(this);
console.log($(this));
var arraied = $(this).nestedSortable('toArray', {startDepthCount: 0});
arraied = dump(arraied);
console.log(arraied);
}
});
});
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Strings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script src="http://mjsarfatti.com/sandbox/nestedSortable/jquery.mjs.nestedSortable.js"></script>
<ol class="sortable">
<li><div>List line 1</div></li>
<li>
<div>List line 2</div>
<ol>
<li><div>List line 3</div></li>
<li><div>List line 4</div></li>
</ol>
</li>
<li><div>List line 5</div></li>
</ol>
You can move nodes around the tree, so that's working fine. Unfortunately, I'm not able to get a response array so I can update my database values.
If you check the browser console while moving around those tiny little node things, you can see that I constantly get the error:
"Uncaught TypeError: Cannot read property 'match' of undefined".
What could be the reason of this error and, equally important, how can I get an array after a tree adjustment?