1

I have a question about my code:

I used ListView in my project, code like below:

<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

My question is: How to use index? So when I click button (tap)="incrementQty($event)" I want to increment Qty only in one row, not in all rows. For this I declare in ng-table index, but not function. Can you suggest me any solution please?

2
  • Do you want to increment a single item in countries list? Commented Jul 18, 2019 at 12:32
  • Yes, increment a single item, not all. Commented Jul 18, 2019 at 12:36

2 Answers 2

1

I have updated the playground for you here. You have defined the qty at page level, that is why when you were incrementing or decremnting the qty , it was getting applied to all rows. If you want to play with qty in each row, it should be part of dataprovider e.g.

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 }
    ];

and on increment so omething like this

incrementQty(args: ItemEventData, i) {
        console.log(i);
        // console.log(this.countries[i].qty + 1);
        this.countries[i].qty += 1;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I don't want to add new property in my JSON file, because JSON file I get form backend. Thank you!
It’s fine even you are getting json from backend, you can add id before aasiging it to list.
I use (tap)="incrementQty(country.product_id)" but how to use it in function? I can't understand
incrementQty(product_id) { for (let i = 0; i < this.countries.length; i++) { if (this.countries[i].product_id == product_id) { this.countries[i].qty += 1; } } }
0

Try the following code

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

And in js use the following

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

4 Comments

It should work try to use a debugger to see what goes inside incrementQty function on button click
@AlonBi Simply pointing it doesn't work won't help any one. If you are facing issues, then share the updated Playground sample to show how you applied this in your code.
Also, I don't want to add new property in my JSON file, because JSON file I get form backend. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.