更改 ListView 中当前所选项目的背景颜色的问题 - Nativescript/Angular2 [英] Issues changing the background colour of the currently selected item in a ListView - Nativescript/Angular2

查看:33
本文介绍了更改 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.

这篇关于更改 ListView 中当前所选项目的背景颜色的问题 - Nativescript/Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