如何启用仅在odoo的canlendar中选择“ Mondays”?

时间:2018-07-18 05:17:45

标签: javascript odoo-9

我正在使用odoo9,我的模型中有一个日期字段,我只想在其日历中启用星期一,我在Google上找到了Jquery解决方案并将其应用于我的日期字段,但无法获得所需的结果。相反,我得到了两个日历,当我继续按鼠标左键单击日期字段时,将显示第二个日历。

enter image description here 第二张图片。enter image description here

这是我的Jquery代码:

       $('.o_datepicker_input').datepicker({
        beforeShowDay: function (date)
        {

        return [(date.getDay() == 1), ""];
        },

        });

2 个答案:

答案 0 :(得分:0)

尝试

$(".week").datepicker("option", {
beforeShowDay: function (date)
{
    return [date.getDay() == 1, ''];
}
});


var weekOptions = { "changeMonth": false, "changeYear": false, "stepMonths": 0, 
beforeShowDay: function (date) {
    return [date.getDay() == 1, ''];
} 
};
$(function () {
$(".week").datepicker("option", weekOptions);
});

答案 1 :(得分:0)

这是答案。有点棘手。

          odoo.define('automation.saturday_datepicker', function (require) {
          "use strict";

          var core = require('web.core');
          var formats = require('web.formats');
          var time = require('web.time');
          var Widget = require('web.Widget');

           var _t = core._t;

         `enter code here`var DateWidget = Widget.extend({
        template: "web.datepicker",
        type_of_date: "date",
        events: {
            'dp.change': 'change_datetime',
            'dp.show': 'set_datetime_default',
            'change .o_datepicker_input': 'change_datetime',
        },
        init: function(parent, options) {
            this._super.apply(this, arguments);

            var l10n = _t.database.parameters;

            this.name = parent.name;
            this.options = _.defaults(options || {}, {
                pickTime: this.type_of_date === 'datetime',
                useSeconds: this.type_of_date === 'datetime',
                startDate: moment({ y: 1900 }),
                endDate: moment().add(200, "y"),
                calendarWeeks: true,
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down'
                },
                language : moment.locale(),
                format : time.strftime_to_moment_format((this.type_of_date === 'datetime')? (l10n.date_format + ' ' + l10n.time_format) : l10n.date_format),
                daysOfWeekDisabled: [0, 1,2,3,4,5],
            });
        },
        start: function() {
            this.$input = this.$('input.o_datepicker_input');
            this.$el.datetimepicker(this.options);
            this.picker = this.$el.data('DateTimePicker');
            this.set_readonly(false);
            this.set_value(false);
        },
        set_value: function(value) {
            this.set({'value': value});
            this.$input.val((value)? this.format_client(value) : '');
            this.picker.setValue(this.format_client(value));
        },
        get_value: function() {
            return this.get('value');
        },
        set_value_from_ui: function() {
            var value = this.$input.val() || false;
            this.set_value(this.parse_client(value));
        },
        set_readonly: function(readonly) {
            this.readonly = readonly;
            this.$input.prop('readonly', this.readonly);
        },
        is_valid: function() {
            var value = this.$input.val();
            if(value === "") {
                return true;
            } else {
                try {
                    this.parse_client(value);
                    return true;
                } catch(e) {
                    return false;
                }
            }
        },
        parse_client: function(v) {
            return formats.parse_value(v, {"widget": this.type_of_date});
        },
        format_client: function(v) {
            return formats.format_value(v, {"widget": this.type_of_date});
        },
        set_datetime_default: function() {
            //when opening datetimepicker the date and time by default should be the one from
            //the input field if any or the current day otherwise
            var value = moment().second(0);
            if(this.$input.val().length !== 0 && this.is_valid()) {
                value = this.$input.val();
            }

            // temporarily set pickTime to true to bypass datetimepicker hiding on setValue
            // see https://github.com/Eonasdan/bootstrap-datetimepicker/issues/603
            var saved_picktime = this.picker.options.pickTime;
            this.picker.options.pickTime = true;
            this.picker.setValue(value);
            this.picker.options.pickTime = saved_picktime;
        },
        change_datetime: function(e) {
            if(this.is_valid()) {
                this.set_value_from_ui();
                this.trigger("datetime_changed");
            }
        },
        commit_value: function() {
            this.change_datetime();
        },
        destroy: function() {
            this.picker.destroy();
            this._super.apply(this, arguments);
        },
    });

    var DateTimeWidget = DateWidget.extend({
        type_of_date: "datetime"
    });

    return {
        DateWidget: DateWidget,
        DateTimeWidget: DateTimeWidget,
    };

    });

     odoo.define('web.form_widgets_custom', function (require) {
"use strict";

var core = require('web.core');
var common = require('web.form_common');
var formats = require('web.formats');

var automation_saturday_date_picker = require('automation.saturday_datepicker')
var _t = core._t;
var QWeb = core.qweb;

var FieldDatetime = core.form_widget_registry.get('datetime')
var FieldDate = FieldDatetime.extend({
    template: "FieldDate",
    build_widget: function() {
        return new automation_saturday_date_picker.DateWidget(this);
    }
});

core.form_widget_registry
    .add('saturday_date_widget', FieldDate)

});

最后将widget =“ saturday_date_widget”赋予xml中的模型字段。