簡體   English   中英

如何在門戶網站odoo13上添加所見即所得

[英]How to add WYSIWYG on portal website odoo13

我想通過使用以下本教程將所見即所得 HTML 添加到 Odoo 13 網站門戶,但我嘗試過的所見即所得顯示器僅顯示加載屏幕,因此無法輸入文本。 有什么遺漏嗎?

[這是顯示加載屏幕][2]<br><br>

XML 代碼:

<template id="portal_my_details_sl_elrn" inherit_id="portal.portal_my_details">
    <xpath expr="//form/div/div/div/div[3]" position="before">
        <div t-attf-class="form-group #{error.get('about_me') and 'o_has_error' or ''} col-xs-12">
            <!-- <label class="col-form-label" for="about_me">About Me</label> -->
            <label class="col-form-label" for="about_me">About Me</label>
            <textarea name="about_me" id="about_me" class="form-control o_wysiwyg_loader">
                <!-- <t t-esc="about_me"/> -->
                <input name="about_me" t-attf-class="form-control #{error.get('about_me') and 'is-invalid' or ''}"
                       t-att-value="about_me or partner.about_me"/>
            </textarea>
        </div>
    </xpath>
</template>

除了您發布的代碼之外,您還可以將website_profile添加到您的模塊depends中,並將o_wprofile_editor_form class 添加到form中。

<xpath expr="//form" position="attributes">
    <attribute name="class">o_wprofile_editor_form</attribute>
</xpath>

或者您可以將 javascript 網站配置文件編輯器代碼添加到website.assets_frontend而不是安裝website_profile

<template id="assets_frontend" inherit_id="website.assets_frontend">
    <xpath expr="script[last()]" position="after">
        <script type="text/javascript" src="/module_name/static/src/js/custom_editor.js"></script>
    </xpath>
</template>

以下代碼可在website_profile static 文件夾中找到。

var publicWidget = require('web.public.widget');
var wysiwygLoader = require('web_editor.loader');


publicWidget.registry.websiteProfileEditor = publicWidget.Widget.extend({
    selector: '.o_wprofile_editor_form',
    read_events: {
        'click .o_forum_profile_pic_edit': '_onEditProfilePicClick',
        'change .o_forum_file_upload': '_onFileUploadChange',
        'click .o_forum_profile_pic_clear': '_onProfilePicClearClick',
        'click .o_wprofile_submit_btn': '_onSubmitClick',
    },

    /**
     * @override
     */
    start: function () {
        var def = this._super.apply(this, arguments);
        if (this.editableMode) {
            return def;
        }

        var $textarea = this.$('textarea.o_wysiwyg_loader');
        var loadProm = wysiwygLoader.load(this, $textarea[0], {
            recordInfo: {
                context: this._getContext(),
                res_model: 'res.users',
                res_id: parseInt(this.$('input[name=user_id]').val()),
            },
        }).then(wysiwyg => {
            this._wysiwyg = wysiwyg;
        });

        return Promise.all([def, loadProm]);
    },

    //--------------------------------------------------------------------------
    // Handlers
    //--------------------------------------------------------------------------

    /**
     * @private
     * @param {Event} ev
     */
    _onEditProfilePicClick: function (ev) {
        ev.preventDefault();
        $(ev.currentTarget).closest('form').find('.o_forum_file_upload').trigger('click');
    },
    /**
     * @private
     * @param {Event} ev
     */
    _onFileUploadChange: function (ev) {
        if (!ev.currentTarget.files.length) {
            return;
        }
        var $form = $(ev.currentTarget).closest('form');
        var reader = new window.FileReader();
        reader.readAsDataURL(ev.currentTarget.files[0]);
        reader.onload = function (ev) {
            $form.find('.o_forum_avatar_img').attr('src', ev.target.result);
        };
        $form.find('#forum_clear_image').remove();
    },
    /**
     * @private
     * @param {Event} ev
     */
    _onProfilePicClearClick: function (ev) {
        var $form = $(ev.currentTarget).closest('form');
        $form.find('.o_forum_avatar_img').attr('src', '/web/static/src/img/placeholder.png');
        $form.append($('<input/>', {
            name: 'clear_image',
            id: 'forum_clear_image',
            type: 'hidden',
        }));
    },
    /**
     * @private
     */
    _onSubmitClick: function () {
        if (this._wysiwyg) {
            this._wysiwyg.save();
        }
    },
});


我們需要在表單提交上調用_onSubmitClick ,在提交按鈕上添加o_wprofile_submit_btn class:

<xpath expr="//button[@type='submit']" position="attributes">
    <attribute name="class">btn btn-primary o_wprofile_submit_btn</attribute>
</xpath>

編輯:

該小部件添加了一個輸入名稱files ,該文件傳遞給 controller 並且當您單擊Confirm按鈕時,會調用details_form_validate方法來檢查data (帖子)中存在的鍵是否也在MANDATORY_BILLING_FIELDSOPTIONAL_BILLING_FIELDS中。

我想您沒有使用files字段(未在BILLING_FIELDS中聲明),以避免警告嘗試繞過驗證:

from odoo.addons.portal.controllers.portal import CustomerPortal

class CustomerPortalNew(CustomerPortal):

    def details_form_validate(self, data):
        files = data.pop('files', None)
        res = super(CustomerPortalNew, self).details_form_validate(data)
        data['files'] = files
        return res


res_partner.py

from odoo import api, models, fields, _

class ResPartner(models.Model):
    _inherit = 'res.partner'

    about_me = fields.Html('About Me')

門戶.py

from odoo.http import Controller
from odoo.addons.portal.controllers.portal import CustomerPortal

CustomerPortal.OPTIONAL_BILLING_FIELDS.append('about_me')

class CustomerPortalNew(CustomerPortal):

    def details_form_validate(self, data):
        files = data.pop('files', None)
        res = super(CustomerPortalNew, self).details_form_validate(data)
        data['files'] = files
        return res

門戶模板.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="assets_frontend" inherit_id="website.assets_frontend">
        <xpath expr="script[last()]" position="after">
            <script type="text/javascript" src="/sl_elrn/static/src/js/website_profile.js"></script>
        </xpath>
    </template>
    <template id="portal_my_details_sl_elrn" inherit_id="portal.portal_my_details">
        <xpath expr="//form" position="attributes">
            <attribute name="class">o_wprofile_editor_form</attribute>
        </xpath>
        <xpath expr="//form/div/div/div/div[3]" position="before">
            <div t-attf-class="form-group #{error.get('about_me') and 'o_has_error' or ''} col-xl-12">
                <label class="col-form-label" for="about_me">About Me</label>
                <textarea name="about_me" id="about_me" style="min-height: 120px" class="form-control o_wysiwyg_loader">
                    <t t-esc="about_me or partner.about_me"/>
                </textarea>
            </div>
        </xpath>
        <xpath expr="//button[@type='submit']" position="attributes">
            <attribute name="class">btn btn-primary o_wprofile_submit_btn</attribute>
        </xpath>
    </template>
</odoo>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM