2016-06-07 46 views
0

我试图将节点添加到所需文件夹,但不幸没有成功。这个想法很简单。通过点击按钮,用户可以看到模式,其中放置了一些参数,例如:节点的名称和描述,该值将从http请求中动态获得,并且他可以选择要保存节点的文件夹,但出于某种原因节点出现在每个父节点中文件夹,我找不到我的错误在哪里? 我转载plunker与我的问题将新节点添加到所需文件夹

我的代码:

(function() { 

    app.controller('HomeCtrl', ["$scope", "$rootScope", "$http", '$interval', '$q', "$log", "$routeParams", "$location", "$uibModal", 
    function($scope, $rootScope, $http, $interval, $q, $log, $routeParams, $location, $uibModal) { 


     $scope.tree_core = { 
     multiple: false, // disable multiple node selection 
     check_callback: function(operation, node, node_parent, node_position, more) { 
      // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node' 
      // in case of 'rename_node' node_position is filled with the new node name 
      if (operation === 'move_node') { 
      return false; // disallow all dnd operations 
      } 
      return true; // allow all other operations 
     } 
     }; 

     $scope.contextMenu = custom.contextMenu; 

     // *** Begin Callback Functions for Tree *** 
     // Describe callback function that to all necessary variables related with Jstree 

     $scope.callback = function(e, data) { 
     // ===== Click event on node for Industry ===== 
     for (var i = 0; i < data.selected.length; i++) { 
      var parentNodeId = data.instance.get_node(data.selected[i]).parent; 
      var parentNodeText = data.instance.get_node(parentNodeId).text; 

      $scope.node = data.instance.get_node(data.selected[i]).text; 
      $scope.path = data.instance.get_path(data.node, '/'); 

     } 
     }; 

     // Declared initial folder 

     $scope.filters = custom.sharedFilter; 

     $scope.open = function() { 

     var modalInstance = $uibModal.open({ 
      animation: $scope.animationsEnabled, 
      templateUrl: 'modal.html', 
      controller: 'EventFilterCtrl', 
      size: 'md', 
      resolve: { 
      items: function() { 
       return $scope.filters; 
      } 
      } 
     }); 

     modalInstance.result.then(function(selectedItem) { 
      $scope.selected = selectedItem; 
     }, function() { 
      $log.info('Modal dismissed at: ' + new Date()); 
     }); 
     }; 

     // Storing variable in localstorage later will moved to DB 
     // Data based on ui-modal and ui-grid callback functions 

     $rootScope.saveFilter = function() { 
     var $saveForm = $('#filter-save-form'); 
     var filterName = $saveForm.find('#filter-save-name').val(); 
     var filterDescription = $saveForm.find('#filter-save-description').val(); 

     $scope.item = { 
      "text": filterName, 
      "description": filterDescription, 
      "value": Date.now(), 
     }; 

     var i = 0; 
     for (i; i < $scope.filters.length; i++) { 
      if ($scope.filters[i]) { 
      console.log("pass"); 
      $scope.filters[i].children.push($scope.item) 
      } 
     } 

     }; 
     // ==== End Build Left Bar ===== 

    } 
    ]); 

}()); 

回答

1

您在保存功能是错误的条件:

if ($scope.filters[i]) { 
     console.log("pass"); 
     $scope.filters[i].children.push($scope.item) 
     } 

$ scope.filters [I]总是返回true。您必须实现一个正确的条件在正确的文件夹中添加新项

我叉你plunker并纠正它来获得模态选择的节点,并把它保存功能:https://plnkr.co/edit/lReRGfPUkYAeRjs9K2Sv?p=preview

+0

哇作品像预期的,谢谢你的好男人!它只说了一件事不能读取未定义的属性“文本”,但我可以使用它 – Anton