繁体   English   中英

在两个数组之间移动对象

[英]Moving objects between two arrays

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

控制器:

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() ,我认为它无法按预期工作。

$.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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM