2013-02-13 2 views
1

자바 스크립트로 작동하는 PHP 함수를 다시 작성하려고합니다. 브라우저 측에서 작업을 수행하고 내 페이지에 추가 할 객체에 이벤트 처리기를 추가해야하기 때문입니다.settimeout이없는 재귀 js 함수

이 함수는 부모 자식 관계가있는 노드 배열을 사용하여 노드를 살펴본 다음 자식 노드와 자식 노드 등을 찾습니다 (트리의 끝을 찾을 때까지).

작업 PHP 함수 :

//print nodes 
function display_child_nodes($parent_id, $level) { 
global $task_list, $index; 
    if(isset($index[$parent_id])) 
     { 
      foreach ($index[$parent_id] as $id) { 
       echo str_repeat("-", $level) . $task_list[$id]["title"] . "<br />"; 
       display_child_nodes($id, $level + 1); 
      } 
     } 
} 
display_child_nodes(0, 0); 

PHP 함수의 결과는 보이는 같은

B

-C

-D

- E

,210

F

G

이것은 내가 쓰기 위해 노력했다 자바 스크립트이지만, 기능 show_child_nodes 그 기능이 정확 JS 아닌 내에서 형성에 나는

기능 ... 전화를 의심 첫 번째 노드를 올바르게 생성하지만 자식 노드를 분석하려고하자마자 멈추므로 함수 내에서 호출이 문제가된다는 의심이 들게됩니다.

javascript는 PHP 스크립트에서 json 배열을 받고 있으며 노드 (작업) 목록과 해당 세부 정보가 포함되어 있으며 order_index 배열은 구조화되어 있습니다 [parent_id1 => array [1,2,3], parent_id2 => [4,5], parent_id3 => [6,7,8]] etc ...에서와 같이 각 parent_id는 값으로 자식 ID를 갖는 키입니다.

{"tasks":{"2":{"id":"2","title":"Task 2","desc":"Task 2 description","parent_id":"0"},"6":{"id":"6","title":"Task 6","desc":"Task 6 description","parent_id":"0"},"1":{"id":"1","title":"Task 1","desc":"Task 1 description","parent_id":"2"},"3":{"id":"3","title":"Task 3","desc":"Task 3 description","parent_id":"1"},"4":{"id":"4","title":"Task 4","desc":"Task 4 description","parent_id":"1"},"5":{"id":"5","title":"Task 5","desc":"Task 5 description","parent_id":"3"}},"order_index":{"0":["2","6"],"2":["1"],"1":["3","4"],"3":["5"]}} 


      $.getJSON("get_items_hierarchy.php",function(data,status){ 

       var order_index = data.order_index; 
       var tasks = data.tasks; 
       var output = []; 

       function show_child_nodes(parent_id,level){ 
        if(order_index[order_index[parent_id]]) 
         { 
          $.each(order_index[parent_id],function(index,id){ 
           var margin = level * 40; 
           output.push('<div style="float: left; clear: left; margin-left: ' + margin + 'px">' + tasks[id]['title'] + '</div> '); 
           show_child_nodes(id, level + 1); 
          }); 
         } 
       } 
       show_child_nodes(0,0); 

      $.each(output,function(index,line){ 
       $("#tasks").append(line); 
      }); 

    }); 
+0

당신은 설정하는 코드를 추가 할 수 'order_index'와'tasks'? – ThinkingStiff

+0

수정 해 주셔서 감사합니다. 제게 돌아 오기 주셔서 감사합니다. 모든 자바 스크립트와 처리 과정에 대한 설명이 포함되어 있습니다 ... 희망 사항이 명확 해지기를 바랍니다. –

+1

예를 들어, 코드를 실행하는 데 필요한 예제 데이터가 있어야합니다. PHP의 축 어적 출력. –

답변

0

이 문제를 해결할 것 : 여기

는 JSON이다.

데모 :jsFiddle

변경이 라인 :

if(order_index[order_index[parent_id]]) 

에 :

if(order_index[parent_id]) 
+0

와아! 완전히 고쳤다. 고맙습니다! –

0

나는 algorith 잘못 아무것도 표시되지 않습니다 m 자체하지만, 다음과 같은 부분이 문제가 될 가능성이 높습니다 :

$.each(output, function(index,line){ 
    $("#tasks").append(line); 
}); 

이 코드는 output의 값을 참조 :하지만,

  1. $.getJSON 비동기이기 때문에 값은 준비가되지 않습니다 아직.

  2. 변수는 AJAX 콜백 내부에서만 액세스 할 수 있습니다.

코드는 대신 AJAX 콜백 함수의 끝에 삽입해야합니다.

다른 답변에서 언급 한 바와 같이

,이 문은 아마 잘못 :

if (order_index[order_index[parent_id]]) 

그냥 오타 수도 있지만 가능성이 올바른 코드는 다음과 같습니다

if (order_index[parent_id]) 
+0

아, 내가 너를 봤어 - 좋은 곳, 조정할거야! 감사! –