一个表单中的两个 Datefield 在 odoo 中不起作用?

问题描述

我目前在以一种形式使用两个日期字段日期选择器时遇到问题当我在表单中使用单个日期字段日期选择器选项(月)时,它工作正常但是当我使用两个日期字段日期时-picker 选项(月和年)在一种形式中它不起作用。我想要的是在第一个日期字段中只打开月份和年份,在第二个日期字段中只打开年份。但是在视图内部真正发生的是覆盖两个日期字段中相同类型的日期选择器。

sale_order_view.xml

<odoo>
    <record id="sale_order_only_month_form_view" model="ir.ui.view">
        <field name="name">sale.order.only.month.form.view</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='partner_id']" position="after">
                <field name="month_date" options="{'datepicker': {'show_type': 'months'}}"/>
                <field name="year_date" options="{'datepicker': {'show_type': 'years'}}"/>
            </xpath>
        </field>
    </record>
</odoo>

datepicker.js

odoo.define('web_month_year.date_picker',function (require) {
"use strict";

    var datepicker = require('web.datepicker');
    var DateWidget = datepicker.DateWidget;
    var field_utils = require('web.field_utils');

    var session = require('web.session');
    var time = require('web.time');

    DateWidget.include({
        init: function (parent,options) {
            if (options.show_type == 'months'){
                options.format = 'MM/YYYY';
                console.log("init : months")
//                options.viewmode = 'months'
            }
            else if (options.show_type == 'years'){
                options.format = 'YYYY';
                console.log("init : years")
//                options.viewmode = 'years'
            }
//            else {
//                options.format = 'MM/DD/YYYY';
//                console.log("init : date")
////                options.viewmode = 'years'
//            }
            this._super(parent,options);
        },_formatClient: function (v) {
            if (this.options.show_type == 'months'){
                console.log("_formatClient : months")
                return field_utils.format[this.type_of_date](v,null,{timezone: false,show_type: 'months'});
            }
            else if (this.options.show_type == 'years'){
                console.log("_formatClient : years")
                return field_utils.format[this.type_of_date](v,show_type: 'years'});
            }
            return this._super.apply(this,arguments);
        },_parseClient: function (v) {
            console.log("v",v)
            if (this.options.show_type == 'months'){
                console.log("_parseClient : months")
                return field_utils.parse[this.type_of_date](v,show_type: 'months'});
            }
            else if (this.options.show_type == 'years'){
                console.log("_parseClient : years")
                return field_utils.parse[this.type_of_date](v,});

    field_utils.format.date = function formatDate(value,field,options) {
        console.log("format value",value)
        if (value === false) {
            return "";
        }
        if (field && field.type === 'datetime') {
            if (!options || !('timezone' in options) || options.timezone) {
                value = value.clone().add(session.getTZOffset(value),'minutes');
            }
        }
        var date_format = time.getLangDateFormat();
        if (options.show_type && options.show_type == 'months'){
            console.log("getLangDateFormat : months")
            date_format = time.strftime_to_moment_format('%m/%Y');
        }
        else if (options.show_type && options.show_type == 'years'){
            console.log("getLangDateFormat : years")
            date_format = time.strftime_to_moment_format('%Y');
        }
        if (options.datepicker && options.datepicker.show_type && options.datepicker.show_type == 'months'){
            console.log("options.datepicker : months")
            date_format = time.strftime_to_moment_format('%m/%Y');
        }
        else if (options.datepicker && options.datepicker.show_type && options.datepicker.show_type == 'years'){
            console.log("options.datepicker : years")
            date_format = time.strftime_to_moment_format('%Y');
        }
        console.log("return",date_format)
        console.log(value.format(date_format))
        return value.format(date_format);
    };

    field_utils.parse.date = function parseDate(value,options) {
        if (!value) {
            return false;
        }
        var datePattern;
        console.log("options.show_type",options.show_type)
        if (options.show_type && options.show_type == 'months'){
            datePattern = time.strftime_to_moment_format('%m/%Y');
        }
        else if (options.show_type && options.show_type == 'years'){
            datePattern = time.strftime_to_moment_format('%Y');
        }
        else{
            datePattern = time.getLangDateFormat();
        }
        console.log("datePattern",datePattern)
        var datePatternWoZero = datePattern.replace('MM','M').replace('DD','D').replace('YYYY','Y');
        var date;
        if (options && options.isUTC) {
            date = moment.utc(value);
        } else {
            date = moment.utc(value,[datePattern,datePatternWoZero,moment.ISO_8601]);
        }
        if (date.isValid()) {
            if (date.year() === 0) {
                date.year(moment.utc().year());
            }
            if (date.year() >= 1900) {
                console.log("1900")
                date.toJSON = function () {
                    return this.clone().locale('en').format('YYYY-MM-DD');
                };
                console.log("date.toJSON",date)
                return date;
            }
        }
        throw new Error(_.str.sprintf(core._t("'%s' is not a correct date"),value));
    }

});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)