简体   繁体   English

如何在简单的Rails应用程序中使用JQuery datepicker?

[英]How to use a JQuery datepicker in a simple Rails app?

I am a newbie when it comes to Ruby On Rails (and web programming in general). 我是Ruby On Rails(和一般的Web编程)的新手。 I come from a typical desktop programming background. 我来自典型的桌面编程背景。 I have written a couple simple rails applications, but this is my first try at using Rails3, and my first usage of jQuery. 我已经编写了几个简单的Rails应用程序,但这是我第一次尝试使用Rails3,也是我第一次使用jQuery。

I am having trouble understanding how to connect my jQuery datepicker to my Rails application. 我无法理解如何将jQuery datepicker连接到我的Rails应用程序。 I am sure that I am just not understanding something basic, so please correct anything you see wrong in my code, or point me to a good reference explaining this. 我确信我只是不了解一些基本知识,因此请更正您在代码中发现的错误,或者为我提供解释此问题的良好参考。

My application displays data from the database using the index function, without any modification from the default rails code. 我的应用程序使用索引功能显示了数据库中的数据,而没有对默认的rails代码进行任何修改。 In the sidebar, I put 2 jQuery datepickers that I want to use to filter this data based on the date. 在边栏中,我放入了2个jQuery datepicker,它们用于根据日期过滤此数据。 I want the page to update when I select the date in the date picker, not when I click some button in a form. 我希望在日期选择器中选择日期时更新页面,而不是单击表单中的某些按钮时更新页面。

The datepickers show up fine, but I can't figure out how to correctly connect them to my rails application. 日期选择器显示正常,但我不知道如何正确将它们连接到我的Rails应用程序。 Here is the code: 这是代码:

<script type="text/javascript">
    $(function() {
        $("#from_filter").datepicker({
            showOn: 'both',
            buttonImage: 'images/calendar.gif',
            buttonImageOnly: true,
            onSelect: function(date,inst) {
                    }
        });

        $("#to_filter").datepicker({
            showOn: 'both',
            buttonImage: 'images/calendar.gif',
            buttonImageOnly: true,
            onSelect: function(date,inst) {
                    }
        });
    });
</script>
<div id="sidebarwindow">
    <div id="sidebarwindowheader">Date Filter</div>
    <table id="hor-zebra">
        <tr>
            <td><label for="from_filter">From:</label></td>
            <td><div id="datepicker"><%= text_field_tag 'from_filter' %></div></td>
        </tr>
        <tr>
            <td><label for="to_filter">To:</label></td>
            <td><div id="datepicker"><%= text_field_tag 'to_filter' %></div></td>
        </tr>
    </table>
</div>

I have googled quite a bit, but I get lost quickly as most examples of jQuery -> Rails seem to focus on using forms, and here I don't want to. 我已经在Google上搜索了很多,但是由于大多数jQuery-> Rails似乎都专注于使用表单,因此我很快就迷路了,在这里我不想。 I verified with an alert() that the onSelect() is working correctly, but I don't know how to call my Rails controller from that function. 我用alert()验证了onSelect()是否正常工作,但是我不知道如何从该函数调用我的Rails控制器。

Basically, I want to choose a date, and then re-render the partials on the page that I know need to be updated. 基本上,我想选择一个日期,然后在我知道需要更新的页面上重新渲染部分内容。 Maybe I just dont fully understand AJAX/jQuery/Rails? 也许我只是不完全了解AJAX / jQuery / Rails?

Basically your problem is that you don't know what to put inside the OnSelect function. 基本上,您的问题是您不知道在OnSelect函数中放置什么。

In there I would call the ajax jQuery function to call your controller, and then use the callback function to add the HTML code needed. 在这里,我将调用ajax jQuery函数来调用您的控制器,然后使用回调函数添加所需的HTML代码。

$.ajax({ url: "/mycontroller/myaction", context: document.body, success: function(){
        $(this).addClass("done");
      }});

More info: 更多信息:

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

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