Examples of javascript scopes and javascript instances

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.