JSTree不从JS变量渲染我的JSON数据

时间:2017-02-16 13:14:44

标签: javascript jquery json jstree

我有一个javascript JSON变量,我正在尝试将其提供给JSTree。它没有呈现任何东西。

{  
   "id":1,
   "data":"GDC",
   "depth":-1,
   "children":[  
      {  
         "id":2,
         "data":"SICS",
         "depth":0,
         "children":[  
            {  
               "id":5,
               "data":"Collaboration",
               "depth":1,
               "children":[  
                  {  
                     "id":10,
                     "data":"Contact Center",
                     "depth":2,
                     "children":[  
                        {  
                           "id":607,
                           "data":"Subscription",
                           "depth":3,
                           "children":[  
                              {  
                                 "depth":4,
                                 "id":608,
                                 "children":[  

                                 ],
                                 "data":"Optimization"
                              }
                           ]
                        },
                        {  
                           "id":606,
                           "data":"Transaction",
                           "depth":3,
                           "children":[  
                              {  
                                 "depth":4,
                                 "id":664,
                                 "children":[  

                                 ],
                                 "data":"Planning"
                              },

                              {  
                                 "depth":4,
                                 "id":672,
                                 "children":[],

                              }
                           ]
                        }
                     ]
                  },
                  {  
                     "id":11,
                     "data":"Productivity",
                     "depth":2,
                     "children":[  
                        {  
                           "id":1231,
                           "data":"DevOps",
                           "depth":3,
                           "children":[  
                              {  
                                 "depth":4,
                                 "id":1280,
                                 "children":[  

                                 ],
                                 "data":"Deployment"
                              }
                           ]
                        },
                        {  
                           "id":1229,
                           "data":"Tool Support",
                           "depth":3,
                           "children":[  
                              {  
                                 "depth":4,
                                 "id":1232,
                                 "children":[  

                                 ],
                                 "data":"UCDT"
                              },

                              {  
                                 "depth":4,
                                 "id":1237,
                                 "children":[  

                                 ],
                                 "data":"PCAT"
                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {  
               "id":3,
               "data":"Security ",
               "depth":1,
               "children":[  
                  {  
                     "id":284,
                     "data":"Security",
                     "depth":2,
                     "children":[  
                        {  
                           "id":1286,
                           "data":"Subscription",
                           "depth":3,
                           "children":[  
                              {  
                                 "depth":4,
                                 "id":1491,
                                 "children":[  

                                 ],
                                 "data":"Software Strategy"
                              },
                              {  
                                 "depth":4,
                                 "id":1496,
                                 "children":[  

                                 ],
                                 "data":"Change Support/Migration"
                              }
                           ]
                        },
                        {  
                           "id":1285,
                           "data":"Transaction",
                           "depth":3,
                           "children":[  
                              {  
                                 "depth":4,
                                 "id":1287,
                                 "children":[  

                                 ],
                                 "data":"CRD"
                              },
                              {  
                                 "depth":4,
                                 "id":1290,
                                 "children":[  

                                 ],
                                 "data":"NIP"
                              }

                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

我已将这些数据转换为JSTree,但它没有呈现任何内容。 我尝试过使用基本的JSON数据,但需要这样做。当我有嵌套的孩子时,它似乎失败了。

JS代码是:

$(function () {
$('#jstree').jstree({
    'json_data' : {
        'data' : data
    }
});
});

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

以下是您更新的json(将您的"数据"更改为" text"属性):

controller

以下是正确的js代码:

var data = {
        "id": 1,
        "text": "GDC",
        "depth": -1,
        "children": [
           {
               "id": 2,
               "text": "SICS",
               "depth": 0,
               "children": [
                  {
                      "id": 5,
                      "text": "Collaboration",
                      "depth": 1,
                      "children": [
                         {
                             "id": 10,
                             "text": "Contact Center",
                             "depth": 2,
                             "children": [
                                {
                                    "id": 607,
                                    "text": "Subscription",
                                    "depth": 3,
                                    "children": [
                                       {
                                           "depth": 4,
                                           "id": 608,
                                           "children": [

                                           ],
                                           "text": "Optimization"
                                       }
                                    ]
                                },
                                {
                                    "id": 606,
                                    "text": "Transaction",
                                    "depth": 3,
                                    "children": [
                                       {
                                           "depth": 4,
                                           "id": 664,
                                           "children": [

                                           ],
                                           "text": "Planning"
                                       },

                                       {
                                           "depth": 4,
                                           "id": 672,
                                           "children": [],

                                       }
                                    ]
                                }
                             ]
                         },
                         {
                             "id": 11,
                             "text": "Productivity",
                             "depth": 2,
                             "children": [
                                {
                                    "id": 1231,
                                    "text": "DevOps",
                                    "depth": 3,
                                    "children": [
                                       {
                                           "depth": 4,
                                           "id": 1280,
                                           "children": [

                                           ],
                                           "text": "Deployment"
                                       }
                                    ]
                                },
                                {
                                    "id": 1229,
                                    "text": "Tool Support",
                                    "depth": 3,
                                    "children": [
                                       {
                                           "depth": 4,
                                           "id": 1232,
                                           "children": [

                                           ],
                                           "text": "UCDT"
                                       },

                                       {
                                           "depth": 4,
                                           "id": 1237,
                                           "children": [

                                           ],
                                           "text": "PCAT"
                                       }
                                    ]
                                }
                             ]
                         }
                      ]
                  },
                  {
                      "id": 3,
                      "text": "Security ",
                      "depth": 1,
                      "children": [
                         {
                             "id": 284,
                             "text": "Security",
                             "depth": 2,
                             "children": [
                                {
                                    "id": 1286,
                                    "text": "Subscription",
                                    "depth": 3,
                                    "children": [
                                       {
                                           "depth": 4,
                                           "id": 1491,
                                           "children": [

                                           ],
                                           "text": "Software Strategy"
                                       },
                                       {
                                           "depth": 4,
                                           "id": 1496,
                                           "children": [

                                           ],
                                           "text": "Change Support/Migration"
                                       }
                                    ]
                                },
                                {
                                    "id": 1285,
                                    "text": "Transaction",
                                    "depth": 3,
                                    "children": [
                                       {
                                           "depth": 4,
                                           "id": 1287,
                                           "children": [

                                           ],
                                           "text": "CRD"
                                       },
                                       {
                                           "depth": 4,
                                           "id": 1290,
                                           "children": [

                                           ],
                                           "text": "NIP"
                                       }

                                    ]
                                }
                             ]
                         }
                      ]
                  }
               ]
           }
        ]
    }