I need help with organizing my folder structure in an html select using angular.
Currently, the select only displays the top level of folders. Is there a way to show all folders with proper indentation to reflect the structure?
I am considering adding a depth property to each folder to assist with the indentation. Since the folders are user-defined, I do not know the exact number of levels without examining the data.
Thank you
var app = angular.module("selectFolderApp", []);
app.controller("selectFolderController", ['$http', '$scope',
function($http, $scope) {
var ctrl = this;
ctrl.folders = [{
"name": "Housing",
"id": "1",
"children": [ {
"name": "Datasheets",
"id": "2",
"children": [ {
"name": "Folder 1",
"id": "3",
"children": []
}, {
"name": "Folder 2",
"id": "4",
"children": []
}
]
}, {
"name": "Charts",
"id": "5",
"children": [ {
"name": "Folder A",
"id": "6",
"children": []
}, {
"name": "Folder B",
"id": "7",
"children": []
}, {
"name": "Folder C",
"id": "8",
"children": [ {
"name": "Sub Folder 1",
"id": "9",
"children": []
}, {
"name": "Sub Folder 2",
"id": "10",
"children": []
}
]
}, {
"name": "Folder D",
"id": "11",
"children": []
}, {
"name": "Folder E",
"id": "12",
"children": []
}
]
}
]
}, {
"name": "Road Works",
"id": "13",
"children": [ {
"name": "Datasheets",
"id": "14",
"children": [ {
"name": "Folder 1",
"id": "15",
"children": [ {
"name": "Sub Folder 1",
"id": "16",
"children": []
}
]
}, {
"name": "Folder 2",
"id": "17",
"children": []
}, {
"name": "Folder 3",
"id": "18",
"children": []
}, {
"name": "Folder 4",
"id": "19",
"children": []
}
]
}, {
"name": "Charts",
"id": "20",
"children": [ {
"name": "Folder A",
"id": "21",
"children": []
}, {
"name": "Folder B",
"id": "22",
"children": []
}, {
"name": "Folder C",
"id": "23",
"children": []
}
]
}
]
}];
}
]);
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="selectFolderApp">
<div ng-controller="selectFolderController as ctrl">
<br>{{ ctrl.folderId }}
<br>
<br>
<select class="form-control" ng-model="ctrl.folderId" ng-options="item.id as item.name for item in ctrl.folders">
<option></option>
</select>
<br>
<script type="text/ng-template" id="folderTree">
{{item.name}}
<ul>
<li ng-repeat="item in item.children" ng-include="'folderTree'"></li>
</ul>
</script>
<ul>
<li ng-repeat="item in ctrl.folders" ng-include="'folderTree'"></li>
</ul>
</div>
</body>
</html>