I am working on a website which has custom designed tables all over the application. All tables have a common functionality of checkbox check/uncheck.
I have used a very simple way to do this. Creating an array of selected ids. On toggle, check if Id exists in the array, then remove else add. And on checkAll/uncheckAll, push all the ids in the array or empty the array.
This code is repeated in each controller of every page that has a table in it.
What I am looking for is some help/suggestion, how to make this functional by defining this code in some service/factory and can be used all over the application.
Toggle check/uncheck:
function toggle(id) {
let index = vm.selectedIds.indexOf(id)
if(index >= 0) {
vm.selectedIds.splice(index, 1);
}
else {
vm.selectedIds.push(id);
}
}
CheckAll/uncheckAll:
function checkUncheckAll() {
if(vm.selectedIds.length != vm.renderedList.length && vm.selectedIds.length <= vm.renderedList.length) {
vm.selectedIds = [];
angular.forEach(vm.renderedList,function(row){
vm.selectedIds.push(row._id);
});
}
else {
vm.selectedIds = [];
}
}
Let me know if I need to add any other details or any other code.
Thanks.
Aucun commentaire:
Enregistrer un commentaire