Grappelli管理界面中datepicker的星期开始

时间:2016-05-09 14:05:48

标签: django jquery-ui-datepicker django-grappelli

Grappelli默认datepicker是星期日作为一周的第一天。这真是太刺激了!我希望星期一成为我现在和未来模特的一周的第一天。

有办法做到这一点吗?

到目前为止,我发现唯一的“solution”涉及更改模型的Media类。但是,此解决方案似乎不能像以下一样工作:

class Monkey(ImportExportActionModelAdmin):

    class Media:
        js = ("static/i18n/ui.datepicker-en-GB.js")

    ...

static/i18n/ui.datepicker-en-GB.js

的位置
(function($) {
    // datepicker, timepicker init
    grappelli.initDateAndTimePicker = function() {

        var options = {
            closeText: "Done",
            prevText: "Prev",
            nextText: "Next",
            currentText: "Today",
            monthNames: [ "January","February","March","April","May","June",
            "July","August","September","October","November","December" ],
            monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
            dayNames: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
            dayNamesShort: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
            dayNamesMin: [ "Su","Mo","Tu","We","Th","Fr","Sa" ],
            weekHeader: "Wk",
            dateFormat: "dd/mm/yy",
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ""
        };

} )(grp.jQuery);

在任何情况下,此解决方案都是次优的,因为使用日期的任何新模型都必须定义相同的Meta类。

2 个答案:

答案 0 :(得分:1)

解决方案的第一部分是将另一个JavaScript文件添加到管理视图中,如@Sardathrion已经描述的那样:

class MyModelAdmin(ModelAdmin):
    class Media:
        js = ('js/grappellihacks.js',)

第二部分是重新配置渲染的datepicker小部件。由于它们具有公共类属性,因此 grappellihacks.js 中的此代码应该足够了:

grp.jQuery(function() {
    grp.jQuery('.hasDatepicker').datepicker('option', 'firstDay', 1);
});

该选项在http://developer.android.com/reference/android/widget/ListView.html#setSelection%28int%29中有所描述。 grp 属性由jQuery UI documentation设置,由管理员模板在自定义媒体文件之前加载。

答案 1 :(得分:0)

上面的代码中确实存在一些错误。

首先,AdminModel应为

class Monkey(ImportExportActionModelAdmin):

    class Media:
        js = ("i18n/ui.datepicker-en-GB.js", )

    ...

由于自动附加静态目录且js变量必须是元组 - 请注意逗号。

其次,我使用的javascrpt是static/grappelli/js/grappelli.js一个的复制粘贴,其中选项firstDay: 1,添加到options变量。它读起来像这样:

(function($) {
    grappelli.initDateAndTimePicker = function() {

        // HACK: get rid of text after DateField (hardcoded in django.admin)
        $('p.datetime').each(function() {
            var text = $(this).html();
            text = text.replace(/^\w*: /, "");
            text = text.replace(/<br>[^<]*: /g, "<br>");
            $(this).html(text);
        });

        var options = {
            firstDay: 1,  // THIS IS THE ONLY CHANGE!!!
            constrainInput: false,
            showOn: 'button',
            buttonImageOnly: false,
            buttonText: '',
            dateFormat: grappelli.getFormat('date'),
            showButtonPanel: true,
            showAnim: '',
            // HACK: sets the current instance to a global var.
            // needed to actually select today if the today-button is clicked.
            // see onClick handler for ".ui-datepicker-current"
            beforeShow: function(year, month, inst) {
                grappelli.datepicker_instance = this;
            }
        };
        var dateFields = $("input[class*='vDateField']:not([id*='__prefix__'])");
        dateFields.datepicker(options);

        if (typeof IS_POPUP != "undefined" && IS_POPUP) {
            dateFields.datepicker('disable');
        }

        // HACK: adds an event listener to the today button of datepicker
        // if clicked today gets selected and datepicker hides.
        // use on() because couldn't find hook after datepicker generates it's complete dom.
        $(document).on('click', '.ui-datepicker-current', function() {
            $.datepicker._selectDate(grappelli.datepicker_instance);
            grappelli.datepicker_instance = null;
        });

        // init timepicker
        $("input[class*='vTimeField']:not([id*='__prefix__'])").grp_timepicker();

    };
})(grp.jQuery);

第三,我修改了所有需要datepicker的AdminModels。这是一个PITA,我希望有一个更好的方法。可能有......