我使用角度js来显示表格。当页面加载时我想要突出显示表格的第一行,然后当你点击表格的另一行时,我想删除高亮度类别。
我设法让它使用
在顶行添加课程 ng-class="{'selected-row':$first}"
然后我无法将其删除。我希望能够从$ scope传递变量,就像这样,这样我就可以在点击时更改值...
...控制器
$scope.changeClass = function(){
if ($scope.myClass === "selected-row"){
$scope.myClass = "off";
window.alert('change class here to remove highlight!!!');
} else {
$scope.myClass = "selected-row";
window.alert('change it back!!');
}
... HTML
<tr ng-repeat="row in $data" ng-click="changeClass()" ng-class="{myClass:$first}">
这不起作用,我无法弄清楚语法(如果它可能!!)任何想法?
此处的Plunker:https://plnkr.co/edit/Zs1r6DQcc3vaUDjzkUh8?p=preview
答案 0 :(得分:2)
我已调整您的@inject("AppState")
@observer
export default class SidebarListItem extends Component {
constructor(props) {
super(props)
this.store = this.props.AppState;
}
doSomething() {
this.store.authenticated();
this.store.doSomethingWithNoDecorator();
this.store.authenticated;
}
}
以按$ index跟踪每个项目。首先,我们使用以下行将活动行索引设置为0:
ng-repeat
然后在行的点击事件中,我们将活动行设置为当前单击的行:
$scope.activeRow = 0
然后要查看哪一行需要突出显示,在$scope.setActive = (index) => {
$scope.activeRow = index;
}
中调用以下函数可以实现神奇的效果:
ng-class
答案 1 :(得分:1)
您可以使用ng-class
以及跟踪行索引的数组来实现此目的
// Code goes here
var app = angular.module('ngTableApp', ['ngTable'])
.controller('selectFilterController', function($scope, $filter, $q, NgTableParams) {
var data = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}
];
$scope.class = "selected-row";
$scope.changeClass = function(index){
$scope.selected=[];
$scope.selected[index]=true;
};
$scope.changeClass(0);
$scope.names = [{id: "", title: ""}, {id: 'Moroni', title: 'Moroni'}, {id: 'Enos', title: 'Enos'}, {id: 'Nephi', title: 'Nephi'}];
$scope.tableParams = new NgTableParams({page: 1, count: 10}, {data: data});
})
/* Styles go here */
.selected-row{
border:solid purple 3px;
}
.off{
border:none;
}
.table-striped > tbody > tr:hover{
border:solid purple 3px;
}
.table-striped > tbody > tr:active{
border:solid purple 3px;
}
.table-striped > tbody > tr:focus{
border:solid purple 3px;
}
<!DOCTYPE html>
<html>
<head>
<link data-require="bootstrap@3.3.2" data-semver="3.3.2" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-table/0.8.3/ng-table.min.css" />
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.7" data-semver="1.3.7" src="https://code.angularjs.org/1.3.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-table/0.8.3/ng-table.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="ngTableApp">
<h1>NgTable with select filters</h1>
<div ng-controller="selectFilterController">
<table ng-table="tableParams" class="table table-striped ng-scope ng-table" show-filter="true">
<tbody>
<tr ng-repeat="row in $data" ng-click="changeClass($index)" ng-class="{'selected-row':selected[$index]}">
<!--<tr ng-repeat="row in $data" ng-click="changeClass()" ng-class="{myClass:$first}"> -->
<td data-title="'Name'" filter="{name: 'select'}" filter-data="names" sortable="'name'">{{ row.name }}</td>
<td data-title="'Age'" filter="{age: 'text'}" sortable="'age'">{{ row.age }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>