我想我已经找到了适合你的好方法。
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>