简体   繁体   English

如何正确设置按钮附带的日期选择器

[英]How to properly set up datepicker attached to a button

I am currently working on a Django project using Grappelli and I am trying to create a page that mimics a standard change form. 我目前正在使用Grappelli进行Django项目,并且正在尝试创建一个模仿标准更改表单的页面。 The page is not based on any model, so I am simply adding in all the fields myself. 该页面不是基于任何模型的,因此我只是自己在所有字段中添加。 However, I am having trouble using the datepicker. 但是,我在使用日期选择器时遇到了麻烦。 Whenever I click the button that is supposed to show the calendar, nothing happens. 每当我单击应该显示日历的按钮时,都不会发生任何事情。 I am sure I am simply not including something important. 我敢肯定,我根本没有包括一些重要的内容。

Here is my current code: 这是我当前的代码:

{% extends "admin/change_form.html" %}

{% block javascripts %}
{{ block.super }}
{% endblock %}

{% block content %}

    <div id="grp-content-container">
        <form enctype="multipart/form-data" action method="post">
            {% csrf_token %}
            <div>
                <fieldset class="module grp-module">
                    <div class="form-row grp-row grp-cells-1 dob">
                        <div class="field-box l-2c-fluid l-d-4">
                            <div class="c-1">
                                <label class="required" for="dob">Birthday</label>
                            </div>
                            <div class="c-2">
                                <p class="datetime">
                                    <input class="vDateField hasDatepicker" type="text" id="dob" name="dob">
                                    <button id="dob-button" type="button" class="ui-datepicker-trigger"></button>
                                </p>
                            </div>
                        </div>
                    </div>
               </fieldset>
            </div>
        </form>
    </div>
{% endblock %}

In the javascripts block, I had attempted to add in the datepicker source functionality as taken from this website http://jqueryui.com/datepicker/ , but that did not seem to have any effect. 在javascripts块中,我试图添加从该网站http://jqueryui.com/datepicker/取得的datepicker源功能,但这似乎没有任何效果。 Any suggestions? 有什么建议么?

I've done it before couple of times and want to tell that with grappelli it works almost out of the box. 我已经做过几次了,想告诉我使用grappelli几乎可以立即使用。

The point of interest: 兴趣点:

grappelli.js with this lines: grappelli.js具有以下grappelli.js行:

var dateFields = $("input[class*='vDateField']:not([id*='__prefix__'])");
dateFields.datepicker(options);

And grappelli.reinitDateTimeFields function which reinits this. grappelli.reinitDateTimeFields函数将其重新初始化。

So basically all you have to do is to put vDateField or/and vTimeField class to your input field and make sure that grappelli.js loaded properly and grappelli.initDateAndTimePicker() being called when document is ready. 因此,基本上您要做的就是将vDateField或/和vTimeField类放入输入字段,并确保正确加载了grappelli.js ,并在文档准备就绪时调用了grappelli.initDateAndTimePicker() Then buttons with datepicker should load themselves automatically. 然后带有日期选择器的按钮应自动加载。

You can be sure that grappelli.initDateAndTimePicker() being called as any other JS-file: just put debugger; 你可以肯定的是grappelli.initDateAndTimePicker()被调用任何其他JS文件:只要把debugger; somewhere in initDateAndTimePicker constructor in grappelli.js and then it must pause in your js-inspector. initDateAndTimePickerinitDateAndTimePicker构造函数中的某个位置,然后它必须在您的js检查器中暂停。

Hope that'll help, it works for me. 希望能对您有所帮助。

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

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