未捕获的TypeError:无法设置未定义的属性"nodeId" [英] Uncaught TypeError: Cannot set property 'nodeId' of undefined

查看:75
本文介绍了未捕获的TypeError:无法设置未定义的属性"nodeId"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的boostrap-treeview检索JSON数据时,我在boostrap-treeview.js:267上收到此错误.

I'm getting this error on boostrap-treeview.js:267 when retrieving JSON data for my boostrap-treeview.

$(document).ready(function() {
    $.ajax({ 
        url: "js/category_data.php",
        method:"POST",
        dataType: "json",            
        success: function(data) {
            $('#doctypesTree').treeview({ data: data });
        }    
    });
});

JSON数据如下:

{
    "2":{
            "id":"12",
            "text":"Certified copy",
            "Description":"","root":"0"
        },
    "3":{
            "id":"13",
            "text":"Charter",
            "Description":"",
            "root":"0"
        },
    "4":{
            "id":"14",
            "text":"Codicil (will)",
            "Description":"",
            "root":"0"
        },
    "5":{
            "id":"15",
            "text":"Cohabitation agreement",
            "Description":"",
            "root":"0"
        },
    "6":{
            "id":"16",
            "text":"Collateral assurance",
            "Description":"",
            "root":"0"
        },
    "7":{
            "id":"17",
            "text":"Commercial invoice",
            "Description":"",
            "root":"0"
        },
    "8":{
            "id":"18",
            "text":"Complaint",
            "Description":"",
            "root":"0"
        },
    "9":{
            "id":"19",
            "text":"Conservation easement",
            "Description":"",
            "root":"0"
        },
    "10":{
            "id":"20",
            "text":"Consignee",
            "Description":"",
            "root":"0"
        },
    "11":{
            "id":"21",
            "text":"Consumer complaint",
            "Description":"",
            "root":"0"
        },
    "12":{
            "id":"22",
            "text":"Contract",
            "Description":"",
            "root":"0"
        },
    "13":{
            "id":"23",
            "text":"Contractual term",
            "Description":"",
            "root":"0"
        },
    "14":{
            "id":"24",
            "text":"Contractual terms in English law",
            "Description":"",
            "root":"0"
        },
    "15":{
            "id":"25",
            "text":"Florence Annie Conybeare",
            "Description":"",
            "root":"0"
        },
    "16":{
            "id":"26",
            "text":"Copyright transfer agreement",
            "Description":"",
            "root":"0"
        },
    "17":{
            "id":"27",
            "text":"Countersign (legal)",
            "Description":"",
            "root":"0"
        },
    "18":{
            "id":"28",
            "text":"County Court judgment",
            "Description":"",
            "root":"0"
        },
    "19":{
            "id":"29",
            "text":"Credit Support Annex",
            "Description":"",
            "root":"0"
        },
    "20":{
            "id":"30",
            "text":"Customs declaration",
            "Description":"",
            "root":"0"
        },
    "21":{
            "id":"31",
            "text":"Bills",
            "Description":"",
            "root":"0",
            "nodes":[
                        {
                            "id":"4",
                            "text":"Bill of costs",
                            "Description":"This type gather all documents related to costs","root":"31"
                        },
                        {
                            "id":"5",
                            "text":"Bill of sale",
                            "Description":"","root":"31"
                        }
                    ]
        } 
}

我正在使用引导树视图

任何想法

推荐答案

您的json是一个对象.您必须将其转换为数组才能使树起作用.您可以更改php以返回适当的数组,也可以使用javascript将其转换为数组,如下所示.

Your json is an object. You'll have to convert it to an array for the tree to work. You can either change your php to return a proper array or use javascript to convert it to an array as shown below.

var data = {
  "2": {
    "id": "12",
    "text": "Certified copy",
    "Description": "",
    "root": "0"
  },
  "3": {
    "id": "13",
    "text": "Charter",
    "Description": "",
    "root": "0"
  },
  "4": {
    "id": "14",
    "text": "Codicil (will)",
    "Description": "",
    "root": "0"
  },
  "5": {
    "id": "15",
    "text": "Cohabitation agreement",
    "Description": "",
    "root": "0"
  },
  "6": {
    "id": "16",
    "text": "Collateral assurance",
    "Description": "",
    "root": "0"
  },
  "7": {
    "id": "17",
    "text": "Commercial invoice",
    "Description": "",
    "root": "0"
  },
  "8": {
    "id": "18",
    "text": "Complaint",
    "Description": "",
    "root": "0"
  },
  "9": {
    "id": "19",
    "text": "Conservation easement",
    "Description": "",
    "root": "0"
  },
  "10": {
    "id": "20",
    "text": "Consignee",
    "Description": "",
    "root": "0"
  },
  "11": {
    "id": "21",
    "text": "Consumer complaint",
    "Description": "",
    "root": "0"
  },
  "12": {
    "id": "22",
    "text": "Contract",
    "Description": "",
    "root": "0"
  },
  "13": {
    "id": "23",
    "text": "Contractual term",
    "Description": "",
    "root": "0"
  },
  "14": {
    "id": "24",
    "text": "Contractual terms in English law",
    "Description": "",
    "root": "0"
  },
  "15": {
    "id": "25",
    "text": "Florence Annie Conybeare",
    "Description": "",
    "root": "0"
  },
  "16": {
    "id": "26",
    "text": "Copyright transfer agreement",
    "Description": "",
    "root": "0"
  },
  "17": {
    "id": "27",
    "text": "Countersign (legal)",
    "Description": "",
    "root": "0"
  },
  "18": {
    "id": "28",
    "text": "County Court judgment",
    "Description": "",
    "root": "0"
  },
  "19": {
    "id": "29",
    "text": "Credit Support Annex",
    "Description": "",
    "root": "0"
  },
  "20": {
    "id": "30",
    "text": "Customs declaration",
    "Description": "",
    "root": "0"
  },
  "21": {
    "id": "31",
    "text": "Bills",
    "Description": "",
    "root": "0",
    "nodes": [{
      "id": "4",
      "text": "Bill of costs",
      "Description": "This type gather all documents related to costs",
      "root": "31"
    }, {
      "id": "5",
      "text": "Bill of sale",
      "Description": "",
      "root": "31"
    }]
  }
};

var dataArray = [];
for (var key in data) {
    if (data.hasOwnProperty(key)) {
        dataArray.push(data[key]);
    }
};

$('#tree').treeview({data: dataArray});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/jonmiles/bootstrap-treeview/master/dist/bootstrap-treeview.min.js"></script>
<div id="tree"></div>

这篇关于未捕获的TypeError:无法设置未定义的属性"nodeId"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