【问题标题】:dynamic json tree creation using a database使用数据库创建动态 json 树
【发布时间】:2016-02-22 17:09:44
【问题描述】:

我最近遇到了一个问题,如下: 我需要在 java/javascript 中创建一个 json 树,其中 json 树的数据是从 MySql 数据库中获取的。 在上面我找不到文件 所以请帮我做以上。 提前感谢您的帮助。

【问题讨论】:

  • @Sanjay Kumar N S 你能帮帮我吗
  • 我对 java 编码了解不多。这种提出问题的方式不会在堆栈溢出中进行romoting。您必须尝试一些事情并将代码放在这里,以便其他人可以找出错误。
  • @SanjayKumarNS 我确实制作了一个 json 树,其中数据集是固定的,但是当我必须使数据动态出现时,真正的问题出现了,令人惊讶的是,没有代码或示例来演示上述内容。所以我认为我最好询问这个庞大的知识资源,这样不仅我而且其他人也能从中受益。所以如果你对此有任何想法,请告诉我
  • 其他人如何发现错误......当我不知道如何让事情首先以所需的方式工作时

标签: javascript java json dynamic


【解决方案1】:

您将需要一种服务器端语言来连接到 MySql 数据库并将数据发送到客户端。

基本示例:

PHP (getJSON.php)

$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);

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);
   }
});

代码没有经过测试,但基本上就是这样:)

【讨论】:

    【解决方案2】:

    我认为在 java 中获取树对象很困难。我尝试过这个。你可以用另一种方式。单击文件夹时,您将进入文件夹文件或文件夹。 这里我使用了 dropbox Api 来实现。请从中得到一个想法。试试你的想法。

    treeview.html:

    <!-- 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>
    

    treecontroller.js:

    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 : []
        });
        };
    }]);
    

    java:

    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;    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-10-05
      • 2019-12-23
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      相关资源
      最近更新 更多