【问题标题】:how to make the row change color whenever a button clicked and go back to original color when clicking again每当单击按钮时如何使行更改颜色并在再次单击时返回原始颜色
【发布时间】:2020-03-27 10:06:16
【问题描述】:

当点击按钮时,我想改变颜色,当点击同一个按钮时,我想回到原来的颜色。现在当点击按钮时,所有的行都会改变颜色。

html

<table>
      <tbody ng-repeat="field in availableFields">
        <tr  ng-class="{'orderTypeTableRowSelected':tableRowTrueOrFalse}">
                <td style="padding:3px;">{{field.name}}</td>
                <td style="padding:3px;">
                <button type="button" ng-click="orderTypeTableRowSelected(field)" class="btn btn-danger" style="margin-left:10px;"><i class="fas fa-check"></i>&nbsp;Required</button>
        <button type="button" class="btn btn-warning"><i class="fas fa-check"></i>&nbsp;Default&nbsp;&nbsp;&nbsp;</button>
                </td>

    </tr>
    </tbody>
</table>

CSS

<style>

    .orderTypeTableRowSelected {
        background-color: #E0F7FA;
    }
</style>

角度

        $scope.tableRowTrueOrFalse = false;
        $scope.orderTypeTableRowSelected = function (field) {
            $scope.tableRowTrueOrFalse = !$scope.tableRowTrueOrFalse;
            console.log(field);
        };

【问题讨论】:

    标签: html css angularjs


    【解决方案1】:

    这里的问题是您有一个值列表,但只有一个布尔值代表所有这些值。

    tableRowTrueOrFalse 应该是一个布尔值数组,而不是一个布尔值。然后你应该默认使用false 填充数组。

    $scope.tableRowTrueOrFalse = Array(availableFields.length).fill(false);
    

    在你的 html 中,它会是这样的:

    <table>
          <tbody ng-repeat="field in availableFields">
            <tr  ng-class="{'orderTypeTableRowSelected':tableRowTrueOrFalse[$index]}">
                    <td style="padding:3px;">{{field.name}}</td>
                    <td style="padding:3px;">
                    <button type="button" ng-click="orderTypeTableRowSelected(field, $index)" class="btn btn-danger" style="margin-left:10px;"><i class="fas fa-check"></i>&nbsp;Required</button>
            <button type="button" class="btn btn-warning"><i class="fas fa-check"></i>&nbsp;Default&nbsp;&nbsp;&nbsp;</button>
                    </td>
    
        </tr>
        </tbody>
    </table>
    

    然后在你的函数中,

    $scope.orderTypeTableRowSelected = function (field, index) {
                $scope.tableRowTrueOrFalse[index] = !$scope.tableRowTrueOrFalse[index];
                console.log(field);
            };
    

    【讨论】:

    • 该死的.. 我尝试使用 Index 但它现在不起作用现在很清楚了。真是好人啊谢谢。我可以问你一些事情吗?你为什么坚持使用最低版本的 Angularjs。
    • 我不使用 AngularJS。我曾经涉足它。讨厌它。现在我用 Vue 做所有事情……甚至没有 React
    猜你喜欢
    • 1970-01-01
    • 2013-04-12
    • 2012-12-06
    • 1970-01-01
    • 2015-05-29
    • 2021-05-02
    • 2016-05-01
    • 2012-12-19
    • 2021-03-05
    相关资源
    最近更新 更多