如何在行(行)而不是列中组织多对多复选框?

2024-03-07

我正在创建一个模块,其中我有一个Many2many字段,我想将其转换为复选框组。我已经在我的 XML 视图中编写了这个来实现它

<field name="location_ids" widget="many2many_checkboxes"/>

但该字段在长列中显示所有选项。我想在多行中显示选项,如下图所示:


我想我已经找到了适合你的好方法。

Research

首先,我搜索了使用小部件呈现的原始模板many2many_checkboxes。这个:

<t t-name="FieldMany2ManyCheckBoxes">
    <div t-foreach="widget.get('records')" t-as="record">
        <div class="o_checkbox">
            <input t-if="widget.get('value').indexOf(record[0]) !== -1" type="checkbox" t-att-data-record-id="JSON.stringify(record[0])" checked="checked"/>
            <input t-if="widget.get('value').indexOf(record[0]) === -1" type="checkbox" t-att-data-record-id="JSON.stringify(record[0])"/>
            <span/>
        </div>
        <label class="o_form_label"><t t-esc="record[1]"/></label>
    </div>
</t>

Template

所以,我复制了结果html代码group结构,我将它与小部件的模板结合起来。

您需要创建一个包含此内容的 xml 文件并将其添加到您的__manifes__.py file:

'qweb': ['static/src/xml/many2many_checkboxes.xml', ]

的代码为many2many_checkboxes.xml

<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
    <t t-extend="FieldMany2ManyCheckBoxes">
        <t t-jquery="div:first" t-operation="replace">
            <div class="o_group">    
                <table class="o_group o_inner_group o_group_col_6">
                    <tbody>
                        <t t-foreach="widget.m2mValues" t-as="record">
                            <t t-if="record_parity == 'odd'">
                                <t t-set="id_for_label" t-value="'o_many2many_checkbox_' + _.uniqueId()"/>
                                <tr>
                                    <td colspan="1" class="o_td_label">
                                        <label t-att-for="id_for_label" class="o_form_label"><t t-esc="record[1]"/></label>
                                    </td>

                                    <td colspan="1" style="width: 50%;">
                                        <div class="o_checkbox o_form_field_boolean o_form_field">
                                            <div class="o_checkbox">
                                                <input type="checkbox" t-att-id="id_for_label" t-att-data-record-id="JSON.stringify(record[0])"/>
                                                <span/>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            </t>
                        </t>
                    </tbody>
                </table>

                <table class="o_group o_inner_group o_group_col_6 pull-right">
                    <tbody>
                        <t t-foreach="widget.m2mValues" t-as="record">
                            <t t-if="record_parity == 'even'">
                                <t t-set="id_for_label" t-value="'o_many2many_checkbox_' + _.uniqueId()"/>
                                <tr>
                                    <td colspan="1" class="o_td_label">
                                        <label t-att-for="id_for_label" class="o_form_label"><t t-esc="record[1]"/></label>
                                    </td>

                                    <td colspan="1" style="width: 50%;">
                                        <div class="o_checkbox o_form_field_boolean o_form_field">
                                            <div class="o_checkbox">
                                                <input type="checkbox" t-att-id="id_for_label" t-att-data-record-id="JSON.stringify(record[0])"/>
                                                <span/>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            </t>
                        </t>
                    </tbody>
                </table>
            </div>
        </t>
    </t>
</templates>

Form

最后在表单中添加该字段。但没有group元素,因为我们已经添加了group上面模板上的 html 元素。您需要添加此属性style="display: block;"为了保持网格中的位置正确。

<separator string="Field name" />
<field name="test_many2many_checkboxes"
        widget="many2many_checkboxes"
        nolabel="1"
        style="display: block;" />

让我知道这是否适合您。我已经用我的 Odoo 11 实例进行了测试,效果很好。这是两列的结果。如果您想要三列,则需要调整模板:

可能的替代方案

我检查了Odoo开发人员如何完成“技术设置”上的表格。他们正在创建一张表,而不是像我一样创建两个表。所以也许有更好的方法来做到这一点,因为我需要循环所有记录两次来构建两个表。

无论如何,您可以自由地改进我的代码。我只是想引导您找到解决方案。也许您可以将记录分成三组来构建行。

Odoo 10

版本 10 的模板略有变化,因此您需要使用此模板:

<t t-extend="FieldMany2ManyCheckBoxes">
    <t t-jquery="div:first" t-operation="replace">
        <div class="o_group">    
            <table class="o_group o_inner_group o_group_col_6">
                <tbody>
                    <t t-foreach="widget.get('records')" t-as="record">
                        <t t-if="record_parity == 'odd'">
                            <tr>
                                <td colspan="1" class="o_td_label">
                                    <label for="o_field_input_28" class="o_form_label" data-original-title="" title="">
                                        <span t-esc="record[1]"/>
                                    </label>
                                </td>

                                <td colspan="1" style="width: 50%;">
                                    <div class="o_checkbox o_form_field_boolean o_form_field">
                                        <div class="o_checkbox">
                                            <input t-if="widget.get('value').indexOf(record[0]) !== -1" type="checkbox" t-att-data-record-id="JSON.stringify(record[0])" checked="checked"/>
                                            <input t-if="widget.get('value').indexOf(record[0]) === -1" type="checkbox" t-att-data-record-id="JSON.stringify(record[0])"/>
                                            <span/>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </t>
                    </t>
                </tbody>
            </table>

            <table class="o_group o_inner_group o_group_col_6 pull-right">
                <tbody>
                    <t t-foreach="widget.get('records')" t-as="record">
                        <t t-if="record_parity == 'even'">
                            <tr>
                                <td colspan="1" class="o_td_label">
                                    <label for="o_field_input_28" class="o_form_label" data-original-title="" title="">
                                        <span t-esc="record[1]"/>
                                    </label>
                                </td>

                                <td colspan="1" style="width: 50%;">
                                    <div class="o_checkbox o_form_field_boolean o_form_field">
                                        <div class="o_checkbox">
                                            <input t-if="widget.get('value').indexOf(record[0]) !== -1" type="checkbox" t-att-data-record-id="JSON.stringify(record[0])" checked="checked"/>
                                            <input t-if="widget.get('value').indexOf(record[0]) === -1" type="checkbox" t-att-data-record-id="JSON.stringify(record[0])"/>
                                            <span/>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </t>
                    </t>
                </tbody>
            </table>
        </div>
    </t>
</t>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在行(行)而不是列中组织多对多复选框? 的相关文章

随机推荐