Nativescript Array of Switch elements | Tricks to read value in tap of Button
Scenario:
Using Nativescript 5.0 with Angular
- I have data coming from API (customers.component.ts)
- Rendering n number of as many IDs I get from API. Works great (customers.component.html)
- User taps the Switches to make his choice and finally taps Save Button which calls a function saveData()
- In saveData(), I am unable to read which Switches are checked.
I tried below in customers.component.html, it renders perfectly based on the data received from API. No error in this.
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [id]="'switch[' + id + ']'" class="m-15" row="0" col="1" ></Switch>
</GridLayout>
</ng-template>
</ListView>
Is
[id]="'switch[' + id + ']'"
this ok?
Is there any way to read if any of the Switches are checked.
Switches are dynamically created based on API data.
**Edit - adding code below **
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch> </GridLayout>
</ng-template>
</ListView>
and important portion of ts file is
import Component, OnInit from "@angular/core";
import RouterExtensions from "nativescript-angular/router";
import ActivatedRoute from "@angular/router";
import ObservableArray from "data/observable-array";
import * as Permissions from "nativescript-permissions";
import Coursemeta from "../../shared/coursemeta.model";
import CoursemetaService from "~/app/shared/coursemeta.service";
var contacts = require( "nativescript-contacts" );
import finalize from 'rxjs/operators';
export class ContactManagerComponent implements OnInit
public constructor(private router: RouterExtensions, private route: ActivatedRoute, private _coursemetaservice: CoursemetaService)
public _coursesmeta: ObservableArray<Coursemeta> = new ObservableArray<Coursemeta>();
ngOnInit(): void
this.callerName=this.route.snapshot.params["callerName"];
this.callerNumber=this.route.snapshot.params["callerNumber"];
this.input="";
this._coursemetaservice.getCoursesmeta()
.pipe(finalize(() => this._isLoading = false))
.subscribe((coursesmeta: Array<Coursemeta>) =>
this._coursesmeta = new ObservableArray(coursesmeta);
this.courseCount=this._coursesmeta.length;
this._isLoading = false;
);
public ShowSelectedCourses()
for(var i=0; i < this._coursesmeta.length; i++)
if(this._coursesmeta.getItem(i).selected == false)
alert("false = " + i);
alert("Saved ....");
What I expect is, if user slides the switch, then selected property should change its value as its part of observablearray.
I am able to see data as expected, no error in API data communication, rendering is perfect.
angular typescript nativescript
add a comment |
Scenario:
Using Nativescript 5.0 with Angular
- I have data coming from API (customers.component.ts)
- Rendering n number of as many IDs I get from API. Works great (customers.component.html)
- User taps the Switches to make his choice and finally taps Save Button which calls a function saveData()
- In saveData(), I am unable to read which Switches are checked.
I tried below in customers.component.html, it renders perfectly based on the data received from API. No error in this.
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [id]="'switch[' + id + ']'" class="m-15" row="0" col="1" ></Switch>
</GridLayout>
</ng-template>
</ListView>
Is
[id]="'switch[' + id + ']'"
this ok?
Is there any way to read if any of the Switches are checked.
Switches are dynamically created based on API data.
**Edit - adding code below **
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch> </GridLayout>
</ng-template>
</ListView>
and important portion of ts file is
import Component, OnInit from "@angular/core";
import RouterExtensions from "nativescript-angular/router";
import ActivatedRoute from "@angular/router";
import ObservableArray from "data/observable-array";
import * as Permissions from "nativescript-permissions";
import Coursemeta from "../../shared/coursemeta.model";
import CoursemetaService from "~/app/shared/coursemeta.service";
var contacts = require( "nativescript-contacts" );
import finalize from 'rxjs/operators';
export class ContactManagerComponent implements OnInit
public constructor(private router: RouterExtensions, private route: ActivatedRoute, private _coursemetaservice: CoursemetaService)
public _coursesmeta: ObservableArray<Coursemeta> = new ObservableArray<Coursemeta>();
ngOnInit(): void
this.callerName=this.route.snapshot.params["callerName"];
this.callerNumber=this.route.snapshot.params["callerNumber"];
this.input="";
this._coursemetaservice.getCoursesmeta()
.pipe(finalize(() => this._isLoading = false))
.subscribe((coursesmeta: Array<Coursemeta>) =>
this._coursesmeta = new ObservableArray(coursesmeta);
this.courseCount=this._coursesmeta.length;
this._isLoading = false;
);
public ShowSelectedCourses()
for(var i=0; i < this._coursesmeta.length; i++)
if(this._coursesmeta.getItem(i).selected == false)
alert("false = " + i);
alert("Saved ....");
What I expect is, if user slides the switch, then selected property should change its value as its part of observablearray.
I am able to see data as expected, no error in API data communication, rendering is perfect.
angular typescript nativescript
add a comment |
Scenario:
Using Nativescript 5.0 with Angular
- I have data coming from API (customers.component.ts)
- Rendering n number of as many IDs I get from API. Works great (customers.component.html)
- User taps the Switches to make his choice and finally taps Save Button which calls a function saveData()
- In saveData(), I am unable to read which Switches are checked.
I tried below in customers.component.html, it renders perfectly based on the data received from API. No error in this.
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [id]="'switch[' + id + ']'" class="m-15" row="0" col="1" ></Switch>
</GridLayout>
</ng-template>
</ListView>
Is
[id]="'switch[' + id + ']'"
this ok?
Is there any way to read if any of the Switches are checked.
Switches are dynamically created based on API data.
**Edit - adding code below **
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch> </GridLayout>
</ng-template>
</ListView>
and important portion of ts file is
import Component, OnInit from "@angular/core";
import RouterExtensions from "nativescript-angular/router";
import ActivatedRoute from "@angular/router";
import ObservableArray from "data/observable-array";
import * as Permissions from "nativescript-permissions";
import Coursemeta from "../../shared/coursemeta.model";
import CoursemetaService from "~/app/shared/coursemeta.service";
var contacts = require( "nativescript-contacts" );
import finalize from 'rxjs/operators';
export class ContactManagerComponent implements OnInit
public constructor(private router: RouterExtensions, private route: ActivatedRoute, private _coursemetaservice: CoursemetaService)
public _coursesmeta: ObservableArray<Coursemeta> = new ObservableArray<Coursemeta>();
ngOnInit(): void
this.callerName=this.route.snapshot.params["callerName"];
this.callerNumber=this.route.snapshot.params["callerNumber"];
this.input="";
this._coursemetaservice.getCoursesmeta()
.pipe(finalize(() => this._isLoading = false))
.subscribe((coursesmeta: Array<Coursemeta>) =>
this._coursesmeta = new ObservableArray(coursesmeta);
this.courseCount=this._coursesmeta.length;
this._isLoading = false;
);
public ShowSelectedCourses()
for(var i=0; i < this._coursesmeta.length; i++)
if(this._coursesmeta.getItem(i).selected == false)
alert("false = " + i);
alert("Saved ....");
What I expect is, if user slides the switch, then selected property should change its value as its part of observablearray.
I am able to see data as expected, no error in API data communication, rendering is perfect.
angular typescript nativescript
Scenario:
Using Nativescript 5.0 with Angular
- I have data coming from API (customers.component.ts)
- Rendering n number of as many IDs I get from API. Works great (customers.component.html)
- User taps the Switches to make his choice and finally taps Save Button which calls a function saveData()
- In saveData(), I am unable to read which Switches are checked.
I tried below in customers.component.html, it renders perfectly based on the data received from API. No error in this.
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [id]="'switch[' + id + ']'" class="m-15" row="0" col="1" ></Switch>
</GridLayout>
</ng-template>
</ListView>
Is
[id]="'switch[' + id + ']'"
this ok?
Is there any way to read if any of the Switches are checked.
Switches are dynamically created based on API data.
**Edit - adding code below **
<ListView [items]="_coursesmeta" [height] = "_coursesmeta.length == 0 ? 30 : _coursesmeta.length * 70" class="list-group">
<ng-template let-result="item">
<GridLayout rows="auto auto" columns="* *" class="m-5" verticalAlignment="stretch">
<Label class="h3 m-15" [text]="result.coursename" textWrap="true" row="0" col="0"></Label>
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch> </GridLayout>
</ng-template>
</ListView>
and important portion of ts file is
import Component, OnInit from "@angular/core";
import RouterExtensions from "nativescript-angular/router";
import ActivatedRoute from "@angular/router";
import ObservableArray from "data/observable-array";
import * as Permissions from "nativescript-permissions";
import Coursemeta from "../../shared/coursemeta.model";
import CoursemetaService from "~/app/shared/coursemeta.service";
var contacts = require( "nativescript-contacts" );
import finalize from 'rxjs/operators';
export class ContactManagerComponent implements OnInit
public constructor(private router: RouterExtensions, private route: ActivatedRoute, private _coursemetaservice: CoursemetaService)
public _coursesmeta: ObservableArray<Coursemeta> = new ObservableArray<Coursemeta>();
ngOnInit(): void
this.callerName=this.route.snapshot.params["callerName"];
this.callerNumber=this.route.snapshot.params["callerNumber"];
this.input="";
this._coursemetaservice.getCoursesmeta()
.pipe(finalize(() => this._isLoading = false))
.subscribe((coursesmeta: Array<Coursemeta>) =>
this._coursesmeta = new ObservableArray(coursesmeta);
this.courseCount=this._coursesmeta.length;
this._isLoading = false;
);
public ShowSelectedCourses()
for(var i=0; i < this._coursesmeta.length; i++)
if(this._coursesmeta.getItem(i).selected == false)
alert("false = " + i);
alert("Saved ....");
What I expect is, if user slides the switch, then selected property should change its value as its part of observablearray.
I am able to see data as expected, no error in API data communication, rendering is perfect.
angular typescript nativescript
angular typescript nativescript
edited Nov 14 '18 at 13:30
Pratik
asked Nov 14 '18 at 9:12
PratikPratik
185
185
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Mutiple ways to achive the desired
Add the onSwitchChecked
and in your .ts file handle the onSwitchChecked
import Switch from 'ui/switch';
public onSwitchChecked(args) {
const sortSwitch = <Switch>args.object;
if (sortSwitch.id === 'id of your swich')
if (sortSwitch.checked)
// Change the _coursesmeta or result.selected here.
Use the ng model for your result.checked e.g.
<Switch [(ngModel)]='isSwitchChecked' (checkedChange)="onSwitchChecked($event)"></Switch>
and in yout .ts
public onSwitchChecked()
alert(this.isSwitchChecked);
I have created playground and you can test functionality there.
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
add a comment |
I wouldn't recommend using dynamic IDs. Also when using ListView, it's not guaranteed that all your Switches / templates are alive, as you scroll up and down same element will be recycled with different data to retain performance.
So you should play with data here, bind the checked state of Switch to a property in data item. For example
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch>
Now you will just have to loop through the array to find which item is selected.
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296561%2fnativescript-array-of-switch-elements-tricks-to-read-value-in-tap-of-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Mutiple ways to achive the desired
Add the onSwitchChecked
and in your .ts file handle the onSwitchChecked
import Switch from 'ui/switch';
public onSwitchChecked(args) {
const sortSwitch = <Switch>args.object;
if (sortSwitch.id === 'id of your swich')
if (sortSwitch.checked)
// Change the _coursesmeta or result.selected here.
Use the ng model for your result.checked e.g.
<Switch [(ngModel)]='isSwitchChecked' (checkedChange)="onSwitchChecked($event)"></Switch>
and in yout .ts
public onSwitchChecked()
alert(this.isSwitchChecked);
I have created playground and you can test functionality there.
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
add a comment |
Mutiple ways to achive the desired
Add the onSwitchChecked
and in your .ts file handle the onSwitchChecked
import Switch from 'ui/switch';
public onSwitchChecked(args) {
const sortSwitch = <Switch>args.object;
if (sortSwitch.id === 'id of your swich')
if (sortSwitch.checked)
// Change the _coursesmeta or result.selected here.
Use the ng model for your result.checked e.g.
<Switch [(ngModel)]='isSwitchChecked' (checkedChange)="onSwitchChecked($event)"></Switch>
and in yout .ts
public onSwitchChecked()
alert(this.isSwitchChecked);
I have created playground and you can test functionality there.
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
add a comment |
Mutiple ways to achive the desired
Add the onSwitchChecked
and in your .ts file handle the onSwitchChecked
import Switch from 'ui/switch';
public onSwitchChecked(args) {
const sortSwitch = <Switch>args.object;
if (sortSwitch.id === 'id of your swich')
if (sortSwitch.checked)
// Change the _coursesmeta or result.selected here.
Use the ng model for your result.checked e.g.
<Switch [(ngModel)]='isSwitchChecked' (checkedChange)="onSwitchChecked($event)"></Switch>
and in yout .ts
public onSwitchChecked()
alert(this.isSwitchChecked);
I have created playground and you can test functionality there.
Mutiple ways to achive the desired
Add the onSwitchChecked
and in your .ts file handle the onSwitchChecked
import Switch from 'ui/switch';
public onSwitchChecked(args) {
const sortSwitch = <Switch>args.object;
if (sortSwitch.id === 'id of your swich')
if (sortSwitch.checked)
// Change the _coursesmeta or result.selected here.
Use the ng model for your result.checked e.g.
<Switch [(ngModel)]='isSwitchChecked' (checkedChange)="onSwitchChecked($event)"></Switch>
and in yout .ts
public onSwitchChecked()
alert(this.isSwitchChecked);
I have created playground and you can test functionality there.
answered Nov 15 '18 at 6:34
Narendra MongiyaNarendra Mongiya
1,6851720
1,6851720
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
add a comment |
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Thanks Narendra for the help. My assumption of Observable Array was a two way automatic binding. i.e. Data from API will get bind to Switch state in .html and vice versa user changes of switch slide will automatically reflect into result.selected value of observable array. There is documentation for two way data binding, then why do we need to manually check id of switch that is pressed and then modify the result.selected? Though, meantime will try your solution Narendra.
– Pratik
Nov 15 '18 at 7:27
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
Narendra, Thanks a lot!! It works great. !!! I used option 2.
– Pratik
Nov 15 '18 at 13:12
add a comment |
I wouldn't recommend using dynamic IDs. Also when using ListView, it's not guaranteed that all your Switches / templates are alive, as you scroll up and down same element will be recycled with different data to retain performance.
So you should play with data here, bind the checked state of Switch to a property in data item. For example
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch>
Now you will just have to loop through the array to find which item is selected.
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
add a comment |
I wouldn't recommend using dynamic IDs. Also when using ListView, it's not guaranteed that all your Switches / templates are alive, as you scroll up and down same element will be recycled with different data to retain performance.
So you should play with data here, bind the checked state of Switch to a property in data item. For example
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch>
Now you will just have to loop through the array to find which item is selected.
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
add a comment |
I wouldn't recommend using dynamic IDs. Also when using ListView, it's not guaranteed that all your Switches / templates are alive, as you scroll up and down same element will be recycled with different data to retain performance.
So you should play with data here, bind the checked state of Switch to a property in data item. For example
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch>
Now you will just have to loop through the array to find which item is selected.
I wouldn't recommend using dynamic IDs. Also when using ListView, it's not guaranteed that all your Switches / templates are alive, as you scroll up and down same element will be recycled with different data to retain performance.
So you should play with data here, bind the checked state of Switch to a property in data item. For example
<Switch [checked]="result.selected" class="m-15" row="0" col="1" ></Switch>
Now you will just have to loop through the array to find which item is selected.
answered Nov 14 '18 at 9:31
ManojManoj
6,6312923
6,6312923
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
add a comment |
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
API provides dynamic data e.g. course id & course name Will your code create Switch dynamically? I have used dynamic IDs several thousands of times in web development, but not sure how it goes here in NS?
– Pratik
Nov 14 '18 at 9:36
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Listview takes an array of data items as input and renders items for each based on the item template you pass in. My code was just psuedo to showcase how you bind the checked property directly instead of I'd. Read more about ListView in the official docs.
– Manoj
Nov 14 '18 at 11:14
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Manoj, thanks for the correct input. I took a while to understand your suggestion. Now I am able to do it but console.log does not reflect user updated state of Switch. Even when all Switches are slide to off state, console says true. As long as I feel, I am not doing anything wrong in ObservableArray. Any further clue will help me call it a day !!!
– Pratik
Nov 14 '18 at 12:52
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Would you mind posting the full example code?
– Manoj
Nov 14 '18 at 12:54
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
Just added code in the question.
– Pratik
Nov 14 '18 at 13:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296561%2fnativescript-array-of-switch-elements-tricks-to-read-value-in-tap-of-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown