2011-03-17 97 views
1

我無法將CheckBox設置爲對具有子節點的節點取消選中[「jstree-unchecked」]。當我使用螢火蟲來確定前端的節點類時,它顯示「jstree-undetermined」。我真正想要的是「jstree-unchecked」成爲類,因爲這是我從服務器傳遞過來的。無法將jstree複選框節點設置爲從JSON取消選中

以下是我使用JSON進行的AJAX調用。我還添加了從服務器獲取的JSON數據。我在服務器端格式化JSON數據的方式有什麼問題嗎?
對於以下示例「Dogs」節點導致問題。雖然「狗」的兒童未經檢查,節點類未受到任何檢查;我看到「狗」節點被選中。任何建議修復或改進代碼是不勝感激。

$(function() { 
     $("#idTOC").bind("loaded.jstree", function (e, data) { 
      data.inst.open_all(-1); // -1 opens all nodes in the container 
     }) 
     .jstree({ 
      "json_data": { 
       "ajax": { 
        "url": "http://localhost/eSiriusAjax/jqTOC.wc", 
        "aync": false, 
        "data": function (n) { 
         return { id: n.attr ? n.attr("id") : 0 }; 
        } 
       } 
      }, 
      "core": { "html_titles": true }, 
      "themes": { "theme": "apple", "dots": false, "icons": false }, 
      "plugins": ["themes", "json_data", "checkbox"], 
      "ui": { "select_limit": 1 } 
     }); 

     $("#idTOC").bind('before.jstree', function (event, data) { 
      if ((data.func === 'check_node') || (data.func === 'uncheck_node')) { 
       event.stopImmediatePropagation(); //stops changing select/deselect when clicked 
       return false; 
      } 
     }); 
    }); 

JSON數據:

