I have an array made of mysql table in angularjs, i return it into json with php and get it in angularjs. I have created an application in ionic and make use of angularjs. I can delete item's, insert items and edit item. Items are having attribute priority, and i want priority to be unique. So on item add i am checking for the last added item.priority and next one has priority + 1. On item delete, i check the item.priority so next added item will have the priority set as the priority of deleted item. But for example i have 6 items and i delete item4 so next added item will have priority 4, but when i add next it's on priority 5, but I already have 5 so i want to have priority set on 7 as i have 6 items. So what i try is :
$scope.priority = Math.max(parseInt($rootScope.obiadki.priority));
But it returns undefined. My question is, how can i check for the max value of priority attribute of all objects in whole array.
How i retrive data :
$scope.getData = function () {
$http({
method: 'get',
url: 'mygetdatacodeurl'
}).then(function successCallback(response) {
// Store response data
$rootScope.obiadki = response.data;
$scope.checkPriority();
});
};
How i check for the priority :
$scope.checkPriority = function () {
$scope.priority = Math.max(parseInt($rootScope.obiadki.priority));
};
How i check priority on addItem :
$scope.checkPriorityOnAdd = function () {
$scope.priority = parseInt($scope.priority) + 1;
};
my addItem function :
$scope.addItem = function() {
$http({
method: "post",
url: 'myinsertcodeurl',
data: {
id: $scope.id,
obiad_name: $scope.obiad_name,
active: $scope.active,
priority: $scope.priority
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
$scope.getData();
$scope.checkPriorityOnAdd();
};
my deleteItem function where i set priority on delete :
$scope.onItemDelete = function(item) {
$http({
method: "post",
url: 'mydeletecodeurl',
data: {
id: item.id
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
$scope.getData();
$scope.priority = item.priority;
};