2013-04-09 86 views
2

我試圖在單擊父節點(類別)時獲取子節點(在我的情況下是子類別)。但我可以看到沒有Ajax請求發生在我的情況。我使用了本網站的解決方案 - (http://www.miketyka.com/2012/10/lazy-loading-with-jstree-and-ajax/)。這裏是我下面的代碼jstree -jstree中沒有ajax請求

/** 
    * menu tree generation 
    */ 
$("#menu_tree") 
.bind("open_node.jstree", function (e, data) { // this binding will collapse all nodes whenever user expands any single node 
    data.rslt.obj.siblings().filter(".jstree-open").each(function() { 
    data.inst.close_node(this); 
    }); 
}) 
.jstree({ 
    "json_data": { 
     "ajax": { 
      "url": base_url + "draw/get_child" 
     } 
    }, 
    "types": { // this will let user to expand or collapse node by clicking on the node title 
     "types": { 
     "default": { 
      "select_node": function (e) { 
       this.toggle_node(e); 
       return false; 
      } 
     } 
     } 
    }, 
    "core": { 
    "html_titles": true, 
    "load_open": true 
}, 
    "plugins": ["themes", "json_data", "html_data", "types", "ui"], 
    "themes": { 
    "theme": "apple", 
    "dots": true, 
    "icons": true 
    } 
}); 

HTML部分低於 -

<div class="" id="menu_tree" style="width: 500px;z-index: 1; float: right;position: relative;"> 
    <ul class="" style="position: absolute;top: 0px;left: 0px;"> 
    <?php 
     if (!empty($parent_categories)) { 
      foreach ($parent_categories as $parent_categories) { 
    ?> 
     <li id="cat<?php echo $parent_categories->objcat_id; ?>"> 
      <a href="#" class=""><?php echo $parent_categories->category_name; ?></a> 
     </li> 
    <?php 
      } 
     } 
    ?> 
    </ul> 
</div> 

我想獲得的數據如下 - (我用笨)

$this->load->model('object_category_model'); 
$result = array(); 
$sub_category = $this->object_category_model->get_subcategories_by_category_id($this->input->post('parent_id')); 
echo json_encode($sub_category); 

我只是第一次加載父節點,所有的子節點都應該通過用戶單擊父節點加載。

如果我使用「html_data」而不是「json_data」然後發生的請求,我可以看到加載GIF,但後來我得到這個錯誤「未捕獲錯誤:NotFoundError:DOM異常8」,但如果我使用「json_data」那麼什麼都不會發生,在螢火蟲中沒有ajax請求。

我不明白我在做什麼錯...任何解決方案,請嗎?

+0

你能看到int螢火蟲嗎?是否發送了AJAX請求? Web服務器是否收到它?簡化您的代碼。首先使用ajax中的url。不確定「load_open」是做什麼的。以前從未使用過。 – Radek 2013-04-09 23:23:14

+0

@Radek ---沒有發送ajax請求,這是問題,因爲我甚至不明白在哪裏可以找到錯誤... – rafi 2013-04-10 04:34:00

+0

@Radek ---我試圖通過單擊加載子節點父節點名稱動態與ajax。 – rafi 2013-04-10 04:40:51

回答

2

的努力之後4天,我得到f回答這個問題,我做錯了。我只是以錯誤的格式從php部分返回數據。現在我已經修復了格式,運行良好,我忘了使用「jstree-closed」類。正確的格式(我現在使用的格式)給出如下─

 $sub_category = $this->object_category_model->get_subcategories_by_category_id($_GET['id']); 

     if ($sub_category != FALSE) { 
      foreach ($sub_category as $sub_category) { 

       $result = '<li class="jstree-closed cat" id="' . $sub_category->objcat_id . '"><a href="#">' . $sub_category->category_name . '</a></li>'; 

       echo $result; 
      } 
     } 

而且我已經使用html_data而非json_data。

"html_data" : { //generating child nodes of selected parent node dynamically using ajax 
       "ajax" : { 
        "url" : base_url + "draw/get_child", 
        "data" : function (n) { 
          return { 
           id : n.attr ? n.attr("id") : 0 
          }; 
        } 
       } 
      } 

謝謝大家試圖幫助我。 :-)

1

嘗試改變

「json_data」:{ 「AJAX」:{ 「URL」:函數(節點){

「json_data」:{ 「AJAX」: { 緩存:假的, 「URL」:功能(節點){

+0

仍然不能正常工作 – rafi 2013-04-09 10:17:46

+2

嘗試在jsfiddle.net上放置一個演示 - 這樣人們可以更好地幫助你。 – Nathan 2013-04-09 10:43:24

+0

這是很難做.... :-( – rafi 2013-04-09 12:10:07