【问题标题】:Moving objects between two arrays在两个数组之间移动对象
【发布时间】:2014-01-21 04:36:15
【问题描述】:

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

控制器:

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

【问题讨论】:

    标签: javascript arrays angularjs object ng-options


    【解决方案1】:

    $.when.apply($,addPromise).done() 似乎不是角度 api 或同步的。所以角度不知道你的变化。您必须将代码包装在 $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()

    【讨论】:

    • 哦,哇,我刚刚阅读了有关 apply() 和 digest() 的内容,但从未建立过适用于您上面指出的代码的联系。是的,你说得对,流行音乐不是我想做的。我需要从组数组中删除 selectedGroups 。就像我有一个数组列表 1,2,3 并选择(这是它自己的数组)1,3,我需要删除那些留下 2。不过我会应用你建议的修复,一次一个问题.谢谢
    • 在我的服务中使用它时出现 $scope is not defined 错误。
    • @Batman $scope 在您的服务中不可用。您可以为 addUserToGroup 函数提供一个附加参数。如果可行,我建议您以使用承诺($q servcie)的方式重构您的代码,这样可以减少依赖关系并为您调用 $apply。
    • SPServices 已经返回承诺。 $.when 基本上是在承诺完成后处理它们。我不太确定如何将其转换为 Angular Promises
    • 我尝试采纳你关于使用 Angular Promise 的建议,但不确定是否正确:jsfiddle.net/4Qfrm
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2020-08-08
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多