2017-07-28 127 views
1

我在boostrap-treeview.js:267上檢索到我的boostrap-treeview的JSON數據時出現此錯誤。Uncaught TypeError:無法設置未定義的屬性'nodeId'

$(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" 
         } 
        ] 
     } 
} 

我使用Bootstrap Tree View

任何想法

+1

的可能的複製[JavaScript的 - 不能設置的未定義的屬性(https://stackoverflow.com/questions/7479520/javascript-cannot-set-property-of-undefined) –

+1

你的JSON似乎錯誤檢查指定的JSON格式[tree-view](https://github.com/jonmiles/bootstrap-treeview) –

回答

1

您的JSON是一個對象。您必須將其轉換爲數組才能工作。你可以改變你的PHP返回一個適當的數組或使用JavaScript將其轉換爲數組,如下所示。

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>

+0

非常感謝。有效!!! –

+0

@RaffaeleColleo不客氣!請接受答案,如果它幫助:) – H77

相關問題