高亮值angularjs

时间:2019-03-01 06:42:10

标签: angularjs javascript-objects

我想通过以下方式获得结果,

预期工作流程图,

enter image description here

这是默认视图,

enter image description here

方案1:当单击数字“ 1”时,从左到右,孩子们需要如下所示突出显示

enter image description here

方案2:考虑方案1的结果,请从左到右单击数字“ 3”,子级需要删除如下所示的突出显示,因为3被我们视为父级/根级,< / p>

enter image description here

方案3:考虑默认视图,默认情况下没有选择当单击数字“ 18”时,所有父值都需要如下所示突出显示,

enter image description here

方案4:考虑方案3的结果,当仅对18单击数字“ 18”时,需要删除高亮显示,如下所示,不需要从右到左父级取消选择任何值。

enter image description here

注意:在单击任意值时,无需取消从右到左父级的选择。在这种情况下,只需单击值即可删除突出显示。

以下是代码:JSFiddle

 $scope.setActiveSelectedItem = function() {
 return $scope.groupOfCheckboxes[i].RowValues[j].isActive = !$scope.groupOfCheckboxes[i].RowValues[j].isActive;
    }

    $scope.setActivePrevNext = function(arr, id) {
     let keys = Object.keys(arr);
     let nextIndex = keys.indexOf(id) +1;
     let nextItem = keys[nextIndex];
     return $scope.groupOfCheckboxes[i].RowValues[nextIndex].isActive = !$scope.groupOfCheckboxes[i].RowValues[nextIndex].isActive;
    }

    $scope.setBinary = function (id) {

        for(i=0; i<$scope.groupOfCheckboxes.length; i++) {
          for(j=0; j<$scope.groupOfCheckboxes[i].RowValues.length; j++) {

             if($scope.groupOfCheckboxes[i].RowValues[j].td == id) { 
             $scope.setActiveSelectedItem();
             $scope.setActivePrevNext($scope.groupOfCheckboxes, id);

             } 

             }

        }


    }

});

1 个答案:

答案 0 :(得分:4)

HTML

<table>
    <tr ng-repeat="checkbox in groupOfCheckboxes track by $index" ng-init="rowId = $index">
        <td ng-repeat="data in checkbox.RowValues track by $index" ng-init="vId = $index">
            <span ng-if="data.show" ng-click="setBinary(rowId,vId,data)"
                ng-class="data.isActive ? 'active' : ''">{{data.td}}</span>
        </td>
    </tr>
</table>

控制器

    $scope.groupOfCheckboxes = [
    {
        Row: "1",
        RowValues: [
            { td: "1", show: true },
            { td: "2", show: true },
            { td: "3", show: true },
            { td: "4", show: true },
            { td: "5", show: true }
        ]
    },
    {
        Row: "2",
        RowValues: [
            { td: "6", show: false },
            { td: "7", show: false },
            { td: "8", show: false },
            { td: "9", show: true },
            { td: "10", show: false }
        ]
    },
    {
        Row: "3",
        RowValues: [
            { td: "11", show: false },
            { td: "12", show: false },
            { td: "13", show: true },
            { td: "14", show: true }
        ]
    },
    {
        Row: "4",
        RowValues: [
            { td: "15", show: false },
            { td: "16", show: false },
            { td: "17", show: false },
            { td: "18", show: true }
        ]
    }
];

// Setting Parents
for (let rowIndex = $scope.groupOfCheckboxes.length - 1; rowIndex >= 0; rowIndex--) {
    for (let valIndex = $scope.groupOfCheckboxes[rowIndex].RowValues.length - 1; valIndex >= 0; valIndex--) {
        $scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].isActive = false;
        if ($scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].show == true) {
            loop4:
            for (let rx = rowIndex; rx >= 0; rx--) {
                for (let vx = valIndex - 1; vx >= 0; vx--) {
                    if ($scope.groupOfCheckboxes[rx].RowValues[vx].show == true) {
                        $scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].pri = rx;
                        $scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].pvi = vx;
                        break loop4;
                    };
                }
            }
        };
    }
}
$scope.setBinary = function (rowId, vId, data) {
    data.isActive = !data.isActive
    bool = data.isActive;
    loopx:
    for (let row = rowId; row < $scope.groupOfCheckboxes.length; row++) {
        loop1:
        for (let v = vId; v < $scope.groupOfCheckboxes[row].RowValues.length; v++) {
            if (row == rowId) {
                if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
                    $scope.groupOfCheckboxes[row].RowValues[v].isActive = bool;
                } else {
                    break;
                };
            } else {
                if (v == vId) {
                    if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
                        break loopx;
                    }
                } else {
                    if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
                        $scope.groupOfCheckboxes[row].RowValues[v].isActive = bool;
                    }
                }
            }
        }
    }
    function rec(pri, pvi) {
        if ($scope.groupOfCheckboxes[pri]) {
            $scope.groupOfCheckboxes[pri].RowValues[pvi].isActive = true;
            x = $scope.groupOfCheckboxes[pri].RowValues[pvi]
            rec(x.pri, x.pvi)
        }
    }
    rec(data.pri, data.pvi)
}