如何使用向上和向下键盘键上下拼接对象

时间:2017-06-05 19:43:54

标签: javascript angularjs json array-splice

我无法通过按向上和向下箭头键使用拼接从阵列中上下移动单个对象。

我有一组看起来像这样的对象:

$scope.peoples = [  
   {  
      "seqNo":0,
      "address":{  
        "street": "123 abc"
      },
      "id":11,
      "name":{  
         "first":"adf"
      }
   },
   {  
      "seqNo":1,
      "address":{  
        "street": "123 bbc"
      },
      "id":24,
      "name":{  
         "first":"adf"
      }
   },
   {  
      "seqNo":2,
      "address":{  
        "street": "123 cbs"
      },
      "id":14,
      "name":{  
         "first":"adf"
      }
   },
]

我的js:

      var moveUp = function(from, to) {
        $scope.peoples.splice(to, 0, $scope.peoples.splice(from, 1)[0]);
      };

      var moveDown = function(from, to) {
        $scope.peoples.splice(from, 0, $scope.peoples.splice(to, 1)[0]);
      };

      $scope.keyEvent = function (people) {
          $scope.keydownkeycode = event.keyCode;
          if (event.keyCode === 38) {
            moveUp(0, 1);
          } else if (event.keyCode === 40) {
            moveDown(0, 1);
          }
      };

目前它只会从阵列中上下移动第一个对象和第二个对象...不完全确定如何解决此问题。其他人以前做过这样的事吗?

编辑:

我的HTML:

<tr ng-keydown="keydownevt(people)" tabindex="0" ng-repeat="people in peoples">
        <td class="people-col">
          <div>{{people.name.first}}</div>
          <div>{{people.address.street}}</div>
          <div>{{people.id}}</div>
        </td>
      </tr>

0 个答案:

没有答案