Nativescript + Angular2: RadListView binding - nativescript

I'm attempting to bind an Observable to a RadListView, without using any Page properties, but appear to be doing something completely wrong. The following is a minified version of the code.
The component:
export class WeatherComponent implements OnInit {
public weather : ObservableArray<StringWrapper>;
constructor(private _weatherService : WeatherService) {
this.weather = new ObservableArray<StringWrapper>([]);
this.weather.push(<StringWrapper>{value:'Sunny'});
}
ngOnInit(): void {
this._weatherService.getDummyWeather().subscribe(
item => {
this.weather.push(item);
}
);
}
}
The XML:
<RadListView [items]="weather">
<template tkListItemTemplate let-item="item">
<StackLayout orientation="vertical">
<Label [text]="item.value"></Label>
</StackLayout>
</template>
</RadListView>
The simple data model:
export interface StringWrapper {
value : string;
}
The service:
#Injectable()
export class WeatherService {
public getDummyWeather() : Observable<StringWrapper> {
return Observable.of(<StringWrapper>{value:'Rainy'});
}
}
The service is correctly updating the model but the view is not reflecting the changes, leading me to believe the problem is located in the observable binding.
Any help would be deeply appreciated!
N.B. Checked related questions and none of the solutions helped, i.e. Nativescript RadListView not binding to source property solution causes a build error.

Change the HTML to
<RadListView [items]="weather" >
<template tkListItemTemplate let-item="item">
<StackLayout class="itemStackLayout" >
<Label class="titleLabel" [text]="item.value" textWrap="true" width="100%"></Label>
</StackLayout>
</template>
</RadListView>
Hint: Seems like stringwrapper is not need. You can use below code if it just a string array
<Label class="titleLabel" [text]="item" textWrap="true" width="100%"></Label>

Related

NativeScript/Angular - Reactive form not working when using actionbar

First I implemented the code below.
app.component.html
<StackLayout [formGroup]="form">
<label text="Name"></label>
<TextField formControlName="Name"></TextField>
<label text="Last Name"></label>
<TextField formControlName="LastName"></TextField>
<button text="save" (tap)="save()"></button>
</StackLayout>
app.component.ts
public form: FormGroup;
constructor(private fb: FormBuilder){
this.form = this.fb.group({
"Name": ["", [Validators.required]],
"LastName": ["", [Validators.required]]
});
}
public save(){
console.log(JSON.stringify(this.form.value));
}
When I run the code above, it's everything alright. I get name and lastname correctly.
The problem occurs when I try to add an action bar to this code.
app.component.html
<ActionBar title="New" class="action-bar">
<ActionItem text="save" (tap)="save()"></ActionItem>
</ActionBar>
<StackLayout [formGroup]="form">
<label text="Name"></label>
<TextField formControlName="Name"></TextField>
<label text="Last Name"></label>
<TextField formControlName="LastName"></TextField>
<button text="save" (tap)="save()"></button>
</StackLayout>
The app.component.ts remains the same.
When I run this code and tap the button inside stacklayout I get name and lastname correctly but when I tap the ActionItem I get an empty string for both name and lastname. Am I missing something?
Your code looks just fine - in fact I have re=-tested it with this test application and everything works as expected on my side.
Side note: If testing on iOS keep in mind that subsequent console logs can be printed only once if they are identical. (this) so it might be that it appears that the log is not printed when in fact the action is done.

NativeScript - Passing content from Parent to Child component

