Reputation: 147
I have a RadListView Nativescript + Angular element that I would like to delete items from via swipe action:
<RadListView [items]="stockList" id="listview" style="height: 100%; background-color: #f5f5f5;" swipeActions="true" pullToRefresh="true"
(pullToRefreshInitiated)="onPullToRefreshInitiated($event)"
(itemSwipeProgressEnded)="onSwipeCellFinished($event)"
(itemSwipeProgressStarted)="onSwipeCellStarted($event)"
(itemSwipeProgressChanged)="onCellSwiping($event)">
<ng-template let-item="item">
<GridLayout rows="*,*" colSpan="5" columns="*,2*,*,*" (tap)="onTap(item.name)">
<SVGImage col="0" row="0" rowSpan="2" class="pgrRating" [src]="pgrRating(item.PGR, item.raw_PGR)"></SVGImage>
<!-- TICKER -->
<Label row="0" col="1" horizontalAlignment="left" class="itemName" [text]="item.symbol"></Label>
<!-- COMPANY NAME -->
<Label row="1" col="1" horizontalAlignment="left" class="itemCompName" [text]="item.name"></Label>
<!-- LAST PRICE TODAY -->
<Label row="0" col="2" colSpan="2" horizontalAlignment="left" class="itemPrice"
[text]="'$' + (item.Last).toFixed(2)"
[ngClass]="item.Change == 0 ? 'black' : (item.Change > 0 ? 'green' : 'red')"></Label>
<Label style="color: #c00000" row="0" col="3" class="icon" text=""></Label>
<!-- $ CHANGE TODAY -->
<Label row="1" col="2" horizontalAlignment="left" class="itemChange"
[text]="item.Change.toFixed(2)"
[ngClass]="item.Change == 0 ? 'black' : (item.Change > 0 ? 'green' : 'red')"></Label>
<!-- % CHANGE TODAY -->
<Label row="1" col="3" horizontalAlignment="left" class="itemPriceChange"
[text]="item.Change > 0 ? ('(+' + item['Percentage '].toFixed(2) + '%)') : ('(' + item['Percentage '].toFixed(2) + '%)')"
[ngClass]="item.Change == 0 ? 'black' : (item.Change > 0 ? 'green' : 'red')"></Label>
</GridLayout>
</ng-template>
</RadListView>
Problem is, I'm not sure where to put the swipe template to retain the 'let-item ngFor' behavior to get the index of that item. The docs mention to put the template:
<GridLayout *tkListItemSwipeTemplate columns="auto, *, auto" class="gridLayoutLayout">
<StackLayout id="mark-view" col="0" class="icon" (tap)="onLeftSwipeClick($event)">
<Label text="mark" class="swipetemplateLabel" verticalAlignment="center" horizontalAlignment="center"></Label>
</StackLayout>
<StackLayout id="delete-view" col="2" class="" (tap)="onRightSwipeClick(item.index)">
<Label row="0" col="2" text="" class="icon swipeIcon" verticalAlignment="center" horizontalAlignment="center"></Label>
</StackLayout>
</GridLayout>
under the <ng-template>
but if I do so, I lose out on the 'item' loop and then don't know how to pass the index. If I place the swipe template inside the ng-template to retain the item index, then the list displays a blank area where the template would be, so then my list can't occupy the whole height of the view.
Any advice? Thanks so much!!
Upvotes: 1
Views: 2292
Reputation: 659
You should put SwipeTemplate in a GridLayout outside of the ng-template in order to achieve Swipe actions like delete et cetera. Everything you need to get an index is sent through args. Check out the example provided, I hope it helps.
// xml file
<GridLayout orientation="vertical" rows="auto, *" tkExampleTitle tkToggleNavButton>
<RadListView #myListView [items]="dataItems" row="1" selectionBehavior="None" (itemSwipeProgressEnded)="onSwipeCellFinished($event)"
(itemSwipeProgressStarted)="onSwipeCellStarted($event)" (itemSwipeProgressChanged)="onCellSwiping($event)" swipeActions="true">
<ng-template tkListItemTemplate let-item="item">
<StackLayout class="listItemStackLayout" orientation="vertical">
<Label class="labelName" [text]="item.name"></Label>
<Label class="labelTitle" [text]="item.title"></Label>
<Label class="labelText" [text]="item.text"></Label>
</StackLayout>
</ng-template>
<GridLayout *tkListItemSwipeTemplate columns="auto, *, auto" class="gridLayoutLayout">
<StackLayout id="mark-view" col="0" class="markViewStackLayout" (tap)="onLeftSwipeClick($event)">
<Label text="mark" class="swipetemplateLabel" verticalAlignment="center" horizontalAlignment="center"></Label>
</StackLayout>
<StackLayout id="delete-view" col="2" class="deleteViewStackLayout" (tap)="onRightSwipeClick($event)">
<Label text="delete" class="swipetemplateLabel" verticalAlignment="center" horizontalAlignment="center"></Label>
</StackLayout>
</GridLayout>
</RadListView>
</GridLayout>
// typescript file
public onLeftSwipeClick(args: ListViewEventData) {
console.log("Left swipe click");
this.listViewComponent.listView.notifySwipeToExecuteFinished();
}
public onRightSwipeClick(args) {
console.log("Right swipe click");
this.dataItems.splice(this.dataItems.indexOf(args.object.bindingContext), 1);
}
Upvotes: 3