如何从 NativeScript 的日期选择器中获取日期
How to fetch a date from a datepicker on NativeScript
这是我的观点:
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
<StackLayout>
<DatePicker day="15" month="5" year="2015" verticalAlignment="center" id="date"/>
<Label text="Saisissez une heure : " verticalAlignment="center"/>
<TextField verticalAlignment="center" width="300" hint="Enter text..."/>
<Button text="Valider" tap="see"></Button>
</StackLayout>
</Page>
我想知道如何从这个日期选择器中获取日期我试过了:
var viewModule = require("ui/core/view");
var dialogs = require("ui/dialogs");
var page;
exports.onPageLoaded = function(args) {
page = args.object;
};
exports.see = function() {
dialogs.alert(viewModule.getViewById( page, "date" ).year).then(function() {
});
};
但是没有用。
感谢您的回答。
尝试在页面变量上使用 getViewById 方法。
您的 DatePicker 驻留在您的页面变量中,根据文档,该变量是 View class 的派生类型,其中包含 getViewById 方法。
像这样更改您的 "see" 方法应该可行(我自己没有测试):
exports.see = function() {
dialogs.alert(page.getViewById("date").year).then(function() {
});
};
仅供参考 - {N} 自此 post 以来已更新,这不再适用于 1.0+
文档在这里有一个不错的例子:Binding Example
var count = 0;
function buttonTap(args) {
count++;
var sender = args.object;
/// ****IMPORTANT var ****///
var parent = sender.parent;
if (parent) {
///*** ref the parent var above ****///
var lbl = view.getViewById(parent, "Label1");
if (lbl) {
lbl.text = "You tapped " + count + " times!";
}
}
}
exports.buttonTap = buttonTap;
Nativescript和Ang.2的代码如下。请注意该代码适用于模态框内的 2 个日期选择器。
import {Component, OnInit, ElementRef, ViewChild} from "@angular/core";
import {ModalDialogParams} from "nativescript-angular/modal-dialog";
import {DatePicker} from "ui/date-picker";
import {Page} from "ui/page";
import {View} from "ui/core/view";
import {Label} from "ui/label";
import {DialogOptions} from "ui/dialogs";
@Component({
selector: 'modal-content',
template: `
<StackLayout>
<StackLayout margin="24" horizontalAlignment="center" verticalAlignment="center" orientation="vertical">
<Label text="From"></Label>
<DatePicker #datePickerID [year]="dtyear2" month="2" day="2" verticalAlignment="center" horizontalAlignment="center"> </DatePicker>
</StackLayout>
<StackLayout margin="24" horizontalAlignment="center" verticalAlignment="center" orientation="vertical">
<Label text="To"></Label>
<DatePicker #datepicker [year]="dtyear" [month]="dtmonth" [day]="dtday" verticalAlignment="center" horizontalAlignment="center"></DatePicker>
</StackLayout>
<StackLayout orientation="horizontal" horizontalAlignment="center" verticalAlignment="center" marginTop="12">
<Button text="Save" (tap)="close('Save')"></Button>
<Button text="Cancel" (tap)="close('Cancel')"></Button>
</StackLayout>
</StackLayout>
`
})
export class DateControlComponent {
// <DatePicker id="datePicker" [(ngModel)]='datePicker' verticalAlignment="center" horizontalAlignment="center"></DatePicker>
public dtyear = 1970;
public dtmonth = 1;
public dtday = 18;
dtyear2 = 1970;
@ViewChild("datePickerID") datePicker: ElementRef;
//public datePicker: datePickerModule.DatePicker = new datePickerModule.DatePicker();
//prompt: string;
constructor(private params: ModalDialogParams) {
//this.prompt = params.context.promptMsg;
}
ngOnInit() {
}
public close(result: string) {
if (result == "Save") {
let datePickerView = <DatePicker>this.datePicker.nativeElement;
console.log("Year " + this.dtyear);
console.log("Year from NGModel Year " + datePickerView.year);
this.params.closeCallback(new Date(datePickerView.year, datePickerView.month, datePickerView.day));
}
else {
this.params.closeCallback(null);
}
}
}
这是我的观点:
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
<StackLayout>
<DatePicker day="15" month="5" year="2015" verticalAlignment="center" id="date"/>
<Label text="Saisissez une heure : " verticalAlignment="center"/>
<TextField verticalAlignment="center" width="300" hint="Enter text..."/>
<Button text="Valider" tap="see"></Button>
</StackLayout>
</Page>
我想知道如何从这个日期选择器中获取日期我试过了:
var viewModule = require("ui/core/view");
var dialogs = require("ui/dialogs");
var page;
exports.onPageLoaded = function(args) {
page = args.object;
};
exports.see = function() {
dialogs.alert(viewModule.getViewById( page, "date" ).year).then(function() {
});
};
但是没有用。
感谢您的回答。
尝试在页面变量上使用 getViewById 方法。 您的 DatePicker 驻留在您的页面变量中,根据文档,该变量是 View class 的派生类型,其中包含 getViewById 方法。
像这样更改您的 "see" 方法应该可行(我自己没有测试):
exports.see = function() {
dialogs.alert(page.getViewById("date").year).then(function() {
});
};
仅供参考 - {N} 自此 post 以来已更新,这不再适用于 1.0+
文档在这里有一个不错的例子:Binding Example
var count = 0;
function buttonTap(args) {
count++;
var sender = args.object;
/// ****IMPORTANT var ****///
var parent = sender.parent;
if (parent) {
///*** ref the parent var above ****///
var lbl = view.getViewById(parent, "Label1");
if (lbl) {
lbl.text = "You tapped " + count + " times!";
}
}
}
exports.buttonTap = buttonTap;
Nativescript和Ang.2的代码如下。请注意该代码适用于模态框内的 2 个日期选择器。
import {Component, OnInit, ElementRef, ViewChild} from "@angular/core";
import {ModalDialogParams} from "nativescript-angular/modal-dialog";
import {DatePicker} from "ui/date-picker";
import {Page} from "ui/page";
import {View} from "ui/core/view";
import {Label} from "ui/label";
import {DialogOptions} from "ui/dialogs";
@Component({
selector: 'modal-content',
template: `
<StackLayout>
<StackLayout margin="24" horizontalAlignment="center" verticalAlignment="center" orientation="vertical">
<Label text="From"></Label>
<DatePicker #datePickerID [year]="dtyear2" month="2" day="2" verticalAlignment="center" horizontalAlignment="center"> </DatePicker>
</StackLayout>
<StackLayout margin="24" horizontalAlignment="center" verticalAlignment="center" orientation="vertical">
<Label text="To"></Label>
<DatePicker #datepicker [year]="dtyear" [month]="dtmonth" [day]="dtday" verticalAlignment="center" horizontalAlignment="center"></DatePicker>
</StackLayout>
<StackLayout orientation="horizontal" horizontalAlignment="center" verticalAlignment="center" marginTop="12">
<Button text="Save" (tap)="close('Save')"></Button>
<Button text="Cancel" (tap)="close('Cancel')"></Button>
</StackLayout>
</StackLayout>
`
})
export class DateControlComponent {
// <DatePicker id="datePicker" [(ngModel)]='datePicker' verticalAlignment="center" horizontalAlignment="center"></DatePicker>
public dtyear = 1970;
public dtmonth = 1;
public dtday = 18;
dtyear2 = 1970;
@ViewChild("datePickerID") datePicker: ElementRef;
//public datePicker: datePickerModule.DatePicker = new datePickerModule.DatePicker();
//prompt: string;
constructor(private params: ModalDialogParams) {
//this.prompt = params.context.promptMsg;
}
ngOnInit() {
}
public close(result: string) {
if (result == "Save") {
let datePickerView = <DatePicker>this.datePicker.nativeElement;
console.log("Year " + this.dtyear);
console.log("Year from NGModel Year " + datePickerView.year);
this.params.closeCallback(new Date(datePickerView.year, datePickerView.month, datePickerView.day));
}
else {
this.params.closeCallback(null);
}
}
}