【问题标题】:How to use index in ListView using Nativescript如何使用 Nativescript 在 ListView 中使用索引
【发布时间】:2019-11-27 09:07:34
【问题描述】:

我对我的代码有疑问:

我在项目中使用了ListView,代码如下:

<ListView class="list-group" [items]="countries" (itemTap)="onItemTap($event)"
    style="height:1250px">
    <ng-template let-country="item" let-i="index">
        <FlexboxLayout flexDirection="row" class="list-group-item">
            <Label [text]="country.name" class="list-group-item-heading"
                verticalAlignment="center" style="width: 60%"></Label>
            <Label [text]="country.price" class="list-group-item-heading"
                verticalAlignment="center" style="width: 60%"></Label>
            <Button (tap)="decrementQty($event)" text='-' style="width: 60%">
            </Button>
            <TextField row="1" col="1" class="textfield" [text]="qty"
                editable="false" style="width: 60%">
            </TextField>
            <Button (tap)="incrementQty($event)" text='+' style="width: 60%">
            </Button>
        </FlexboxLayout>
    </ng-template>
</ListView>

Demo

我的问题是:如何使用索引?因此,当我单击按钮 (tap)="incrementQty($event)" 时,我只想在一行中增加 Qty,而不是在所有行中。为此,我在 ng-table 索引中声明,但不是函数。你能建议我任何解决方案吗?

【问题讨论】:

  • 你想增加国家列表中的单个项目吗?
  • 是的,增加一个项目,而不是全部。

标签: angular typescript listview nativescript


【解决方案1】:

我已经为你更新了游乐场here。您已经在页面级别定义了 qty,这就是为什么当您增加或减少 qty 时,它会应用于所有行。 如果您想在每一行中使用 qty,它应该是 dataprovider 的一部分,例如

countries: { name: string, product_id: string, price: number, qty: number }[] = [
        { name: "Australia", product_id: "1", price: 100, qty: 1 },
        { name: "Belgium", product_id: "2", price: 1009, qty: 1 },
        { name: "Bulgaria", product_id: "3", price: 1008, qty: 1 },
        { name: "Canada", product_id: "4", price: 1006, qty: 1 },
        { name: "Switzerland", product_id: "5", price: 1000, qty: 1 },
        { name: "China", product_id: "6", price: 1011, qty: 1 },
        { name: "Czech Republic", product_id: "7", price: 150, qty: 1 },
        { name: "Germany", product_id: "8", price: 109, qty: 1 },
        { name: "Spain", product_id: "9", price: 10, qty: 16 },
        { name: "Ethiopia", product_id: "10", price: 105, qty: 1 }
    ];

并且在增量上是这样的

incrementQty(args: ItemEventData, i) {
        console.log(i);
        // console.log(this.countries[i].qty + 1);
        this.countries[i].qty += 1;
    }

【讨论】:

  • 我不想在我的 JSON 文件中添加新属性,因为 JSON 文件我得到了表单后端。谢谢!
  • 即使你从后端获取json也可以,你可以在将它添加到列表之前添加id。
  • 我使用(tap)="incrementQty(country.product_id)",但如何在函数中使用它?看不懂
  • incrementQty(product_id) { for (let i = 0; i
【解决方案2】:

试试下面的代码

<Button (tap)="incrementQty($event, i)" text='+' style="width: 60%">
</Button>

在js中使用如下

function incrementQty($event, i) {
   this.countries[i].qty++;
}

【讨论】:

  • 应该可以尝试使用调试器查看按钮单击时 incrementQty 函数内部的内容
  • @AlonBi 简单地指出它不起作用对任何人都没有帮助。如果您遇到问题,请分享更新后的 Playground 示例,以展示您如何在代码中应用它。
  • 另外,我不想在我的 JSON 文件中添加新属性,因为 JSON 文件我得到了表单后端。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
相关资源
最近更新 更多