我最近遇到的问题如下: 我需要在java / javascript中创建一个json树,其中json树的数据是从MySql Database获得的。 在上面我找不到文件 所以请帮我做上面的事情。 感谢您的帮助。
答案 0 :(得分:0)
我认为在java中获取树对象是不合适的。我尝试过这个。你可以用另一种方式。单击文件夹时,您将获得文件夹文件或文件夹。 在这里我使用Dropbox Api来做到这一点。请从中得到一个想法。试试你的想法。
<!-- Nested list template -->
<script type="text/ng-template" id="items_renderer.html">
<div ui-tree-handle>
<span ng-show="item.file" class="glyphicon glyphicon-folder-close"></span>
<a ng-hide="item.file" class="btn btn-success btn-xs" data-nodrag ng-click="toggle(this);toggleMe(this)">
<span class="glyphicon glyphicon-folder-close" ng-class="{'glyphicon-chevron-right': collapsed, 'glyphicon-chevron-down': !collapsed}">
</span></a>
{{item.title}}
<a class="pull-right btn btn-danger btn-xs" data-nodrag ng-click="remove(this)"><span class="glyphicon glyphicon-remove"></span></a>
<a class="pull-right btn btn-primary btn-xs" data-nodrag ng-click="newSubItem(this)" style="margin-right: 8px;"><span class="glyphicon glyphicon-plus"></span></a>
</div>
<ol ui-tree-nodes="options" ng-model="item.items" ng-class="{hidden: collapsed}">
<li ng-repeat="item in item.items" collapsed="true" ui-tree-node ng-include="'items_renderer.html'">
</li>
</ol>
</script>
<div ui-tree="options">
<ol ui-tree-nodes ng-model="list" >
<li ng-repeat="item in list track by $index" ui-tree-node collapsed="true" ng-include="'items_renderer.html'"></li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
app.controller('treeController',['$scope', '$http','fileUpload', function($scope, $http,fileUpload) {
console.log("$$$ tree controller has been initialized $$$")
/** To get list of files from the dropbox */
var files = [];
$scope.list = [];
var foldername = '';
/*** we call dropbox cloud when the user wants and we get files and
* folders for an initialization*/
$scope.getListOfFiles = function() {
var foldername = "/";
$http.get("http://localhost:8080/dropbox_api_.10/dropbox/getListOfFiles/?folderPath=" + foldername)
.success(function(data, status) {
console.log("List of files from dropbox folder &&&&", angular.toJson(data));
$scope.list = data;
}).error(function(data, status, headers, config) {
alert('error');
});
}
var folders = [];
var buildFloderPath = function(scope) {
if (scope.$parentNodeScope != null) {
folders.push(scope.$parentNodeScope.$modelValue.title);
buildFloderPath(scope.$parentNodeScope);
}
};
/** When we call, we expand tree here and clear when collapse tree*/
$scope.toggleMe = function(scope) {
folders = [];
foldername="";
if (!scope.collapsed) {
var nodeData = scope.$modelValue;
folders.push(nodeData.title);
buildFloderPath(scope);
console.log(angular.toJson(folders));
for (var i = folders.length - 1; i >= 0; i--) {
foldername += "/" + folders[i];
}
/***/
//continueFileUploading(foldername);
$http.get("http://localhost:8080/dropbox_api_.10/dropbox/getListOfFiles/?folderPath=" + foldername)
.success(function(data, status) {
console.log(" @@@@ Selected path @@@",foldername);
console.log("List of files from dropbox folder &&&&", angular.toJson(data));
for (var i = 0; i < data.length; i++) {
nodeData.items.push(data[i]);
}
}).error(function(data, status, headers, config) {
alert('error');
});
}
else{
var nodeData = scope.$modelValue;
nodeData.items = [];
}
};
$scope.getListOfFiles();
/*****************/
$scope.remove = function(scope) {
scope.remove();
};
$scope.toggle = function(scope) {
scope.toggle();
};
$scope.newSubItem = function(scope) {
var nodeData = scope.$modelValue;
nodeData.items.push({
id : nodeData.id * 10 + nodeData.items.length,
title : nodeData.title + '.' + (nodeData.items.length + 1),
items : []
});
};
}]);
public ArrayList<String> listDropboxFolders(@RequestParam("folderPath") String folderPath) throws DbxException {
ArrayList<String> fileList=new ArrayList<String>();
for (DbxEntry child : listing.children) {
//File child;
if(child.isFolder()){
fileList.add(child.name);
System.out.println("file is theere"+child.name);
}else{
System.out.println("file name **"+child.name);
}
child.toString());
}
return fileList;
}
答案 1 :(得分:0)
您需要一种服务器端语言才能连接到MySql数据库并将数据输出到客户端。
基本示例:
PHP(getJSON.php)
declare var h337: any;
的Javascript / jQuery的:
$db = new mysqli('localhost', 'user', 'pass', 'demo');
$strSQL = mysqli_query("SELECT ...");
$result = $db->query($sql)
$rows = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
}
$db->close()
print json_encode($rows);
代码未经过测试,但这基本上是:)