更改 ListView 中当前所选项目的背景颜色的问题 - Nativescript/Angular2
我正忙于一个 Nativescript/Angular2 应用程序,我在一个 ListView 中显示活动股票列表.我正在尝试更改 ListView 中当前选定的盘点项目的背景颜色以显示当前选定的盘点.我将 css 类添加到 ng-template 内的 StackLayout,但由于某种原因,当我点击一个项目时,css 没有被应用.知道这里可能出了什么问题/我如何解决这个问题吗?我没有收到任何错误,所以我完全不知道这里可能有什么问题......
I am busy with a Nativescript/Angular2 app where I display a list of active stock takes in a ListView. I am trying to change the background color lf the current selected stock take item in the ListView to show the currently selected StockTake. I add the css class to the StackLayout inside the ng-template but for some reason the css isn't applied when I tap on a item. Any idea what could be going wrong here/how I can fix this? I don't get any errors so i'm completely lost as to what the issue might be here...
my ListView xml code:
<ListView [items]="activeStockTakes" class="list-group">
<ng-template let-activeStockTake="item" let-i="index">
<StackLayout [class.highlight]="item.isSelected" (tap)="selectActiveStockTake(item, index)">
<Label class="list-group-item" [text]="activeStockTake.UserCode + ' - ' + activeStockTake.Comment"></Label>
</StackLayout>
</ng-template>
</ListView>
my css code:
.highlight {
background-color: red !important;
border-radius: 3;
}
my selectActiveStockTake event:
selectActiveStockTake(item, index) {
//console.log(args.index);
this.selectedActiveStockTake = this.activeStockTakes[index];
this.selectedActiveStockTake.isSelected = true;
console.log(this.selectedActiveStockTake.UserCode);
}
尝试
<ListView [items]="activeStockTakes" class="list-group">
<ng-template let-activeStockTake="item" let-i="index">
<StackLayout [class.highlight]="item.isSelected" (tap)="selectActiveStockTake(item, i)">
<Label class="list-group-item" [text]="activeStockTake.UserCode + ' - ' + activeStockTake.Comment"></Label>
</StackLayout>
</ng-template>
</ListView>
然后:
selectActiveStockTake(item, i) {
item.isSelected = !item.isSelected;
}
这是一个更干净的解决方案,应该可以完美运行.
This is a much cleaner solution and should work perfectly.