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

    }
  ]);

}());
4

1 回答 1

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

于 2016-06-08T06:25:29.750 回答