I've tried to store a list-structure in my db (based on this question [1]: How to serialize a nested sortable ul list) and after that I need a way to restore/reset the list if the page is refreshed or load new based on the data from db.
Is there a build-in way to do this or how can I do that?
In my list, I use nested elements to support the way to create flexible nested structure.
This is what I already have
The list
<ul id="mysite" class="mysite editor-ul ui-sortable-handle ui-sortable">
<li class="draggable editor-li editor-li-head ui-sortable-handle">Headline</li>
<li class="draggable editor-li ui-sortable-handle" id="p-1">
Item1
<ul class="mysite editor-ul ui-sortable"></ul>
</li>
<li class="draggable editor-li ui-sortable-handle" id="p-5">
Item 2
<ul class="mysite editor-ul ui-sortable">
<li class="draggable editor-li ui-sortable-handle" id="p-7" style="">
Item 3
<ul class="mysite editor-ul ui-sortable"></ul>
</li>
<li class="draggable editor-li ui-sortable-handle" id="p-6" style="">
Item 4
<ul class="mysite editor-ul ui-sortable"></ul>
</li>
</ul>
</li>
</ul>
The JS Code
$(function() {
//$(".mysite").sortable({
$(".mysite").sortable({
connectWith: ".mysite",
placeholder: "placeholder",
update: function(event, ui) {
var struct = [];
var i = 0;
$(".mysite").each(function(ind, el) {
struct[ind] = {
index: i,
class: $(el).attr("class"),
count: $(el).children().length,
parent: $(el).parent().is("li") ? $(el).parent().attr("id") : "",
parentIndex: $(el).parent().is("li") ? $(el).parent().index() : "",
array: $(el).sortable("toArray"),
serial: $(el).sortable("serialize")
};
i++;
});
console.log("Structure", struct);
$.ajax({
data: {
"'.$this->security->get_csrf_token_name().'": "'.$this->security->get_csrf_hash().'",
"job": "updateSiteStruc",
"data": struct
},
type: "POST",
url: "'.base_url().'"
});
var iArr = [];
var iSer = [];
$(".mysite:eq(0) li").each(function(ind, el) {
if ($(el).attr("id") != undefined) {
var label = $(el).attr("id");
} else {
return true;
}
iArr.push(label);
var pre = label.slice(0, label.indexOf("-")) + "[]=";
iSer.push(pre + label.slice(label.indexOf("-") + 1));
if ($(el).children().eq(0).is("ul")) {
$(el).find("li").each(function() {
iArr.push($(this).attr("id"));
iSer.push(pre + $(this).attr("id").slice(label.indexOf("-") + 1));
});
}
});
console.log("Items Array", iArr);
console.log("Items Serial", iSer.join("&"));
}
}).disableSelection();
});
Based on this I store the structure
- send by ajax
- save
json_encode($post['data'])
in db
So now I have in my db
"[{"array": ["", "p-1", "p-5"], "class": "mysite editor-ul ui-sortable-handle ui-sortable", "count": "3", "index": "0", "parent": "", "serial": "p[]=1&p[]=5", "parentIndex": ""}, {"class": "mysite editor-ul ui-sortable", "count": "0", "index": "1", "parent": "p-1", "serial": "", "parentIndex": "1"}, {"array": ["p-7", "p-6"], "class": "mysite editor-ul ui-sortable", "count": "2", "index": "2", "parent": "p-5", "serial": "p[]=7&p[]=6", "parentIndex": "2"}, {"class": "mysite editor-ul ui-sortable", "count": "0", "index": "3", "parent": "p-7", "serial": "", "parentIndex": "0"}, {"class": "mysite editor-ul ui-sortable", "count": "0", "index": "4", "parent": "p-6", "serial": "", "parentIndex": "1"}]"
Formatted version of string above:
"[{
"array": ["", "p-1", "p-5"],
"class": "mysite editor-ul ui-sortable-handle ui-sortable",
"count": "3",
"index": "0",
"parent": "",
"serial": "p[]=1&p[]=5",
"parentIndex": ""
}, {
"class": "mysite editor-ul ui-sortable",
"count": "0",
"index": "1",
"parent": "p-1",
"serial": "",
"parentIndex": "1"
}, {
"array": ["p-7", "p-6"],
"class": "mysite editor-ul ui-sortable",
"count": "2",
"index": "2",
"parent": "p-5",
"serial": "p[]=7&p[]=6",
"parentIndex": "2"
}, {
"class": "mysite editor-ul ui-sortable",
"count": "0",
"index": "3",
"parent": "p-7",
"serial": "",
"parentIndex": "0"
}, {
"class": "mysite editor-ul ui-sortable",
"count": "0",
"index": "4",
"parent": "p-6",
"serial": "",
"parentIndex": "1"
}]"