Javascript Datepicker ajax调用beforeShow;不起作用
我有一个显示数据库中可用日期的页面。它工作得很好,但我必须刷新页面才能让它工作。如果我没有,datepicker类就像一个文本框。此外,我需要它在显示之前进行ajax调用 总之,我转到“/tasks/new”。页面加载正确。我在datepicker字段中单击,没有日历。控制台中没有错误。我刷新页面,它就工作了。我需要做什么?我最初的想法是需要等待页面加载,但这似乎不起作用(除非我做错了) 以下是相关代码: htmlJavascript Datepicker ajax调用beforeShow;不起作用,javascript,jquery,coffeescript,datepicker,Javascript,Jquery,Coffeescript,Datepicker,我有一个显示数据库中可用日期的页面。它工作得很好,但我必须刷新页面才能让它工作。如果我没有,datepicker类就像一个文本框。此外,我需要它在显示之前进行ajax调用 总之,我转到“/tasks/new”。页面加载正确。我在datepicker字段中单击,没有日历。控制台中没有错误。我刷新页面,它就工作了。我需要做什么?我最初的想法是需要等待页面加载,但这似乎不起作用(除非我做错了) 以下是相关代码: html tasks.js.coffee full_days =[] partial_
tasks.js.coffee
full_days =[]
partial_days=[]
getDays = (date) ->
mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear()
jqxhr = $.get("/getdates/"+mmddyyyy, (data) ->
full_days = data['full']
partial_days = data['partial']
formatDays
return
)
formatDays = (date) ->
mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear()
if $.inArray(mmddyyyy, full_days) isnt -1
[
true
"full-day"
"Available"
]
else
if $.inArray(mmddyyyy, partial_days) isnt -1
[
true
"partial-day"
"Available"
]
else
[
false
""
"unAvailable"
]
# manipulate the json to make an array
availableHours = (day) ->
hours =[]
for index, hour_obj of day
hours.push hour_obj['hour']
return hours
$ ->
$('.datepicker').datepicker(
dateFormat: 'mm-dd-yy'
beforeShow: -> getDays
beforeShowDay: formatDays
onChangeMonthYear: (year, month, inst) ->
target = "#{month}-01-#{year}"
jqxhr = $.get("/getdates/"+target, (data) ->
full_days = data['full']
partial_days = data['partial']
formatDays
return
)
onSelect: (selectedDay) ->
box_id = $(this).attr('id')
box_id = "\##{(box_id.split '_')[1]}_hour"
new_list_items =[]
jqxhr2 = $.get('/gethours/'+selectedDay, (data)->
hours = availableHours(data)
for k,hour of hours
new_list_items.push '<option>'+hour+'</option>'
$(box_id).html('').append(new_list_items)
)
)
full_days=[]
部分天数=[]
getDays=(日期)->
mmddyyyy=(“0”+(date.getMonth()+1)).slice(-2)+“--”+(“0”+date.getDate()).slice(-2)+“-”+date.getFullYear()
jqxhr=$.get(“/getdates/”+mmddyyyy,(数据)->
完整天数=数据['full']
部分天数=数据['partial']
形成期
返回
)
formatDays=(日期)->
mmddyyyy=(“0”+(date.getMonth()+1)).slice(-2)+“--”+(“0”+date.getDate()).slice(-2)+“-”+date.getFullYear()
如果$.inArray(mmddyyyy,full_days)不是-1
[
真的
“全天”
“可用”
]
其他的
如果$.inArray(mmddyyy,部分天)不是-1
[
真的
“部分日”
“可用”
]
其他的
[
假的
""
“不可用”
]
#操纵json以生成数组
可用时数=(天)->
小时数=[]
对于索引,一天中的小时数
hours.push hour_obj['hour']
返程时间
$ ->
$('.datepicker').datepicker(
日期格式:“mm-dd-yy”
展会前:->getDays
展示日前:3天
变更月份:(年、月、月)->
target=“#{month}-01-#{year}”
jqxhr=$.get(“/getdates/”+target,(数据)->
完整天数=数据['full']
部分天数=数据['partial']
形成期
返回
)
onSelect:(selectedDay)->
box_id=$(this.attr('id'))
box_id=“\##{(box#id.split'\')[1]}\u小时”
新列表项目=[]
jqxhr2=$.get('/gethours/'+selectedDay,(数据)->
小时数=可用小时数(数据)
对于k,小时中的小时
新列表项目。推送“”+小时+“”
$(框id).html(“”).append(新列表项)
)
)
我找到了解决问题的方法。这一切都是关于我何时调用函数以及何时放置初始值$->
:
$ ->
full_days = []
partial_days = []
getDays = (date) ->
if (typeof date != 'string')
date = new Date()
date = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear()
jqxhr = $.get("/getdates/" + date, (data) ->
full_days = data['full']
partial_days = data['partial']
formatDays
)
return
getDays()
formatDays = (date) ->
mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear()
if $.inArray(mmddyyyy, full_days) isnt -1
[
true
"full-day"
"Available"
]
else
if $.inArray(mmddyyyy, partial_days) isnt -1
[
true
"partial-day"
"Available"
]
else
[
false
""
"unAvailable"
]
# manipulate the json to make an array
availableHours = (day) ->
hours = []
for index, hour_obj of day
hours.push hour_obj['hour']
return hours
showdatepicker = ->
$('.datepicker').datepicker(
dateFormat: 'mm-dd-yy'
beforeShowDay: formatDays
onChangeMonthYear: (year, month, inst)->
target = "#{month}-01-#{year}"
getDays target
onSelect: (selectedDay) ->
getDays()
box_id = $(this).attr('id')
box_id = "\##{(box_id.split '_')[1]}_hour"
new_list_items = []
jqxhr2 = $.get('/gethours/' + selectedDay, (data)->
hours = availableHours(data)
for k,hour of hours
new_list_items.push '<option>' + hour + '</option>'
$(box_id).html('').append(new_list_items)
)
)
showdatepicker()
$->
完整天数=[]
部分天数=[]
getDays=(日期)->
如果(日期类型!=“字符串”)
日期=新日期()
日期=(“0”+(date.getMonth()+1)).slice(-2)+“--”+(“0”+date.getDate()).slice(-2)+“-”+date.getFullYear()
jqxhr=$.get(“/getdates/”+日期,(数据)->
完整天数=数据['full']
部分天数=数据['partial']
形成期
)
返回
getDays()
formatDays=(日期)->
mmddyyyy=(“0”+(date.getMonth()+1)).slice(-2)+“--”+(“0”+date.getDate()).slice(-2)+“-”+date.getFullYear()
如果$.inArray(mmddyyyy,full_days)不是-1
[
真的
“全天”
“可用”
]
其他的
如果$.inArray(mmddyyy,部分天)不是-1
[
真的
“部分日”
“可用”
]
其他的
[
假的
""
“不可用”
]
#操纵json以生成数组
可用时数=(天)->
小时数=[]
对于索引,一天中的小时数
hours.push hour_obj['hour']
返程时间
showdatepicker=->
$('.datepicker').datepicker(
日期格式:“mm-dd-yy”
展示日前:3天
变更月份:(年、月、月)->
target=“#{month}-01-#{year}”
getDays目标
onSelect:(selectedDay)->
getDays()
box_id=$(this.attr('id'))
box_id=“\##{(box#id.split'\')[1]}\u小时”
新列表项目=[]
jqxhr2=$.get('/gethours/'+selectedDay,(数据)->
小时数=可用小时数(数据)
对于k,小时中的小时
新列表项目。推送“”+小时+“”
$(框id).html(“”).append(新列表项)
)
)
showdatepicker()
是beforeShow:->getDays
应该是beforeShow:getDays
还是beforeShow:->getDays()
?这帮助我解决了部分问题。谢谢
$ ->
full_days = []
partial_days = []
getDays = (date) ->
if (typeof date != 'string')
date = new Date()
date = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear()
jqxhr = $.get("/getdates/" + date, (data) ->
full_days = data['full']
partial_days = data['partial']
formatDays
)
return
getDays()
formatDays = (date) ->
mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear()
if $.inArray(mmddyyyy, full_days) isnt -1
[
true
"full-day"
"Available"
]
else
if $.inArray(mmddyyyy, partial_days) isnt -1
[
true
"partial-day"
"Available"
]
else
[
false
""
"unAvailable"
]
# manipulate the json to make an array
availableHours = (day) ->
hours = []
for index, hour_obj of day
hours.push hour_obj['hour']
return hours
showdatepicker = ->
$('.datepicker').datepicker(
dateFormat: 'mm-dd-yy'
beforeShowDay: formatDays
onChangeMonthYear: (year, month, inst)->
target = "#{month}-01-#{year}"
getDays target
onSelect: (selectedDay) ->
getDays()
box_id = $(this).attr('id')
box_id = "\##{(box_id.split '_')[1]}_hour"
new_list_items = []
jqxhr2 = $.get('/gethours/' + selectedDay, (data)->
hours = availableHours(data)
for k,hour of hours
new_list_items.push '<option>' + hour + '</option>'
$(box_id).html('').append(new_list_items)
)
)
showdatepicker()