html - 每当单击按钮时如何使行更改颜色并在再次单击时返回原始颜色
问题描述
单击按钮时,我想更改颜色,单击同一按钮时,我想返回原始颜色。现在,当单击按钮时,所有行都会改变颜色。
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> Required</button>
<button type="button" class="btn btn-warning"><i class="fas fa-check"></i> Default </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);
};
解决方案
这里的问题是你有一个值列表,但只有一个布尔值代表所有这些值。
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> Required</button>
<button type="button" class="btn btn-warning"><i class="fas fa-check"></i> Default </button>
</td>
</tr>
</tbody>
</table>
然后在你的函数中,
$scope.orderTypeTableRowSelected = function (field, index) {
$scope.tableRowTrueOrFalse[index] = !$scope.tableRowTrueOrFalse[index];
console.log(field);
};
推荐阅读
- c# - 机器人框架 | 直线 | 附件上传 - 需要有关限制、安全和存储的更多信息
- elasticsearch - 使用function_score时如何向弹性查询添加过滤器?
- android - 如何禁用点击 TabLayout 内的 TabItem?
- php - 允许其他人连接到我的 XAMPP mysql
- groovy - Mule Anypoint Studio groovy.lang.MissingPropertyException:没有这样的属性:类的 StringEscapeUtils
- python - 该应用程序使用 UWSGI 和 Flask (tiangolo/uwsgi-nginx-flask) 在生产模式下运行两次
- postgresql - Postgres:带有时区的时间戳类型的无效输入语法:“”
- sql - 使用 SQL 汇总数据
- css - Web 配置和 IIS
- excel - Excel宏上的元素相乘