以角度方式在子子阵列中向上和向下移动项目

时间:2014-09-08 21:04:25

标签: javascript arrays angularjs

我正在尝试设置函数以允许用户通过索引向上或向下移动作用域中的项目。我没有问题,如果它在json中是1级,我附加到范围,但当它是三级时,我的代码似乎表现得非常奇怪。

以下是控制器中逻辑的第一级向下(这很好)

    var move = function (origin, destination, parent) {
            var temp = $scope.dataBlocks.selections[parent].types[destination];
            $scope.dataBlocks.selections[parent].types[destination] = $scope.dataBlocks.selections[parent].types[origin];
            $scope.dataBlocks.selections[parent].types[origin] = temp;
        };
    $scope.blockMoveUp = function(index, parent) {
        move(index, index - 1, parent);
    };

    $scope.blockMoveDown = function(index, parent) {
        move(index, index + 1, parent);
    };

其中click函数只传递项目自己的索引,每个按钮上的ng-click =“blockMoveUp($ index,$ parent。$ index)”的父索引。再次,这很好,但我试图将这个逻辑更深层次地应用到1级。它内部有一些块,就像子区域类型场景中的子数组一样,我也希望用户能够四处移动。

这是我的尝试:

var moveInner = function (origin, destination, parent, granp) {
        var temp = $scope.dataBlocks.selections[granp].types[parent].blocks[destination];
        $scope.dataBlocks.selections[granp].types[parent].blocks[destination] = $scope.dataBlocks.selections[granp].types[parent].blocks[origin];
        $scope.dataBlocks.selections[granp].types[parent].blocks[origin] = temp;
    };

    $scope.blockMoveUpSmall = function(index, parent, granp) {

        moveInner(index, index - 1, parent, granp);
    };

    $scope.blockMoveDownSmall = function(index, parent, granp) {
        moveInner(index, index + 1, parent, granp);
    };

每次ng重复使用ng init存储变量的索引,因此外部最重复的重复看起来像

<div class="subformWrappers" ng-repeat="selection in dataBlocks.selections" ng-init="selectionIndex = $index">

和下一个级别

    <div class="saPromptType" ng-repeat="type in selection.types" ng-init="typeIndex = $index">

和第三级

<div class="saPromptTypeSub blocksHere"  ng-repeat="block in type.blocks"ng-init="blockIndex = $index">

所以移动功能看起来像

ng-click="blockMoveUpSmall($index, typeIndex, selectionIndex)"

它以非常奇怪的方式移动物品。我找不到移动的任何模式,但似乎第一次点击(向上或向下)似乎工作正常,然后在它之后它完全没有了。我想也许它可能与存储在inits中的变量有关?如果我无法存储init变量,我可以做一个$ parent。$ parent。$ index也许可以解决这个问题?

我对这个问题感到困惑,我认为一旦我可以引用父项目,那将是非常直接的。任何/所有帮助将不胜感激。感谢您的阅读!!

0 个答案:

没有答案
相关问题