如何在行(行)而不是列中组织 many2many 复选框? [英] How to organize many2many checkboxes in lines (rows) rather than columns?

查看:105
本文介绍了如何在行(行)而不是列中组织 many2many 复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是该字段在一个长列中显示了所有选项.我想在多行中显示选项,如下图所示:

解决方案

我想我找到了适合您的好方法.

研究

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

<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(记录[0])"已检查=已检查"/><input t-if="widget.get('value').indexOf(record[0]) === -1" type="checkbox" t-att-data-record-id="JSON.stringify(记录[0])"/><span/>

<label class="o_form_label"><t t-esc="record[1]"/></label>

</t>

模板

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

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

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

many2many_checkboxes.xml

的代码

<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/>

</td></tr></t></t></tbody><table class="o_group o_inner_group o_group_col_6 pull-right"><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/>

</td></tr></t></t></tbody>

</t></t></模板>

表格

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

<field name="test_many2many_checkboxes"小部件 =many2many_checkboxes"无标签=1"样式=显示:块;"/>

让我知道这是否适合您.我已经用我的 Odoo 11 实例进行了测试,它工作正常.这是两列的结果.如果您需要三列,则需要调整模板:

可能的选择

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

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

Odoo 10

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

<t t-jquery="div:first" t-operation="replace"><div class="o_group"><table class="o_group o_inner_group o_group_col_6"><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]"/></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(记录[0])"已检查=已检查"/><input t-if="widget.get('value').indexOf(record[0]) === -1" type="checkbox" t-att-data-record-id="JSON.stringify(记录[0])"/><span/>

</td></tr></t></t></tbody><table class="o_group o_inner_group o_group_col_6 pull-right"><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]"/></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(记录[0])"已检查=已检查"/><input t-if="widget.get('value').indexOf(record[0]) === -1" type="checkbox" t-att-data-record-id="JSON.stringify(记录[0])"/><span/>

</td></tr></t></t></tbody>

</t></t>

I am creating one module where I have a Many2many field and I would like to convert it into a checkbox group. I have written this in my XML view to achieving it

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

But the field is shown all the options in a long column. I would like to show the options in multiple rows as in the following image:

解决方案

I think I have found a good approach for you.

Research

First I have search the original template which is rendered with the widget many2many_checkboxes. This one:

<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

So, I have copied the result html code of the group structure and I have combined it with the template of the widget.

You need to create a xml file with this content and add it to your __manifes__.py file:

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

The code of 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

Finally add the field in your form. But without group elements since we are already adding the group html elements on the template above. You need to add the this attribute style="display: block;" in order to keep the position in the grid right.

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

Let me know if this works for you. I have tested with my Odoo 11 instance and it works fine. This is the result with two columns. If you want three columns you will need to adapt the template:

Possible Alternative

I have checked how Odoo developers have done the table on the "Technical Settings". They are creating one single table instead of two as I do. So maybe there is a better way to do it because I need to loop over all the records twice to build the two tables.

Anyway you are free to improve my code. I only wanted to guide you to the solution. Maybe you can group the records in threes to build the rows.

Odoo 10

The template changes a little bit for the version 10, so you need to use this template instead:

<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>

这篇关于如何在行(行)而不是列中组织 many2many 复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