2016-11-28 160 views
2

我正在使用angular,我试图创建一个“全选”按钮。indexof - 检查一个值是否已经在javascript数组中)

我有一个项目列表,每个项目都有一个切换,我正在做的是,在变化(每次切换从真(选择)变为假(未选定),我运行一个函数来创建一个数组与

所选元素的所有ID。这工作几乎是完美的,问题是我面临的indexfOf方法的一些问题,以检查是否ID已在数组中。

var isInArray; 

isInArray = function(arr, id) { 
    console.log("index of ", arr.indexOf(id)); 
    return arr.indexOf(id); 
}; 


scope.evtSelectAll = function() { 
    return angular.forEach(scope.listToDisplay, function(element) { 
    element.copyTo = true; 
    return scope.selectFromList(element.iID, element.copyTo); 
    }); 
}; 

scope.selectFromList = function(id, copy) { 
    if (copy === true && isInArray(scope.selected, id) === -1) { 
    scope.selected.push(id); 
    } else { 
    scope.selected.pop(id); 
    } 
    console.log("scope.selected - ", scope.selected); 
    if (scope.selected.length > 0) { 
    console.log("Emitted event: can proceed!"); 
    scope.$emit('enough-elements'); 
    } else { 
    console.log("Emitted event: can not proceed!"); 
    scope.$emit('not-enough-elements'); 
    } 
    return scope.result = scope.selected; 
}; 

的我得到的问题是当数组(scope.selected)有多个ID时

比方说,例如,我的s​​cope.selected看起来像这样:

scope.selected = [2,3,4,7] 

如果我点击全选,没有被添加(这是正确的)

现在,让我们说我勾去掉4和7例如,我的s​​cope.selected现在看起来是这样的:

scope.selected = [2,3] 

如果我现在点击全选,我的结果如下:[2,4,7]。

我失去了

我认为这是由于这样的事实,我的数组没有一个单一的项目?

感谢您的任何帮助。这里还有一个快速的codepen来解释这个问题。如果您检查控制台并使用切换进行播放,您应该能够立即看到我所指的内容。

在此先感谢

+2

如果完全清空$ scope.selected,并且一旦发生切换,通过重新分析列表中选中的元素来重新创建它会是一个问题吗? –

+0

不,我不明白为什么它应该是一个诚实的问题。我只需要小心地将它清空,只是选择我猜的全部,对吧? – Nick

+1

至少在codepen示例中,'element.iID'应改为'element.id' – Matthias

回答

1

由于马蒂亚斯基督教Bonato了他们的建议。

最后,我解决了使用他们的两个建议,最终结果似乎按预期工作。

下面是与最终版本codepen:http://codepen.io/NickHG/pen/KNXPBb

基本上,我添加元素之前改变

scope.selected.pop(id); 

$scope.selected.splice(isInArray($scope.selected, id),1); 

,并在全选事件函数,我总是空scope.selected[]到阵列

$scope.evtSelectAll = function() { 
$scope.selected = [] 
angular.forEach($scope.list, function(element) { 
    element.copyTo = true; 
    return $scope.selectFromList(element.id, element.copyTo); 
}); 

};

谢谢你的帮助!

+1

清空阵列似乎是一个很好的解决方案,这里的问题,好工作 – Matthias

+0

不要忘记接受你的答案作为解决方案 – Matthias

+0

我只能在两天内接受。我会尽快接受。谢谢 – Nick

0

我认为你的代码主要包含逻辑错误。您正在使用功能selectFromList取消选择(单独完成时)和全选(您不想用于取消选择)。

正如有人在指出,由于某种原因,现在删除回答,pop.()功能不应该与任何参数调用(它只是除去最后一个元素),你应该使用拼接这样的: $scope.selected.splice(isInArray($scope.selected, id),1);

除非你真的需要发射功能上选择的所有运行,你可以尝试,如果这是你的答案:

var isInArray; 

isInArray = function(arr, id) { 
    console.log("index of ", arr.indexOf(id)); 
    return arr.indexOf(id); 
}; 


scope.evtSelectAll = function() { 
    return angular.forEach(scope.listToDisplay, function(element) { 
    element.copyTo = true; 
    if (isInArray($scope.selected, element.id) === -1) { 
     $scope.selected.push(element.id); 
    } 
    }); 
}; 

scope.selectFromList = function(id, copy) { 
    if (copy === true && isInArray(scope.selected, id) === -1) { 
    scope.selected.push(id); 
    } else { 
    $scope.selected.splice(isInArray($scope.selected, id), 1); 
    } 
    console.log("scope.selected - ", scope.selected); 
    if (scope.selected.length > 0) { 
    console.log("Emitted event: can proceed!"); 
    scope.$emit('enough-elements'); 
    } else { 
    console.log("Emitted event: can not proceed!"); 
    scope.$emit('not-enough-elements'); 
    } 
    return scope.result = scope.selected; 
}; 

现在全部选择只增加​​如果没有找到ID在​​列表中。

+0

的感谢您的答案。我添加了你的行,并且我改变了select all函数以包含第一个注释(当我单击select all时选择了空的scope.Selected),并且使用两个组合解决方案,它可以根据需要进行接缝工作。谢谢很多 – Nick

相关问题