简体   繁体   English

rails 3 jQuery UI Datepicker只想启用特定日期

[英]rails 3 jQuery UI Datepicker want to enable only specific dates

I have a Registration model and form which uses a datepicker. 我有一个注册模型和使用日期选择器的表格。 I want the user to be only able to select dates which correspond to events (another model). 我希望用户只能选择与事件相对应的日期(另一个模型)。

My problem is I can't find the right way to pass the event array to javascript. 我的问题是我找不到将事件数组传递给javascript的正确方法。

this is in the controller: 这是在控制器中:

@available_dates = Event.pluck(:start_time).as_json

this is in the view: 这是在视图中:

    <script>
       var availableDates = [<%= @available_dates.to_s.html_safe %>] ;
    </script>

and this is the js: 这是js:

function available(date) {
    ymd = date.getFullYear() + "-" + ('0' + (date.getMonth()+1)).slice(-2) + "-" + ('0' + date.getDate()).slice(-2);
    console.log(ymd+' : '+($.inArray(ymd, availableDates)));
    if ($.inArray(ymd, availableDates) != -1) {
        return [true, "","Available"];
    } else {
        return [false,"","unAvailable"];
    }
}
$("#registration_date").datepicker({ beforeShowDay: available, dateFormat: "yy-mm-dd"});

I think I'm doing something wrong in the view since the js array seems to be empty looking at the console... 我认为我在视图中做错了,因为从控制台看js数组似乎是空的...

any help would be greatly appreciated 任何帮助将不胜感激

In the question comments you have mentioned @available_dates.to_s returns "[\\"2013-01-05\\", \\"2013-02-02\\", \\"2013-03-02\\"]" . 在您提到的问题注释中, @available_dates.to_s返回"[\\"2013-01-05\\", \\"2013-02-02\\", \\"2013-03-02\\"]" This itself will render the array on the page so no need to "box it up" again. 这本身将在页面上呈现数组,因此无需再次“装箱”。

Try changing the following line: 尝试更改以下行:

var availableDates = [<%= @available_dates.to_s.html_safe %>] ;

to this: 对此:

var availableDates = <%= @available_dates.to_s.html_safe %>;

Hope it helps. 希望能帮助到你。

If it doesn't you are either clearing the value of @available_dates somewhere further in your code or @available_dates is not available in the context of your view. 如果不是,那么您要么在代码中@available_dates地方清除@available_dates的值, @available_dates在视图的上下文中不使用@available_dates Have your tried stepping through the code using debugger; 让您尝试使用debugger;步执行代码debugger; ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM