樹形圖修改某個元素的值

s思考萬遍,不如動手寫一遍!bash

var data = processData = [{
				"id": 1360,
				"parentID": 0,
				"name": "頭排+頭排裏+後套裏+中底",
				"title": "貼前貼",
				"isCheck": true,
				"children": [{
					"id": 1353,
					"parentID": 1360,
					"name": "頭排裏",
					"title": "品檢品檢",
					"isCheck": true,
					"children": [{
						"id": 1352,
						"parentID": 1353,
						"name": "頭排裏",
						"title": "批皮批後套裏子跟",
						"isCheck": false,
						"children": [{
							"id": 1351,
							"parentID": 1352,
							"name": "頭排裏",
							"title": "下面皮下面料(2層)",
							"isCheck": false,
							"children": [{
								"id": 1296,
								"parentID": 1351,
								"name": "頭排裏",
								"title": "原材料",
								"isCheck": false,
								"children": []
							}]
						}]
					}]
				}]
			}]
			var arr=[];
			function tree(data){
				for(var i=0;i<data.length;i++){
					//console.log(data[i].isCheck);
					//console.log("1111:"+data[i].id);
					if(data[i].isCheck && data[i].children && data[i].children!=""){
						tree(data[i].children);
						arr.unshift(data[i].id);
						//console.log(data[i].id);
						//return arr ;
					}
				}
			}
		tree(data);
		console.log(arr[arr.length - 1]);
		function tree1(data,id){
			for(var i=0;i<data.length;i++){
				//console.log(id);
				console.log("111:"+data[i].title);
				console.log("111:"+data[i].isCheck);
				if(data[i].id == id){
					data[i].isCheck =false;
					console.log("222:"+data[i].title);
					console.log("222:"+data[i].isCheck);
				}else if(data[i].children && data[i].children!=""){
					tree1(data[i].children ,id);
				}
			}
		}
		tree1(data,arr[arr.length - 1]);

複製代碼
相關文章
相關標籤/搜索