index.html
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.error{
border-color: darkred;
}
</style>
</head>
<body ng-app="test" ng-controller="ctrlTest" ng-init="validator = validator">
<ng-form name="mForm" matrix-validator data-all="validator">
<table>
<tr ng-repeat="row in matrix" ng-init="rowId = $index">
<td ng-repeat="item in row track by $index">
<input ng-model="row[$index]" type="number"
data-row="rowId" data-col="$index"
name="item{{rowId}}_{{$index}}"
ng-class="{error: mForm['item'+rowId+'_'+$index].$invalid}"
>
</td>
</tr>
</table>
</ng-form>
<script src="../node_modules/angular/angular.js"></script>
<script src="../src/matrix-validation.js"></script>
<script>
angular.module('test', ['matrixValidation']).controller('ctrlTest', [
'$scope',
($scope) => {
$scope.matrix = [
[0,0,0],
[0,0,0],
[0,0,0]
];
$scope.test=2;
$scope.validator = (matrix, validationMatrix) => {
for(let x = 0; x < matrix.length; x++){
let row = matrix[x];
for(let y = 0; y < row.length; y++){
let value = row[y];
if(validationMatrix[x][y] != null) continue;
for(
let z = (x < matrix.length - 1 ) ? x+1 : 0;
z < ((x < matrix.length -1 ) ? matrix.length : matrix.length - 1);
z++
){
if(value == matrix[z][y]){
validationMatrix[x][y] = false;
validationMatrix[z][y] = false;
break;
}
}
if(validationMatrix[x][y] === null) validationMatrix[x][y] = true;
}
}
return validationMatrix;
};
}
]);
</script>
</body>
</html>