在两个数组之间移动对象

时间:2014-01-21 04:36:15

标签: javascript arrays angularjs object ng-options

我有两个由包含对象的数组组成的列表。 我正在尝试将对象从一个列表移动到另一个列表,反之亦然。

控制器:

spApp.controller('userCtrl', 
    function userCtrl($scope,userService,groupService){

        //Generate list of all users on the SiteCollection
        $scope.users = userService.getUsers();

            //array of objects selected through the dom
        $scope.selectedAvailableGroups;
        $scope.selectedAssignedGroups;

            //array of objects the user actually belongs to
            $scope.availableGroups;
            $scope.assignedGroups;

        //Generate all groups on the site
        $scope.groups = groupService.getGroups();

        //Boolean used to disable add/remove buttons
        $scope.selectedUser = false;

            //Take the selectedAvailableGroups, add user to those groups
            //so push objects to "assignedGroups" array and remove from "avaiableGroups" array
        $scope.addUserToGroup = function (){
            userService.addUserToGroup($scope.selectedUser, $scope.selectedAvailableGroups, $scope.assignedGroups, $scope.availableGroups)
        };
    }
);

服务:

spApp.factory('userService', function(){
var addUserToGroup = function (selectedUser, selectedAvailableGroups, assignedGroups, availableGroups) {
    var addPromise = [];
    var selectLength = selectedAvailableGroups.length;

    //Add user to selected groups on server
    for (var i = 0; i < selectLength; i++) {
      addPromise[i] = $().SPServices({
        operation: "AddUserToGroup",
        groupName: selectedAvailableGroups[i].name,
        userLoginName: selectedUser.domain
      });      
    };

    //when all users added, update dom
    $.when.apply($,addPromise).done(function (){
      for (var i = 0; i < selectLength; i++) {
        assignedGroups.push(selectedAvailableGroups[i]);
        availableGroups.pop(selectedAvailableGroups[i]);
      };
      //alert(selectedUser.name + " added to: " + JSON.stringify(selectedAvailableGroups));  
    });
  }
}

对象:

[{ 
id: 85,
name: Dev,
Description:, 
owner: 70,
OwnerIsUser: True
 }]

HTML:

        <div>
            <label for="entityAvailable">Available Groups</label>
            <select id="entityAvailable" multiple   
                ng-model="selectedAvailableGroups" 
                ng-options="g.name for g in availableGroups | orderBy:'name'">
            </select>
        </div>
        <div id="moveButtons" >
            <button type="button" ng-disabled="!selectedUser" ng-click="addUserToGroup()">Add User</button>
            <button type="button" ng-disabled="!selectedUser" ng-click="removeUserFromGroup()">Remove</button>
        </div>
        <div>
            <label for="entityAssigned">Assigned Groups</label>
            <select id="entityAssigned" multiple
                ng-model="selectedAssignedGroups" 
                ng-options="g.name for g in assignedGroups | orderBy:'name'">                       
            </select>
        </div>

现在,推送到已分配的组可以正常工作,但只有在我点击其他内容或列表中时才会更新,而不是真正动态。但最大的问题是.pop(),我认为这不符合预期。

1 个答案:

答案 0 :(得分:1)

$.when.apply($,addPromise).done()似乎不是角度api或同步。所以angular不知道你的变化。您必须将代码包含在$scope.$apply调用中:

$scope.$apply(function(){
      for (var i = 0; i < selectLength; i++) {
        assignedGroups.push(selectedAvailableGroups[i]);
        availableGroups.pop(selectedAvailableGroups[i]);
      };
});

如果你点击某个内容,就会发生$ digest循环,你会看到你的变化。

您的pop无效,因为Array.pop仅删除了最后一个元素。我猜这不是你想要的。如果要删除特定元素,请使用Array.splice()