Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1334 Lượt xem

Hello!
I'm using odoo 17 community version.

In my model i have a Datetime field

(.py file)
r_date = fields.Datetime(string="R date", required=False)

(xml)
<field name="r_date "/>

Field is empty by default, it's okay.  In the previous version(ver.10), when you clicked on a field, it was filled with the current date and time value, but now a widget appears and the value is still empty. Is this normal behavior? Can I return "as before" when the field, having received focus, is filled with the current datetime value at the current moment? I know about (default) in the model, but at the time of creation I don’t need to fill in the field, it should be empty - I need the field to be filled with the current date when it receives focus

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

This is my solution in Odoo 18.0. I think it same on 17.0

/** @odoo-module **/

import { DateTimeField, dateRangeField } from "@web/views/fields/datetime/datetime_field";

import { patch } from "@web/core/utils/patch";

import { onMounted, useRef } from "@odoo/owl";

const { DateTime } = luxon;


patch(DateTimeField.prototype, {

setup() {

super.setup();

this.startDateRef = useRef("start-date");


onMounted(() => {

this.startDateRef.el.addEventListener("focus", this.onFocus.bind(this));

});

},


/**

* @param {PointerEvent} ev

*/

onFocus ({ target }) {

if(this.props.defaultStartDateField === "today" && !this.state.value[0]) {

this.state.value = [

DateTime.now(),

false

];

}

},

});


DateTimeField.props = {

...DateTimeField.props,

defaultStartDateField: { type: String, optional: true },

};


const super_extractProps = dateRangeField.extractProps;

dateRangeField.extractProps = ({ attrs, options }, dynamicInfo) => ({

...super_extractProps({ attrs, options }, dynamicInfo),

defaultStartDateField: options.default_start_date_field,

});



In the views, you can add it to options
<field name="date_start" position="attributes">

<attribute name="options">{'end_date_field': 'date_end', 'useCurrent': 'day'}</attribute>

</field>

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 24
1709
2
thg 2 22
3336
1
thg 5 24
2684
1
thg 6 23
4805
0
thg 4 22
5446