Can i add a ListView inside a Layout ?
I have this View:
function pageLoaded(args) {
var page = args.object;
var array = new observableArray.ObservableArray();
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
page.bindingContext = vmModule.mainViewModel, {"myItems": array};
};
exports.pageLoaded = pageLoaded;
And this template:
<GridLayout>
<TabView>
<TabView.items>
<TabViewItem title="Nearest Wifi">
<TabViewItem.view>
<GridLayout>
<ListView items="{{ myItems }}">
<ListView.itemTemplate>
<GridLayout columns="*,*" rows="2">
<Label col="0" row="0" text="{{ city || 'Downloading...' }}" textWrap="true" class="title" />
<Label col="1" row="0" text="{{ distance || 'Downloading...' }}" textWrap="true" class="title" />
<Label col="0" row="1" colspan="2" text="Votes: {{nr_votes | 0 }}"/>
</GridLayout>
</ListView.itemTemplate>
</ListView>
</GridLayout>
</TabViewItem.view>
</TabViewItem>
...
I try to make it work for 3 hours .. any help would be apreciated. Thanks!
This code works well, I believe it's what you're after:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
<ActionBar title="Cities - WIFI" />
<TabView>
<TabView.items>
<TabViewItem title="Nearest Wifi">
<TabViewItem.view>
<ListView items="{{ cities }}">
<ListView.itemTemplate>
<GridLayout columns="*,*,*" rows="*,*">
<Label col="0" row="0" text="City"/>
<Label col="1" row="0" text="Distance" />
<Label col="2" row="0" text="Votes" />
<Label col="0" row="1" text="{{city}}" />
<Label col="1" row="1" text="{{distance}}" />
<Label col="2" row="1" text="{{votes}}" />
</GridLayout>
</ListView.itemTemplate>
</ListView>
</TabViewItem.view>
</TabViewItem>
</TabView.items>
</TabView>
</Page>
Javascript:
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
function pageLoaded(args) {
var page = args.object;
page.bindingContext = pageData;
}
exports.pageLoaded = pageLoaded;
var pageData = new Observable({
cities: new ObservableArray([
{city:"Madrid", distance:"0.2km",votes:"0"},
{city:"Madrid", distance:"0.2km",votes:"0"},
{city:"Madrid", distance:"0.2km",votes:"0"}
])
})
This is the way if you want to iterate the data. You can put array.push lines into for loop or wherever you want.
XML:
<ListView items="{{ cities }}">
<ListView.itemTemplate>
<GridLayout columns="*,*,*" rows="*,*">
<Label col="0" row="0" text="City"/>
<Label col="1" row="0" text="Distance" />
<Label col="2" row="0" text="Votes" />
<Label col="0" row="1" text="{{city}}" />
<Label col="1" row="1" text="{{distance}}" />
<Label col="2" row="1" text="{{votes}}" />
</GridLayout>
</ListView.itemTemplate>
</ListView>
JS:
var array = new ObservableArray();
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
array.push({"city":"Madrid","distance":"0.2km","votes":"0"});
var pageData = new Observable({cities: array});
page.bindingContext = pageData;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.