Javascript 使用数据库创建动态json树
我最近遇到一个问题,问题如下: 我需要在java/javascript中创建一个json树,其中json树的数据是从MySql数据库获取的。 在上面我找不到文件 所以请帮我做上面的。 提前感谢您的帮助。 我认为在java中获取树对象是很困难的。我试过了。你可以用另一种方法。单击文件夹时,您将进入文件夹文件或文件夹在这里我使用dropbox Api来实现它。请从中得到一个想法。试试你的想法。 treeview.html:Javascript 使用数据库创建动态json树,javascript,java,json,dynamic,Javascript,Java,Json,Dynamic,我最近遇到一个问题,问题如下: 我需要在java/javascript中创建一个json树,其中json树的数据是从MySql数据库获取的。 在上面我找不到文件 所以请帮我做上面的。 提前感谢您的帮助。 我认为在java中获取树对象是很困难的。我试过了。你可以用另一种方法。单击文件夹时,您将进入文件夹文件或文件夹在这里我使用dropbox Api来实现它。请从中得到一个想法。试试你的想法。 treeview.html: {{item.title} treecontroller.js:
{{item.title}
treecontroller.js:
app.controller('treeController',['$scope','$http','fileUpload',函数($scope,$http,fileUpload){
console.log(“$$$树控制器已初始化$$$”)
/**从dropbox获取文件列表的步骤*/
var文件=[];
$scope.list=[];
var foldername='';
/***当用户需要时,我们调用dropbox cloud,并获取文件和数据
*用于初始化的文件夹*/
$scope.getListOfFiles=函数(){
var foldername=“/”;
$http.get(“http://localhost:8080/dropbox_api_.10/dropbox/getListOfFiles/?folderPath=“+foldername)
.成功(功能(数据、状态){
log(“dropbox文件夹中的文件列表&&&&&”,angular.toJson(数据));
$scope.list=数据;
}).error(函数(数据、状态、标题、配置){
警报(“错误”);
});
}
var文件夹=[];
var buildFloderPath=函数(范围){
if(作用域$parentNodeScope!=null){
folders.push(作用域$parentNodeScope.$modelValue.title);
buildFloderPath(范围$parentNodeScope);
}
};
/**当我们调用时,我们在这里展开树,并在折叠树时清除*/
$scope.toggleMe=函数(范围){
文件夹=[];
foldername=“”;
如果(!scope.collapsed){
var nodeData=范围$modelValue;
folders.push(nodeData.title);
buildFloderPath(范围);
log(angular.toJson(folders));
对于(var i=folders.length-1;i>=0;i--){
foldername+=“/”+文件夹[i];
}
/***/
//continueFileUploading(foldername);
$http.get(“http://localhost:8080/dropbox_api_.10/dropbox/getListOfFiles/?folderPath=“+foldername)
.成功(功能(数据、状态){
console.log(“@@@@Selected path@@”,foldername);
log(“dropbox文件夹中的文件列表&&&&&”,angular.toJson(数据));
对于(变量i=0;i
爪哇:
public ArrayList listDropboxFolders(@RequestParam(“folderPath”)字符串folderPath)抛出DbxException{
ArrayList fileList=新建ArrayList();
for(DbxEntry-child:listing.children){
//文件子项;
if(child.isFolder()){
fileList.add(child.name);
System.out.println(“文件是theere”+子文件名);
}否则{
System.out.println(“文件名**”+子文件名);
}
child.toString());
}
返回文件列表;
}
您需要一种服务器端语言来连接MySql数据库并将数据输出到客户端
基本示例:
PHP(getJSON.PHP)
Javascript/jQuery:
$.ajax({
url: 'http://{your-host}/getJSON.php',
dataType: 'JSON',
success: function(data, status) {
console.log(data)
},
error: function(e) {
console.log(e.statusText + " : " + e.responseText);
}
});
代码没有经过测试,但基本上就是这样:)@Sanjay Kumar N S你能帮我一下吗?我对java编码知之甚少。这种提问方式并不是在堆栈溢出中轻举妄动。您必须尝试一些方法,并将代码放在这里,以便其他人能够找出错误。@SanjayKumarNS我确实制作了一个json树,其中数据集是固定的,但当我必须动态生成数据时,真正的问题出现了,令人惊讶的是,没有代码或示例演示上述内容。所以我想最好是我问这个巨大的知识资源,这样不仅我而且其他人都能从中受益。。因此,如果你对此有任何想法,请告诉我其他人将如何发现错误。。。当我不知道如何使事情在第一时间以期望的方式运作时
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;
}
$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);
$.ajax({
url: 'http://{your-host}/getJSON.php',
dataType: 'JSON',
success: function(data, status) {
console.log(data)
},
error: function(e) {
console.log(e.statusText + " : " + e.responseText);
}
});