Files
gzth/funenc_json_widget/static/src/json_widget.js
T
2025-09-01 19:50:27 +08:00

77 lines
2.0 KiB
JavaScript

/** @odoo-module **/
import { registry } from "@web/core/registry";
import { _t } from "@web/core/l10n/translation";
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { Component, xml, onWillUpdateProps } from "@odoo/owl";
export class JsonTemplateWidget extends Component {
setup() {
this.parseValue();
onWillUpdateProps(() => {
this.parseValue();
});
}
parseValue() {
try {
let name = this.props.name;
let data = this.props.record.data;
let value = data[name] || "{}";
this.jsonData = JSON.parse(value);
console.log('JsonTemplateWidget - 解析的JSON数据:', this.jsonData);
} catch (e) {
console.error('JsonTemplateWidget - JSON解析错误:', e.message);
this.jsonData = { error: e.message };
}
}
get templateName() {
if (this.jsonData && this.jsonData.template) {
return this.jsonData.template;
}
return null;
}
get hasValidTemplate() {
return this.templateName !== null;
}
static template = xml`
<div class="json-template-widget">
<t t-if="hasValidTemplate">
<t t-call="{{ templateName }}" t-call-context="jsonData" />
</t>
<t t-else="">
<div class="alert alert-warning">
<t t-if="jsonData.error">
错误: <t t-esc="jsonData.error" />
</t>
<t t-else="">
未找到有效模板
</t>
</div>
</t>
</div>
`;
static props = {
...standardFieldProps,
};
get isReadonly() {
return true;
}
}
export const JsonTemplateField = {
component: JsonTemplateWidget,
displayName: _t("Json Template"),
supportedTypes: ["char", "text"],
isEmpty: () => false
};
registry.category("fields").add("json_template", JsonTemplateField);