js 自動產生下拉樹

來源:互聯網
上載者:User

標籤:function   元素   

toTree:function(treeDatas) {     var that = this;     var rs = []; for(var i=0; i<treeDatas.length; i++) {var pid = -1;if(treeDatas[i].hasOwnProperty("pid")){pid = treeDatas[i].pid;}rs.push({id: treeDatas[i].id, name: treeDatas[i].subjectName, pid: pid, code: treeDatas[i].subjectNo}); }      //擷取容器物件      var selectbox = document.getElementById("selectbox");      var tree = that.listToTree(rs,-1);//調用函數,傳入要轉換的list數組,和樹中頂級元素的pid      //調用函數,並將結構出入到下拉框容器中      selectbox.innerHTML = that.creatSelectTree(tree);     },     listToTree:function(list,pid) {     var that = this;     var ret = [];//一個存放結果的臨時數組     for(var i in list) {     if(list[i].pid == pid) {//如果當前項的父id等於要尋找的父id,進行遞迴     list[i].children = that.listToTree(list, list[i].id);     ret.push(list[i]);//把當前項儲存到臨時數組中     }     }     return ret;//遞迴結束後返回結果     },     preFlag:"+",//首碼符號,用於顯示父子關係,這裡可以使用其它符號     creatSelectTree:function(d){     var that = this;      var option="";     for(var i=0;i<d.length;i++){     if(d[i].children.length){//如果有子集     option += "<option value=‘" + d[i].id + "‘>" + that.preFlag + d[i].name + "</option>";     that.preFlag += "-";//首碼符號加一個符號          option += that.creatSelectTree(d[i].children);//遞迴調用子集          that.preFlag = that.preFlag.slice(0,that.preFlag.length - 1);//每次遞迴結束返回上級時,首碼符號需要減一個符號      }else{//沒有子集直接顯示      option += "<option value=‘"+d[i].id+"‘>" + that.preFlag + d[i].name + "</option>";     }      }      return option;//返回最終html結果     }


js 自動產生下拉樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.