Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript id键,然后将按项目跟踪.name更改为按项目跟踪.id,所有操作与以前一样_Javascript_Html_Angularjs_Select_Dropdownbox - Fatal编程技术网

Javascript id键,然后将按项目跟踪.name更改为按项目跟踪.id,所有操作与以前一样

Javascript id键,然后将按项目跟踪.name更改为按项目跟踪.id,所有操作与以前一样,javascript,html,angularjs,select,dropdownbox,Javascript,Html,Angularjs,Select,Dropdownbox,可能是orderBy:“name”正在导致issue@pankajparkar我们是否可以在不使用文档的情况下使用不同的方法来实现这一点。getElementById,您对此有何想法?此控制器代码是一种糟糕的角度练习。DOM操作应该由指令而不是控制器来完成。@tpie是的,你是对的……我正在考虑如何将代码简化为命令specific@tpie如果多个selectedits用于单个选择,则更难操作,但似乎不适用于多值选择重试-我已经解决了。这不是角度方式。这是角度方式:P,它使用与原始帖子相同的算法

可能是
orderBy:“name”
正在导致issue@pankajparkar我们是否可以在不使用
文档的情况下使用不同的方法来实现这一点。getElementById
,您对此有何想法?此控制器代码是一种糟糕的角度练习。DOM操作应该由指令而不是控制器来完成。@tpie是的,你是对的……我正在考虑如何将代码简化为命令specific@tpie如果多个selectedits用于单个选择,则更难操作,但似乎不适用于多值选择重试-我已经解决了。这不是角度方式。这是角度方式:P,它使用与原始帖子相同的算法解决OPs问题(顺便说一句,这很酷)。@pixelbits您的代码工作正常,但就像我在寻找一些基于角度的东西一样,我们的解决方案是有效的,但有一个问题是,如果我选择
Lenu
Ferix
,然后在它们到达顶部时单击向上按钮,将
Lenu
作为第一位置,将
Ferix
作为第二位置,再次,如果我们单击向上按钮,订单将更改为
Ferix
作为第一个位置,而
Lenu
作为第二个位置,同样,在初始位置,选择所有选项并单击向上按钮,您可以看到一些意外的订单发生是的,我也注意到了这一点。这是因为函数的工作方式,它获取任何选定项的位置,并递减索引。我还发现了一些奇怪的行为时,选择一个或两个以上的项目,并向下移动。正在解决这个问题,@tpie,我也尝试过解决这个问题,但是没有通过检查更新的代码。我消除了很多奇怪的行为…但是正在解决最后一个问题。明白了。现在应该没有bug了。
<div ng-app='myApp' ng-controller="ArrayController">
    <select id="select" size="9" ng-model="persons" ng-options="item as item.name for item in peoples | orderBy:'name'" multiple></select>
    <br/>
    <button ng-click="moveUp()">Up</button>
    <br/>
    <button ng-click="moveDown()">Down</button>
    <br/>
</div>
var app = angular.module('myApp', []);
app.controller('ArrayController', function ($scope) {
    $scope.peoples = [{
        name: 'Jacob'
    }, {
        name: 'Sunny'
    }, {
        name: 'Lenu'
    }, {
        name: 'Mathew'
    }, {
        name: 'Ferix'
    }, {
        name: 'Kitex'
    }];

    $scope.moveUp = function () {
        var select = document.getElementById("select");
        var i1=0, i2=1;
        while (i2 < select.options.length) {
            swapIf(select,i1++,i2++);
        }
    };

    $scope.moveDown = function () {
        var select = document.getElementById("select");
        var i1=select.options.length-1, i2=i1-1;
        while (i1 > 0) {
            swapIf(select,i1--,i2--);
        }
    };

    var swapVar = '';
    function swapIf(sel,i1,i2) {
        if ( ! select[i1].selected && select[i2].selected) {
            swapVar = select[i2].text;
            select[i2].text = select[i1].text;
            select[i1].text = swapVar;
            swapVar = select[i2].value;
            select[i2].value = select[i1].value;
            select[i1].value = swapVar;
            select[i1].selected = true;
            select[i2].selected = false;
        }
    }
});
function swapIf(sel,i1,i2) {
    if ( ! select[i1].selected && select[i2].selected) {

        var obj1 = $scope.peoples[i1];
        var obj2 = $scope.peoples[i2];
        $scope.peoples[i2] = obj1;
        $scope.peoples[i1] = obj2;
        select[i1].selected = true;
        select[i2].selected = false;
    }
}
   $scope.moveUp = function () {
        for(var i = 0; i < $scope.persons.length; i++) {
            var idx = $scope.peoples.indexOf($scope.persons[i])
            console.log(idx);
            if (idx > 0) {
                var itemToMove = $scope.peoples.splice(idx, 1)
                console.log(itemToMove[0])
                $scope.peoples.splice(idx-1, 0, itemToMove[0]);

            }
        }
    };
   $scope.moveDown = function () {
        for(var i = 0; i < $scope.persons.length; i++) {
            var idx = $scope.peoples.indexOf($scope.persons[i])
            console.log(idx);
            if (idx < $scope.peoples.length) {
                var itemToMove = $scope.peoples.splice(idx, 1)
                console.log(itemToMove[0])
                $scope.peoples.splice(idx+2, 0, itemToMove[0]);

            }
        }
    };    
$scope.moveUp = function () {
        var prevIdx = -1;
        var person = $scope.persons.concat();
        console.log($scope.persons);
        for(var i = 0; i < $scope.persons.length; i++) {
            var idx = $scope.peoples.indexOf($scope.persons[i])
            console.log(idx);
            if (idx-1 === prevIdx) {
                prevIdx = idx
            } else if (idx > 0) {
                var itemToMove = $scope.peoples.splice(idx, 1)
                console.log(itemToMove[0])
                $scope.peoples.splice(idx-1, 0, itemToMove[0]);

            }
        }
    };