2016-06-14 28 views
0

我想有一條規定,這使得用戶僅選擇屬於特定的樹節點特定的樹(水平)的節點;用戶必須沒有possibily選擇不同級別的多個節點是這樣的:如何選擇屬於使用JSTREE

the problem

你有任何想法如何做到這一點?

在此先感謝。

+0

這樣做的方式是(不知道是否有更簡單的方法)來偵聽select_node事件,檢查如果選擇的節點具有相同的父與其他選擇的節點......如果我贏得成功,我會發布答案 – SpinaloS

回答

0

我發現,如果有人也面臨同樣的問題,解決方法:

HTML部分:

<div id="jstree"/> 

JavaScript部分:

$(function() {$("#jstree").on("select_node.jstree", function (evt, data) { 

    var selectedNode= $('#jstree').jstree(true).get_selected(true); 

    for(var i = 0, j = selectedNode.length; i < j; i++) { 

    if(selectedNode.length > 1){ 

     var res = selectedNode[i].parents[selectedNode[i].parents.length-2]; 

     if(data.node.parent != res){ 
      $('#jstree').jstree("deselect_all"); 
     } 

    } 

    } 

}); 

$('#jstree').jstree({ 
    'core': { 

     'data': [{ 
      "id": "1.0", 
      "text": "Fresh Products", 
      "icon": "", 
      "state": { 
       "opened": false, 
       "disabled": false, 
       "selected": false 
      }, 
      "children": [{ 
       "id": "2.06.0", 
       "text": "Ethnic & Specialty", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": [{ 
       "id": "2.16.0", 
       "text": "Ethnic & Specialty", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }, { 
       "id": "2.17.0", 
       "text": "Natural & Organic", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }], 
       "liAttributes": null, 
       "aAttributes": null 
      }, { 
       "id": "2.07.0", 
       "text": "Natural & Organic", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }, { 
       "id": "2.08.0", 
       "text": "Prepared Foods", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }, { 
       "id": "2.09.0", 
       "text": "Seafood", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }, { 
       "id": "2.010.0", 
       "text": "Seafood", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }], 
      "liAttributes": null, 
      "aAttributes": null 
     }, { 
      "id": "2.0", 
      "text": "Frozen Products", 
      "icon": "", 
      "state": { 
       "opened": false, 
       "disabled": false, 
       "selected": false 
      }, 
      "children": [], 
      "liAttributes": null, 
      "aAttributes": null 
     }, { 
      "id": "3.0", 
      "text": "Store Equipment ", 
      "icon": "", 
      "state": { 
       "opened": false, 
       "disabled": false, 
       "selected": false 
      }, 
      "children": [], 
      "liAttributes": null, 
      "aAttributes": null 
     }, { 
      "id": "4.0", 
      "text": "Packaged Grocery", 
      "icon": "", 
      "state": { 
       "opened": false, 
       "disabled": false, 
       "selected": false 
      }, 
      "children": [], 
      "liAttributes": null, 
      "aAttributes": null 
     }, { 
      "id": "5.0", 
      "text": "Retail Technology", 
      "icon": "", 
      "state": { 
       "opened": false, 
       "disabled": false, 
       "selected": false 
      }, 
      "children": [], 
      "liAttributes": null, 
      "aAttributes": null 
     }, { 
      "id": "6.0", 
      "text": "HBC/Non-Foods", 
      "icon": "", 
      "state": { 
       "opened": false, 
       "disabled": false, 
       "selected": false 
      }, 
      "children": [{ 
       "id": "2.090.0", 
       "text": "Seafood", 
       "icon": "", 
       "state": { 
        "opened": false, 
        "disabled": false, 
        "selected": false 
       }, 
       "children": false, 
       "liAttributes": null, 
       "aAttributes": null 
      }], 
      "liAttributes": null, 
      "aAttributes": null 
     }] 



    }, 
    "search": { 

     "case_insensitive": true, 
     "show_only_matches" : true 


    }, 

    "plugins": ["search"] 


}); 

});