单击按钮排列列表列表 Angularjs

Arrange lists of lists on button click Angularjs

我有数据,两个带按钮的输入字段,单击按钮我想将数据移动到特定位置:

两个输入字段 = 源和目标它基本上是将某个项目从源移动到目标的索引

数据=

        {
      "0": [
        {
          "key": "Survey Meta Data"
        }
      ],
      "1": [
        {
          "key": "New Section"
        }
      ],
      "2": [
        {
          "key": "Tax Authority"
        }
      ]
    }

解释我想要什么 输入字段 source=2destination=0 现在我的数据将是

    {
  "0": [
    {
      "key": "Tax Authority"
    }
  ],
  "1": [
    {
      "key": "Survey Meta Data"
    }
  ],
  "2": [
    {
      "key": "New Section"
    }
  ]
}

因为它被移动到第一个索引并且其他项目被推入 任何帮助将不胜感激

index.html

Source=<input  ng-model="source" type="number>
Destination<input ng-model="destination" type="number>
<button ng-click="arrange(source,destination)></button>
{{model|json}}

index.js

$scope.model=[[{"key":'Survey Meta data'}],[{key:'New Section'}],[{key:Tax Authority'}]]

$scope.arrange(src,dest)
{
//trick to push
}

您只需使用 splice 即可。

JS:

   $scope.myObj = {
    "0": [{
        "key": "Survey Meta Data"
    }],
    "1": [{
        key: 'New Section'
    }],
    "2": [{
        "key": "Tax Authority"
    }]
};
$scope.model = [];
for (var i in $scope.myObj) {
    if ($scope.myObj.hasOwnProperty(i)) {
        $scope.model.push($scope.myObj[i]);
    }
}
Array.prototype.insert = function(index, item) {
    this.splice(index, 0, item);
};
$scope.arrange = function(src, dest) {
    var temp = [];
    temp = $scope.model.splice(parseInt(src), 1);
    $scope.model.insert(parseInt(dest), temp[0]);
    for (var i = 0; i < $scope.model.length; i++) {
        $scope.myObj[i] = $scope.model[i];
    }
}

演示http://jsfiddle.net/yj9sswq1/5/