2016-12-06 138 views
0

我有一個變量,我想將它傳遞給$('#tree').jstree()將JSON作爲變量傳遞給jsTree

嘗試:

$('#tree').jstree({ 
    plugins: ["checkbox", "types"], 
    "types": { 
     "file": { 
      "icon": "jstree-file" 
     } 
    }, 
    'core': { 
     'data': mdata.data 

    } 
}); 
//------------------------------ 
$('#tree').jstree({ 
    plugins: ["checkbox", "types"], 
    "types": { 
     "file": { 
      "icon": "jstree-file" 
     } 
    }, 
    'core': { 
     'data': JSON.stringify(mdata.data) 

    } 
}); 
//------------------------------ 
$('#tree').jstree(mdata.data) 

我的JSON變量(mdata.data)由JSON.parse()已經被解析。我已通過jsonlint.com驗證了它的有效性。那就是:

[{ 
    "soslist": [{ 
     "code_intext": "utf-8", 
     "count_of_pages": 2, 
     "count_of_records": 7, 
     "curobj": "1", 
     "obj": "1", 
     "page": 1 
    }, { 
     "code_intext": "utf-8", 
     "count_of_pages": 2, 
     "count_of_records": 7, 
     "curobj": "1", 
     "obj": "1", 
     "page": 1 
    }], 
    "system": [{ 
     "count_of_pages": 2, 
     "count_of_records": 7, 
     "curobj": "1", 
     "page": 1 
    }] 
}] 

mdata.data控制檯:

它甚至有可能還是需要jsTree具體JSON結構和類似的東西idparent_id

回答

0

jsTree需要特定的JSON結構。從他們documentation

// Expected format of the node (there are no required fields) 
{ 
    id   : "string" // will be autogenerated if omitted 
    text  : "string" // node text 
    icon  : "string" // string for custom 
    state  : { 
    opened : boolean // is the node open 
    disabled : boolean // is the node disabled 
    selected : boolean // is the node selected 
    }, 
    children : [] // array of strings or objects 
    li_attr  : {} // attributes for the generated LI node 
    a_attr  : {} // attributes for the generated A node 
} 

你的目標是在一個完全不同的格式,所以它不會工作。