I've been searching for this all over during the last 2 days so I decided to ask for help.
Imagine we have a parent component called ParentComponent and we have also a child component called SomeComponent.
SomeComponent template would be:
import {Component} from "#angular/core";
#Component({
selector: "SomeComponent",
template: `
<ActionBar title="TestApp">
</ActionBar>
<StackLayout style="margin-top:20;">
<Label text="Somenthing on top"></Label>
#CONTAINER CONTENT HERE#
<Label text="Something in the bottom"></Label>
</StackLayout>
`,
})
export class SomeComponent {}
..and ParentComponent template would be:
import {Component} from "#angular/core";
import {SomeComponent} from "../some/where/...";
#Component({
selector: "parent",
template: `
<SomeComponent>
<Label text="Something here"></Label>
<Label text="Something else here"></Label>
</SomeComponent>
`,
})
export class ParentComponent {}
Considering the aforementioned example, how can I get the content inside "< SomeComponent >" defined in my ParentComponent, to be displayed properly in the SomeComponent in the reserved "#CONTAINER CONTENT HERE#" area?
In theory it is as if I would end up with something like this:
<ActionBar title="TestApp">
</ActionBar>
<StackLayout style="margin-top:20;">
<Label text="Somenthing on top"></Label>
<Label text="Something here"></Label>
<Label text="Something else here"></Label>
<Label text="Something in the bottom"></Label>
</StackLayout>
It looks like something pretty simple that I used to do in react native, that I can't get to work on NS.
Thanks in advance.
You can use a ng-content tag to transclude the content from the parent container to the child. I believe all you need to add ng-content to your SomeContent component, which will then look like:
import {Component} from "#angular/core";
#Component({
selector: "SomeComponent",
template: `
<ActionBar title="TestApp">
</ActionBar>
<StackLayout style="margin-top:20;">
<Label text="Somenthing on top"></Label>
<ng-content></ng-content>
<Label text="Something in the bottom"></Label>
</StackLayout>
`,
})
export class SomeComponent {}
You can read more about transclusion here https://toddmotto.com/transclusion-in-angular-2-with-ng-content
Also you can see a working example inside of the slides plugin I wrote https://github.com/TheOriginalJosh/nativescript-ngx-slides/blob/master/slides/app/slides/slides.component.ts#L40

Nativescript Listview with header and scroll entire page

I am using ListView with Header portion on top of it like below,
<StackLayout>
<StackLayout height="200">
<Label text="Header content goes in this section"></Label>
<StackLayout>
<ListView [items]='posts'>
<!-- template items goes here -->
</ListView>
</StackLayout>
When we scroll to list the header is sticky in this case.
Is there a option that scroll overrides header also ?.I mean that header also part of scroll.
Fr Angular-2 application you can now use tkTemplateKey deirective and create your own headers, footers, groups and other custom list-view elements.
Example can be found here
Here is the code for a list-view with header and groups.
page.component.html
<ListView [items]="countries" [itemTemplateSelector]="templateSelector" (itemTap)="onItemTapFirstList($event)" class="list-group" separatorColor="white">
<ng-template nsTemplateKey="header" let-header="item">
<Label [text]="header.name" class="list-group-item h3 bg-primary" isUserInteractionEnabled="false" color="white" fontSize="24"></Label>
</ng-template>
<ng-template nsTemplateKey="footer" let-footer="item">
<Label [text]="footer.name" class="list-group-item" backgroundColor="gray"></Label>
</ng-template>
<ng-template nsTemplateKey="cell" let-country="item">
<StackLayout class="list-group-item">
<Label [text]="country.name" class="list-group-item-heading"></Label>
<Label [text]="country.desc" class="list-group-item-text" textWrap="true"></Label>
</StackLayout>
</ng-template>
</ListView>
page.component.ts
#Component({
moduleId: module.id,
templateUrl: "./multi-line-grouped.component.html",
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MultiLineGroupedListViewExampleComponent implements OnInit {
public countries: Array<any> = [];
public templateSelector = (item: any, index: number, items: any) => {
return item.type || "cell";
}
ngOnInit() {
for (let i = 0; i < mockedCounties.length; i++) {
this.countries.push(mockedCounties[i]);
}
}
onItemTapFirstList(args: ItemEventData) {
console.log(args.index);
}
}
Not sure if there's another way, but one way could be moving the header inside the listview. For that to work it needs to be in the posts Array, so you may want to transform that into some sort of wrapping class that can contain eiter a header or item row. Then create two templates inside the listview that depending on the template key render a header or an item.
For details on templates, see https://docs.nativescript.org/cookbook/ui/list-view#define-multiple-item-templates-and-an-item-template-selector-in-xml
You can use *ngFor creating the list.Here is the sample code for doing this.
<ScrollView>
<StackLayout>
//define your header over here
<Label text="hey header"></Label>
<StackLayout *ngFor="let item of <Array>">
<GridLayout columns="4*,*" rows="*,">
<Label row="0" col="0" text="hey label"></Label>
</GridLayout>
<StackLayout>
<StackLayout>
</ScollView>

NativeScript: how to make view 'transparent' for any user interactions

