效果图:
一:实现年份数组
function generateYearArray(startYear, endYear) {
const yearArray = [];
for (let year = startYear; year <= endYear; year++) {
yearArray.push(year+'年');
}
return yearArray;
}
二,设置年份区间(1995,2024)
@State select: number = 0//选中项在选择范围数组中的索引值
@State generateYear: string [] = []
aboutToAppear(){
this.generateYear = generateYearArray(1995, 2024);
}
三:绑定年份数组到选择器
// 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
TextPickerDialog.show({
range:this.generateYear,
selected: this.select,
onAccept:(value: TextPickerResult)=>{
this.select = value.index
}
})