Pieduriens Posted October 7, 2013 Report Share Posted October 7, 2013 (edited) Labdien. Saskāros ar problēmu, kuru nespēju ļoģiski atrisināt. Tātad esmu izveidojis ar Jūsu palīdzību Nestable list menu (http://php.lv/f/topic/21576-sub-array-menu/). Tālāk scenārijs paredz, pēc tabulas sakārtošanas ar jquery nestable, serialize izvadi: [{"id":1,"children":[{"id":4}]},{"id":2}] Pēc json_decode iegūstu array: Array ( [0] => Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 4 ) ) ) [1] => Array ( [id] => 2 ) ) Kā sakārtot array tā lai iegūtu ko līdzīgu: $menu = array( array( 'name' => 'Item 1', 'id' => '1' ,'children' => '0'), array( 'name' => 'Item 4', 'id' => '4' ,'children' => '1'), array( 'name' => 'Item 3', 'id' => '3' ,'children' => '0'), ); Kā arī turpmākais loģiskais risinājums. Tabula satur rindās visu menu izskatu( kārtību utt). Liekas ka izmantojot nestable list, pirms menu updeita, man būtu jāiztīra visa db tabula lai updeitotu to? Paldies. UPDATE Tur, kur 'children' => '0' , tā ir root kategorija ( Item1, Item 3 ir root kategorijas ) Tur, kur 'children' => 'nav 0' children ir augstākas kategorijas id. ( Item2 ir sub zem Item 1) Edited October 7, 2013 by Pieduriens Quote Link to comment Share on other sites More sharing options...
101111 Posted October 7, 2013 Report Share Posted October 7, 2013 Ko nozīmē šis? 'children' => '0' Tas ir skaits vai kas? Tas neatbilst tam serializējumam ko sākumā minēji. Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted October 7, 2013 Author Report Share Posted October 7, 2013 Ko nozīmē šis? 'children' => '0' Tas ir skaits vai kas? Tas neatbilst tam serializējumam ko sākumā minēji. Atvainojos, aizmirsu šo atrunāt. Skatīt 1. posta UPDATE. Quote Link to comment Share on other sites More sharing options...
jurchiks Posted October 8, 2013 Report Share Posted October 8, 2013 (edited) Tu jauc child ar parent. Item 4 parent ir Item 1. Ja tev būtu child, tad būtu Item 1 children = [4] (jābūt masīvam, jo var būt vairāki child itemi). Edited October 8, 2013 by jurchiks Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted October 8, 2013 Author Report Share Posted October 8, 2013 Tu jauc child ar parent. Item 4 parent ir Item 1. Ja tev būtu child, tad būtu Item 1 children = [4] (jābūt masīvam, jo var būt vairāki child itemi). ja var but vairaki, tacu planoju tos vairakus child itemus kartot ar position vertibu. Quote Link to comment Share on other sites More sharing options...
F3llony Posted October 9, 2013 Report Share Posted October 9, 2013 http://mbraak.github.io/jqTree/ http://stackoverflow.com/a/8587437 Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted October 9, 2013 Author Report Share Posted October 9, 2013 Paldies F3llony par padomu. Taču vēlos attīstīt šo: Uzrakstīju kā tam itkā loģiski vajadzētu izskatīties, bet šis nav ejošs: $string = '[{"id":1,"children":[{"id":4}]},{"id":2}]'; $data = json_decode($string, true); $menu = array(); function build_menu($data, $i = 0){ foreach ($data as $items){ $i++; if(array_key_exists("children",$data)){ foreach ($items as $item){ array_push($menu, $item['id']); build_menu($menu, $i); } } else{ array_push($menu, $items['id']); } } return $menu; } $menu = build_menu($data); var_dump($menu); Quote Link to comment Share on other sites More sharing options...
jurchiks Posted October 9, 2013 Report Share Posted October 9, 2013 (edited) <?php $string = '[{"id":1,"children":[{"id":4},{"id":5,"children":[{"id":6}]}]},{"id":2}]'; $data = json_decode($string, true); function draw_menu($data, $nestingLevel = 0) { foreach ($data as $menuItem) { echo str_repeat('-', $nestingLevel * 4), $menuItem['id'], '<br/>'; if (!empty($menuItem['children'])) { draw_menu($menuItem['children'], $nestingLevel + 1); } } } draw_menu($data); Edited October 9, 2013 by jurchiks Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted October 9, 2013 Author Report Share Posted October 9, 2013 Paldies Jurchik par piemēru. Gandrīz vai perfekto. Tikai $menu ir jāatgriež nevis jāizvada. Quote Link to comment Share on other sites More sharing options...
codez Posted October 9, 2013 Report Share Posted October 9, 2013 (edited) var jau ari klienta pusē pārkonvertēt koku masīvā - ideja tā pati - ejam rekursīvi ar dfs cauri kokam un katru zaru metam masīvā: http://jsfiddle.net/UujJS/ var data=[{"id":1,"children":[{"id":4}]},{"id":2}]; var result=[]; function conv(node,parent){ for(var i in node){ result.push({id:node[i].id,pid:parent}); if (node[i]['children']) conv(node[i]['children'],node[i]['id']); } } conv(data, 0); $('#info').append($('<div>').html(JSON.stringify(data))); $('#info').append($('<div>').html(JSON.stringify(result))); P.S. Nedaudz pareizāk strukturēts kods - bez globāliem mainīgajiem: http://jsfiddle.net/UujJS/1/ function conv(data){ var result=[]; function dfs(node, parent) { for(var i in node){ result.push({id:node[i].id,pid:parent}); if (node[i]['children']) dfs(node[i]['children'],node[i]['id']); } } dfs(data, 0); return result; } var data=[{"id":1,"children":[{"id":4}]},{"id":2}]; var result=conv(data); $('#info').append($('<div>').html(JSON.stringify(data))); $('#info').append($('<div>').html(JSON.stringify(result))); Edited October 9, 2013 by codez Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted October 9, 2013 Author Report Share Posted October 9, 2013 Super ! Vienkārši super..Client Side Must Have! Tik kad lieku lai nolasītu saturu no textarea. Ar statiski definētu var protams viss ir ideāli :) var data= $("textarea#nestable_list_1_output").val(); tieku outputā pie: [{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0},{"pid":0}] Quote Link to comment Share on other sites More sharing options...
codez Posted October 9, 2013 Report Share Posted October 9, 2013 ar .val tu iegūsti json tekstu, kurš tev jāpārveido js objektā: http://jsfiddle.net/H6TAk/ var text=$('#ta').val(); var data=$.parseJSON(text); console.info(text); console.info(data); konsolē vari redzēt atškirību starp tekstu un objektu/masīvu. Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted October 11, 2013 Author Report Share Posted October 11, 2013 (edited) Skaidrs. text ir vienkārši kā strings, bet man vajag array ko pēc tam padod PHP json_decode. Vai Tavai funkcijai: function conv(data) ir iespēja pielikt klāt position? Piemēram: [ {"id":1,"pid":0,"position":100}, // Pirmais roots {"id":4,"pid":1,"position":100}, // Pirmais sub roots {"id":5,"pid":1,"position":101}, // Otrais sub roots {"id":2,"pid":0,"position":101}, // Otrais roots {"id":3,"pid":0,"position":102}, // Treshais roots ] Tiku galā: function conv(data){ var result=[]; function dfs(node, parent) { for(var i in node){ result.push({id:node[i].id,pid:parent,position:position}); position++; if (node[i]['children']) dfs(node[i]['children'],node[i]['id']); } } var position=100; dfs(data, 0); return result; } Edited October 12, 2013 by Pieduriens Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.