如何实现Nativescript + Angular列表视图滑动操作以实现项目删除?

时间:2018-09-18 13:27:44

标签: ios angular listview mobile nativescript

我有一个RadListView Nativescript + Angular元素,我想通过滑动操作从其中删除项目:

    <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="&#xf071;"></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>

问题是,我不确定将滑动模板放在哪里以保留“ let-item ngFor”行为以获取该项目的索引。文档提到放置模板:

<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="&#xf1f8;" class="icon swipeIcon" verticalAlignment="center" horizontalAlignment="center"></Label>
  </StackLayout>
</GridLayout>

<ng-template>下,但是如果这样做,我会在'item'循环中迷失方向,然后不知道如何传递索引。如果我将滑动模板放置在ng-template内以保留项目索引,那么列表将在模板所在的位置显示空白区域,因此我的列表无法占据视图的整个高度。

有什么建议吗?非常感谢!!

1 个答案:

答案 0 :(得分:2)

您应该将SwipeTemplate放在ng-template之外的GridLayout中,以实现诸如delete et cetera之类的Swipe操作。获取索引所需的所有内容都通过args发送。查看提供的示例,希望对您有所帮助。

// 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);
}