I am trying to create a simple CRUD table that does not allow duplicate names. I have been able to get the duplicate to work when adding a contact to the table, but it does not seem to be working when updating the table.
Below is my simple function that verified uniqueness of a contact:
// (allowed) dupCount is 0 when adding, and 1 when in update
// mode to allow saving the contact without making any changed.
var isUnqiue = function(newContact, dupCount) {
var returnVal = true;
var count = 0;
for (var contact in $scope.model.contacts) {
if (newContact.name.toUpperCase() === $scope.model.contacts[contact].name.toUpperCase()) {
count++;
}
}
if (count > dupCount) {
returnVal = false;
}
return returnVal;
}
For some reason, it the duplicate in update mode is not working at all! Even if I update 3 or 4 contacts to the same name, the 'if (count > dupCount) ... ' statement always seem to compare 1 and 1.
Here is a JSFiddle with the entire code: https://jsfiddle.net/7ay9nsLv/
Simple scenario:
Add 'Adam, Smith'
1. Edit 'Adam, Smith', Save without Changing > Good
2. Add 'Adam, Smith' again > Duplicate Error
Add 'Adam, SmithX'
3. Edit 'Adam, SmithX' to 'Adam, Smith' > Duplicate Error
Also note, that the data in the table could be sorted and all, so not sure if passing $index to the controller would be too useful (unless sorting doesn't change the data index).