[ 
    { 
    "data": "Intro", 
    "attr": { 
     "id": "1", 
     "class": "jstree-checked", 
     "href": "ProtocolIntro.aspx" 
    } 
    }, 
    { 
    "data": "Overview", 
    "attr": { 
     "id": "3", 
     "class": "jstree-unchecked", 
     "href": "ProtocolOverview.aspx" 
    } 
    }, 
    { 
    "data": "Funding", 
    "attr": { 
     "id": "5", 
     "class": "jstree-checked" 
    } 
    }, 
    { 
    "data": "Use TYPE(s)", 
    "attr": { 
     "id": "7", 
     "class": "jstree-unchecked" 
    } 
    }, 
    { 
    "data": "Transportation", 
    "attr": { 
     "id": "9", 
     "class": "jstree-unchecked" 
    } 
    }, 
    { 
    "data": "Dogs", 
    "attr": { 
     "id": "11", 
     "class": "jstree-unchecked" 
    }, 
    "children": [ 
     { 
     "data": "Info", 
     "attr": { 
      "id": "12", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "# Justification", 
     "attr": { 
      "id": "13", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Choice Justification", 
     "attr": { 
      "id": "14", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Source", 
     "attr": { 
      "id": "15", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Enrichment/Excercise", 
     "attr": { 
      "id": "16", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Quarantine/Conditioning", 
     "attr": { 
      "id": "17", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Housing", 
     "attr": { 
      "id": "18", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Use Labs", 
     "attr": { 
      "id": "19", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Surgery", 
     "attr": { 
      "id": "20", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Procedures", 
     "attr": { 
      "id": "21", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Euthanasia", 
     "attr": { 
      "id": "22", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Methodology", 
     "attr": { 
      "id": "23", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "USDA Categories", 
     "attr": { 
      "id": "24", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Adverse Consq.", 
     "attr": { 
      "id": "25", 
      "class": "jstree-unchecked" 
     } 
     } 
    ] 
    }, 
    { 
    "data": "Mice", 
    "attr": { 
     "id": "26", 
     "class": "jstree-unchecked" 
    }, 
    "children": [ 
     { 
     "data": "Info", 
     "attr": { 
      "id": "27", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "# Justification", 
     "attr": { 
      "id": "28", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Choice Justification", 
     "attr": { 
      "id": "29", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Source", 
     "attr": { 
      "id": "30", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Housing", 
     "attr": { 
      "id": "31", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Use Labs", 
     "attr": { 
      "id": "32", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Surgery", 
     "attr": { 
      "id": "33", 
      "class": "jstree-checked", 
      "href": "ProtocolSurgery.aspx" 
     } 
     }, 
     { 
     "data": "Breeding", 
     "attr": { 
      "id": "34", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Methodology", 
     "attr": { 
      "id": "35", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "USDA Categories", 
     "attr": { 
      "id": "36", 
      "class": "jstree-unchecked" 
     } 
     }, 
     { 
     "data": "Adverse Consq.", 
     "attr": { 
      "id": "37", 
      "class": "jstree-unchecked" 
     } 
     } 
    ] 
    }, 
    { 
    "data": "Personnel", 
    "attr": { 
     "id": "38", 
     "class": "jstree-checked", 
     "href": "ProtocolPersonnel.aspx" 
    } 
    }, 
    { 
    "data": "Database Searches", 
    "attr": { 
     "id": "40", 
     "class": "jstree-unchecked" 
    } 
    }, 
    { 
    "data": "Error Verification", 
    "attr": { 
     "id": "42", 
     "class": "jstree-unchecked" 
    } 
    }, 
    { 
    "data": "Preview", 
    "attr": { 
     "id": "44", 
     "class": "jstree-unchecked" 
    } 
    }, 
    { 
    "data": "Cancel Un-finished Protocol", 
    "attr": { 
     "id": "46", 
     "class": "jstree-unchecked" 
    } 
    } 
] 

回答

1

在JSON數據,而不是類= 「jstree覈對」,比如,你可以 「選中」 發送自定義屬性: 「真」 | 「檢查」:「錯誤」。

然後在ajax請求中添加一個完成回調,在此檢查所有已將checked屬性設置爲「true」的元素。

 
"json_data": { 
    "ajax": { 
     "url": "http://localhost/eSiriusAjax/jqTOC.wc", 
      "aync": false, 
      "data": function (n) { 
        return { id: n.attr ? n.attr("id") : 0 }; 
      }, 
      "complete" : function() { 
        $('#idTOC li[checked=true]').each(function() { 
         $.jstree._reference('#idTOC').check_node(this); 
       });   
      } 
     } 
} 
0

這是對我的作品 -

.bind("load_node.jstree", function (event, data) { 

       var productId = $("#Product_ProductId").val(); 
       if (!productId || productId < 1) { 
        data.inst.hide_checkboxes(); 
       } else 
        data.inst.change_state('li[selected=selected]', false); 
      }) 

JSON數據 -

[{"data":{"title":"Electronics","icon":null},"metadata":{"id":3,"level":1,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"Folder Name","icon":null},"metadata":{"id":28,"level":2,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[{"data":{"title":"New Category","icon":null},"metadata":{"id":29,"level":3,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]}]},{"data":{"title":"Folder Name","icon":null},"metadata":{"id":30,"level":2,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"File Name","icon":null},"metadata":{"id":31,"level":3,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"File Name","icon":null},"metadata":{"id":32,"level":4,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]}]},{"data":{"title":"Folder Name","icon":null},"metadata":{"id":33,"level":3,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"New Category","icon":null},"metadata":{"id":34,"level":4,"leaf":true,"mapId":13},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":true},"children":[]}]}]}]},{"data":{"title":"Furniture","icon":null},"metadata":{"id":4,"level":1,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"Folder Name","icon":null},"metadata":{"id":20,"level":2,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]},{"data":{"title":"File Name","icon":null},"metadata":{"id":22,"level":2,"leaf":true,"mapId":14},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":true},"children":[]},{"data":{"title":"File Name","icon":null},"metadata":{"id":25,"level":2,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]},{"data":{"title":"Folder Name","icon":null},"metadata":{"id":26,"level":2,"leaf":true,"mapId":12},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":true},"children":[]}]}] 

添加一個 '選擇' 屬性爲 'ATTR' 對象