如何使用 Autoform 以时间格式显示数字?
How can I display a number in time format using Autoform?
我目前正在使用 Autoform 在 Meteor 中制作一个表单,并试图让用户输入他们想要用于事件的时间限制。我当前的架构看起来像下面的示例,但我只是想知道如何更改 timeLimit 对象,以便它不会只显示格式为“0”的数字,而是 return 格式为“00:00”和让我能够在几秒钟内记录信息。
EventSchema = new SimpleSchema({
name: {
type: String,
label: "Event Name"
},
timeLimit: {
type: Number,
label: "Time Limit"
},
rounds: {
type: Number,
label: "Rounds"
}
});
一种方法是为用户输入一个字段,它是 "mm:ss" 格式的字符串。另一个计算并显示秒数的字段。
所以在本例中,您将在表单上显示 timeLimitString 字段,以便用户可以输入 "mm:ss" 格式。并在 timeLimit 字段上使用 autoValue 函数来计算秒数(本例使用 moment.js 库来计算秒数);
EventSchema = new SimpleSchema({
name: {
type: String,
label: "Event Name"
},
timeLimitString: {
type: String,
regEx: // define regex here for "mm:ss" format
},
timeLimit: {
type: Number,
label: "Time Limit",
autoValue: function() {
var string = this.field("timeLimitString");
if (string.isSet) {
var time = moment(string.value, "mm:ss");
var seconds = time.minutes()*60 + time.seconds();
return seconds;
}
}
},
rounds: {
type: Number,
label: "Rounds"
}
});
我目前正在使用 Autoform 在 Meteor 中制作一个表单,并试图让用户输入他们想要用于事件的时间限制。我当前的架构看起来像下面的示例,但我只是想知道如何更改 timeLimit 对象,以便它不会只显示格式为“0”的数字,而是 return 格式为“00:00”和让我能够在几秒钟内记录信息。
EventSchema = new SimpleSchema({
name: {
type: String,
label: "Event Name"
},
timeLimit: {
type: Number,
label: "Time Limit"
},
rounds: {
type: Number,
label: "Rounds"
}
});
一种方法是为用户输入一个字段,它是 "mm:ss" 格式的字符串。另一个计算并显示秒数的字段。
所以在本例中,您将在表单上显示 timeLimitString 字段,以便用户可以输入 "mm:ss" 格式。并在 timeLimit 字段上使用 autoValue 函数来计算秒数(本例使用 moment.js 库来计算秒数);
EventSchema = new SimpleSchema({
name: {
type: String,
label: "Event Name"
},
timeLimitString: {
type: String,
regEx: // define regex here for "mm:ss" format
},
timeLimit: {
type: Number,
label: "Time Limit",
autoValue: function() {
var string = this.field("timeLimitString");
if (string.isSet) {
var time = moment(string.value, "mm:ss");
var seconds = time.minutes()*60 + time.seconds();
return seconds;
}
}
},
rounds: {
type: Number,
label: "Rounds"
}
});