Odoo-9,我的小部件在 QWeb 视图中不起作用

Odoo-9, my widget doesn't work in QWeb view

我在 Odoo 9 中制作了一个小部件,用于在 website view 中剪切产品描述。添加 widget="short_desc" 到产品表单视图和网站产品视图。我的意思是这样的:

<span t-field="product.description"/> <!-- full description -->
<span t-field="product.description" t-field-options='{"widget": "short_desc"}'/> <!-- short description -->
<span t-field="product.description" widget="short_desc"/> <!-- also tried this syntax -->

我发现这个答案很有用:,但它只适用于 product form 而不适用于 website

所以,我有一个 widgets.js:

odoo.define('wsup.widgets', function (require) {
'use strict';

var core = require('web.core');
var FieldChar = core.form_widget_registry.get('char');

var ShortDescriptionView = FieldChar.extend({
    render_value: function() {
        console.log('hey, im working!');
        this.$el.html('<span>Ok, widget really works</span>');
    },
});

core.form_widget_registry.add('short_desc', ShortDescriptionView);
});

当我转到 Sales -> Products 并打开任何产品时,我可以看到 "Ok, widget really works" 而不是它的描述,但是当我转到 /shop 页面时 — 产品描述仍然没有变化JS 控制台中没有任何内容。

这是我的网站产品 XML 视图的一部分(除了简短描述部分,效果很好):

<div class="product-preview oe_website_sale">
    <div class="product-preview__image">
        <a t-attf-href="/shop/product/{{ item.id }}">
            <span itemprop="image" t-field="item.image" t-field-options='{"widget": "image"}' t-att-alt="item.name"/>
        </a>
    </div>
    <div class="product-preview__info text-center">
        <div class="product-preview__info__title">
            <h2><a t-attf-href="/shop/product/{{ item.id }}"><span t-field="item.name"/></a></h2>
        </div>
        <div class="product-preview__info__description">
            <p><span t-field="item.description" t-field-options='{"widget": "short_desc"}'/></p>
        </div>
    </div>
</div>

为什么它在 /shop 页面上不起作用?我忘了做什么?谢谢。

据我了解您的评论,您的要求是显示少量描述而不是显示大量描述。所以,我觉得这个需求不用创建widget也可以轻松实现。

假设,您将此内容描述为:


两个比一个好

与许多小型耳机不同,Apple 入耳式耳机的每个耳机都包含两个独立的高性能驱动器 - 一个用于处理低音和中音的低音扬声器以及一个用于处理高频音频的高音扬声器。这些专用驱动器有助于确保在整个声波频谱中发出准确、细腻的声音。结果:您沉浸在音乐中,听到您从未意识到的细节。即使在听旧的最爱时,您也可能会觉得自己是第一次听到它。

从这么多的描述中,如果你想显示少量的描述或字数,你可以简单地使用下面的代码。

<span t-if="product.website_description and len(product.website_description) &gt; 500">
    <t t-set="description" t-value="product.website_description[:500] and product.website_description[:500].replace('+', '\n')+'...'"/>
        <p class="text-muted ">
            <t t-raw="description"/>
        </p>
</span>

在上面的代码中,[:500] 将是要使用的字数。


输出将是:

两个比一个好

与许多小型耳机不同,Apple 入耳式耳机的每个听筒都包含两个独立的高性能驱动器 — 一个低音扬声器以...


希望这段代码对您有所帮助。 谢谢