SegmentedBar get the selected id

nativescriptcore

#1

I’d like to access the selected id of a segmentedBar item, so I would be able to dinamically add/remove different items without depending on the position (index) of the items on the code …

I think that a segmentedBar.selectedId would be great in order to get rid of Item1, 2 …

onSelectedIndexChange(args) {
        const segmentedBar = <SegmentedBar>args.object;
        this.prop = "Item" + (segmentedBar.selectedIndex + 1);

        switch (this.prop) {
            case "Item1":

#2

You could use the selectedIndex to get the item, it’s id or anything.

HTML

<SegmentedBar [items]="myItems" selectedIndex="0" (selectedIndexChange)="onSelectedIndexChange($event)" class="m-5"></SegmentedBar>
<Label [text]="prop" textWrap="true" verticalAlign="center" class="h2 p-15 text-center"></Label>

TS

import { Component } from "@angular/core";

import { PropertyChangeData } from "data/observable";
import { SegmentedBar, SegmentedBarItem } from "ui/segmented-bar";

.....
.....

    public myItems: Array<SegmentedBarItem>;
    public prop: string = "Item 1";

    constructor() {
        this.myItems = [];
        for (let i = 1; i < 5; i++) {
            const item = new SegmentedBarItem();
            (<any>item).id = i;
            item.title = "Tab " + i;
            this.myItems.push(item);
        }
    }

    public onSelectedIndexChange(event: PropertyChangeData) {
        let item = (<SegmentedBar>event.object).items[event.value];
        this.prop = item.id;
    }

....
....


#3

Works like a charm and really obvious now :wink: Thanks!!