EDIT: Dopo aver risposto alla grande @ vadim-ashikhman. La fine della domanda.aggiornamento nestet set tree con php e plug-in jquery nestable.js
risorse che sto usando:
- PHP quadro Codeigniter
- Biblioteca insieme nidificato: https://github.com/olimortimer/ci-nested-sets/
- jQuery libreria: http://dbushell.github.io/Nestable/
sono riuscito ad aggiungere categorie e sottocategorie, senza Qualche problema.
Sono stato anche in grado di mostrare il menu con tutte le categorie con annidabile libreria jQuery.
Fin qui tutto bene.
Ora, quando cerco di ordinare attraverso il drag and drop. Catturato l'intero array, ecco un esempio:
[{
"id": 1,
"children": [{
"id": 2,
"children": [{
"id": 3
}, {
"id": 6
}, {
"id": 5
}, {
"id": 7
}, {
"id": 9
}]
}, {
"id": 8,
"children": [{
"id": 10,
"children": [{
"id": 11
}, {
"id": 12
}, {
"id": 13
}, {
"id": 14
}, {
"id": 15
}]
}, {
"id": 16,
"children": [{
"id": 17
}, {
"id": 18
}, {
"id": 19
}, {
"id": 20
}]
}]
}, {
"id": 22,
"children": [{
"id": 23
}, {
"id": 24
}, {
"id": 25
}, {
"id": 26
}]
}, {
"id": 27,
"children": [{
"id": 28
}, {
"id": 29
}, {
"id": 30
}, {
"id": 31
}]
}, {
"id": 32
}, {
"id": 39
}, {
"id": 40
}]
}]
Ma non come aggiornare le posizioni, questo mi ha sconcertato, ho preso quasi 16 ore di funzionamento test, ma senza alcun risultato.
- Ho provato a riordinare gli stessi dati con i campi di sinistra e righ.
- Inoltre, ho provato a pulire la tabella e ad aggiungere nuovamente il menu.
Senza risultati favorevoli.
Quindi vengo da te per vedere se puoi aiutarmi.
EDIT: Dopo il grande risposta @ Vadim-ashikhman
- ho usato una forcella di annidabile biblioteca.js, perché aveva una funzione che potrebbe ottenere la posizione (https://github.com/BeFiveINFO/Nestable/)
- Codice Javsascript:
$('#nestable').nestable({
group: 1,
maxDepth :6
}).on('dragEnd', function(event, item, source, destination, position) {
\t // Make an ajax request to persist move on database
\t // here you can pass item-id, source-id, destination-id and position index to the server
\t // ....
\t var parent_id = $(item).parent().parent().data('idcata');
\t var actual_id = $(item).data('idcata');
\t var prev_id = $(item).prev("li").data('idcata');
\t var page_id = $(item).data('pagina-id');
\t console.log("id "+ actual_id + "\nParent: "+ parent_id +"\nPosition:" + position + "\nPrev : " + prev_id + "\nPagina_id: "+page_id);
\t $.ajax({
type: "POST",
dataType: "json",
url: '<?=site_url("admin/categories/ordenar")?>',
data: {
id:actual_id,
parent_id:parent_id,
position:position,
prev_id:prev_id,
page_id:page_id,
},
cache: false,
success: function(data) {
if(data.data==1)
alert('Guardado!!');
else
alert('No se ha podido guardar la posición');
},
error: function() {
alert('No se ha podido guardar la posición');
}
});
\t });
- codice PHP:
$idcata = $this->input->post('id');
\t $newParentId = $this->input->post('parent_id');
\t $newPosition = $this->input->post('position');
\t $prevId = $this->input->post('prev_id');
\t $page_id = $this->input->post('page_id');
\t $this->nested_set = new Nested_set();
\t $this->nested_set->setControlParams('nested_set_tree');
\t $categoria = $this->nested_set->getNodeFromId($idcata);
\t $categoriaPadre = $this->nested_set->getNodeFromId($newParentId);
\t if($newPosition == 0){
\t \t $newCategoria = $this->nested_set->setNodeAsFirstChild($categoria,$categoriaPadre);
\t }else{
\t \t $prevCategoria = $this->nested_set->getNodeFromId($prevId);
\t \t $newCategoria = $this->nested_set->setNodeAsNextSibling($categoria,$prevCategoria);
\t }
TADA !!! E funziona perfettamente
Non riesco a capire quale sia la tua domanda e quale risultato ti aspetti. Per favore descrivi di più su ciò che vuoi. Vuoi utilizzare il formato JSON per generare il menu? – North
Accetto con @North - per favore spiega più chiaramente il tuo problema/situazione. – sitilge
Usa questo plugin basato su jQuery UI [nestedSortable] (https://github.com/ilikenwf/nestedSortable) – DarkMaze