index.html 2.21 KB
<!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>