Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一个表单中的两个日期字段在odoo中不起作用?_Javascript_Python 3.x_Datepicker_Odoo_Odoo 12 - Fatal编程技术网

Javascript 一个表单中的两个日期字段在odoo中不起作用?

Javascript 一个表单中的两个日期字段在odoo中不起作用?,javascript,python-3.x,datepicker,odoo,odoo-12,Javascript,Python 3.x,Datepicker,Odoo,Odoo 12,目前,我在一个表单中使用两个日期字段日期选择器时遇到了问题。当我在表单中使用单日期字段日期选择器选项(月)时,它工作正常,但当我在一个表单中使用两个日期字段日期选择器选项(月和年)时,它不工作。我想要的是在第一个日期字段中只打开月份和年份,在第二个日期字段中只打开年份。但在视图中真正发生的是在两个日期字段中覆盖相同类型的日期选择器 销售订单视图.xml <odoo> <record id="sale_order_only_month_form_view&quo

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

销售订单视图.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>
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, null, {timezone: false, 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, null, {timezone: false, show_type: 'months'});
            }
            else if (this.options.show_type == 'years'){
                console.log("_parseClient : years")
                return field_utils.parse[this.type_of_date](v, null, {timezone: false, show_type: 'years'});
            }
            return this._super.apply(this, arguments);
        },
    });

    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, field, 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));
    }

});