Examples of javascript scopes and javascript instances
A tree directory needs to be generated based on JSON data during the recent project process. The result code is as follows:
Var folderList = [{"FolderName": "ASD", "ChildList": null}, {"FolderName": "intranet announcement", "ChildList": null }, {"FolderName": "Test Document", "ChildList": null },{ "FolderName": "Software Development", "ChildList": null },{ "FolderName ": "Test", "ChildList": [{"FolderName": "Test2", "ChildList": [{"FolderName": "Test3", "ChildList ": null}] },{ "FolderName": "individual", "ChildList": null },{ "FolderName": "company notification", "ChildList": null }, {"FolderName": "procurement platform", "ChildList": null}]; var str = ''; function generateFolders (arr) {if (arr. length> 0) {str + = '<div class = "sui-list-icon ubt bc-gra1">'; for (var I = 0, len = arr. length; I <len; I ++) {str + = '<ul class = "ub-ac">' + '<li class = "sui-list-licon sui-icon-OK-circle fts2">' + '<ul class = "ub ub-f1 sui-list-item">' + '<li class = "ub-f1 sui-list-text sui-list -nowrap "> '+ arr [I]. folderName + '</li>' + '<li class = "sui-list-ricon ub-img sui-icon-chevron-right fts2"> </li>' +' </ul> '+' </ul> '; if (isDefine (arr [I]. childList) {str + = generateFolders (arr [I]. childList) ;}}str + = '</div>'; return str ;}else {return '';}} var folderTxt = '<div class = "sui-list-icon ubt bc-gra1">' + '<ul class = "ub-ac">' + '<li class = "sui-list-licon sui-icon-drawer fts2"> '+' </li> '+' <ul class = "ub ub-f1 sui-list-item"> '+' <li class = "ub-f1 sui-list-text sui-list-nowrap"> my directory </li> '+' <li class = "sui-list- ricon ub-img sui-icon-chevron-right fts2 "> </li> '+' </ul> '; folderTxt + = generateFolders (folderList); folderTxt + = '</div>'; Folder ('folder list'folder .html (folderTxt ); /*** determine whether it is null * @ param value */function isDefine (value) {if (value = null | value = "" | value = "undefined" | value = undefined | value = "null" | value = = "(null) "| value = 'null' | typeof (value) = 'undefined') {return false;} else {value = value +" "; value = value. replace (/\ s/g, ""); if (value = "") {return false;} return true ;}}
The resulting tree is as follows:
After investigation, it is found that the location defined by str is incorrect. You can define str as a local variable.
function generateFolders(arr) { var str=''; if (arr.length > 0) { str += '<div class="sui-list sui-list-icon ubt bc-gra1">'; for (var i = 0, len = arr.length; i < len; i++) { str += '<ul class="ub ub-ac">' + '<li class="sui-list-licon sui-icon-ok-circle fts2"></li>' + '<ul class="ub ub-f1 sui-list-item">' + '<li class="ub-f1 sui-list-text sui-list-nowrap">' + arr[i].FolderName + '</li>' + '<li class="sui-list-ricon ub-img sui-icon-chevron-right fts2"></li>' + '</ul>' + '</ul>'; if (isDefine(arr[i].ChildList)) { str += generateFolders(arr[i].ChildList); } } str += '</div>'; return str; }else{ return ''; } }
After modification, you can achieve the following results:
The above is all the content of this article. I hope you will like it.