Is there a way to make a view 'transparent' to any user interactions? For example I have a view (with transparent background) and a button under that view. I want the user could tap the button under that view. If I have a scroller view under that view I want the user interacts with scroller when scroll over that view, so the view doesn't interfere or intercept user's gestures. But only this view should be transparent to user's interactions not its children. So, if I have a button inside that view it behaves normally.
Example XML:
<AbsoluteLayout width="100%" height="100%">
<Button text="Button1" tap="onTap1" />
<GridLayout width="100%" height="100%" backgroundColor="transparent">
<Button text="Button2" tap="onTap2" horizontalAlignment="center" verticalAlignment="center"/>
</GridLayout>
</AbsoluteLayout>
Thank you for your help.
You have multiple approaches to make a view change its color in NativeScript.
For example you can directly change its backgroundColor. Another oiption is to use animation and third option is to use CSS-animation.
Here is a basic example for the first two options.
page.xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
<StackLayout>
<GridLayout width="300" height="300" id="myGrid" backgroundColor="transparent">
</GridLayout>
<Button text="Tap me" tap="onTap" />
<Button text="Or Tap me" tap="onAnotherTap" />
</StackLayout>
</Page>
page.js
import { EventData } from "data/observable";
import { Page } from "ui/page";
import { HelloWorldModel } from "./main-view-model";
import { GridLayout } from "ui/layouts/grid-layout";
import { Color } from "color";
var myGridView;
export function navigatingTo(args: EventData) {
var page = <Page>args.object;
page.bindingContext = new HelloWorldModel();
// get refference to the view using its id
myGridView = <GridLayout>page.getViewById("myGrid");
}
export function onTap(args:EventData) {
var color = new Color("#FF0000");
myGridView.backgroundColor = color;
}
export function onAnotherTap(args:EventData) {
myGridView.animate({
backgroundColor: new Color("#3D5AFE"),
duration: 3000
});
}
All of the options can be found described in NativeScript documenation

NativeScript 2.0 Custom Components and CSS

I'm having an issue with NativeScript 2.0 CSS and custom components. There seems to be a giant gap in my knowledge and I'm missing something vital that is non-obvious.
Take an empty NS 2.0 app created with
$ tns create test --ng
Delete the contents of app.css (to prevent side effects).
Change app.component.ts:
import {Component} from "#angular/core";
#Component({
selector: "my-app",
template: `
<StackLayout orientation="vertical">
<Label text="Label in first StackLayout"></Label>
<StackLayout orientation="vertical"
style="width: 80%;background-color: red;">
<Label text="Label in second StackLayout"></Label>
</StackLayout>
</StackLayout>
`,
})
export class AppComponent {}
Pretty basic stuff. Produces the following expected result:
Let's try to convert that inner StackLayout into a reusable component.
custom.component.ts
import {Component} from "#angular/core";
#Component({
selector: "Custom",
template: `
<StackLayout orientation="vertical"
style="width: 80%;background-color: red;">
<Label text="Label in second StackLayout"></Label>
</StackLayout>
`,
})
export class CustomComponent {}
Change the app.component.ts
import {Component} from "#angular/core";
import {CustomComponent} from "./custom.component"
#Component({
selector: "my-app",
directives: [CustomComponent],
template: `
<StackLayout orientation="vertical">
<Label text="Label in first StackLayout"></Label>
<Custom></Custom>
</StackLayout>
`,
})
export class AppComponent {}
Now the output looks like this:
The background color is applied but the width is not.
I even tried:
<Custom style="width: 80%;"></Custom> /* Does this even make sense? */
to no avail.
I realize percentages are experimental but suspect the error is in my code rather than NativeScript.
Where did I go wrong?
I reviewed your code in the given code snippet and found that it could be NativeScript issue. At the moment changing the width of the StackLayout in your CustomView using inline style will be working only on Android. To change the width of your CustomView using % for both platform at the moment you should setup this property in your css file and bind cssClass property.
custom.component.ts
import {Component} from "#angular/core";
#Component({
selector: "Custom",
template: `
<StackLayout orientation="vertical"
[cssClass]="styleView" style="background-color: red;">
<Label text="Label in second StackLayout"></Label>
</StackLayout>
`,
})
export class CustomComponent {
public styleView="customViewStyle";
}
app.css
.customViewStyle{
width:80%;
}

Resources