Nativescript add tab into TabView dynamically - co

2019-06-02 21:02发布

I asked two days ago question about ListView but now I find out the core problem wasn't about listview but the problem was in TabView. On the start when It was created tabs everything looks fine but when I Tap on button which add another tab it doesn't work correctly. Tab is added (on the end) and selected index of tabs is 2 but content of previous tab (tab index 1) disappears. But when I go to last tab (index 3) and back to tab (index 1) content is there.

Here is the code snippet where you can try:

Home.component.ts

import { Component, OnInit, NgZone } from "@angular/core";
import { Page } from "tns-core-modules/ui/page/page";
import { ListView } from "ui/list-view"

@Component({
    selector: "app-home",
    moduleId: module.id,
    templateUrl: "./home.component.html",
    styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

    site = [{ "name": "cau 1", }, { "name": "cau 2" },
    { "name": "settings" }];

    tabSelectedIndex = 0;
    onItemTap(args) {
        this.ngZone.run(() => {
            this.site.push({ "name": "next" });
            this.tabSelectedIndex = 2;
        });
    }

    constructor(private ngZone: NgZone, private page: Page) {
    }

    ngOnInit(): void {
    }
    onTabChanged(args) {
        console.log("ahoj");
    }
}

Home.component.html

<ActionBar title="YOUR APP"></ActionBar>
<GridLayout class="page">
    <TabView [(ngModel)]="tabSelectedIndex" height="100%" (selectedIndexChange)="onTabChanged($event)" class="content p-20">
        <ng-template ngFor let-item [ngForOf]="site">
            <StackLayout *tabItem="{title: item.name}">

                <Label row="0" col="0" [text]="test"></Label>
                <Button text="+" class="btn btn-active tmp" (tap)="onItemTap($event)"> </Button>
            </StackLayout>
        </ng-template>
    </TabView>
</GridLayout>

Tested on android emulator only.

Additional question: It is possible to add new tab as first (to index 0)?

Thank you very much for your time and any answer.

EDIT: This code you can try here

Or maybe better sample is here

1条回答
手持菜刀,她持情操
2楼-- · 2019-06-02 21:34

I created a nativescript issue. Answer is " adding/removing tab view items are not officially supported operations and is a known limitation for that widget." Here is link for the issue.

查看更多
登录 后发表回答