Jump to content
php.lv forumi

Update Nestable list


Pieduriens

Recommended Posts

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 by Pieduriens
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

<?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 by jurchiks
Link to comment
Share on other sites

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 by codez
Link to comment
Share on other sites

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}]
Link to comment
Share on other sites

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 by Pieduriens
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...