This commit is contained in:
李鹏宇
2026-04-16 10:38:39 +08:00
parent d816d29f4e
commit 38e0ac64ae
5 changed files with 62 additions and 87 deletions
@@ -147,7 +147,6 @@ export class FieldsFilter extends Component {
fieldString: field.fieldString,
searchLimit: field.fieldOptions?.search_limit || 10,
placeholder: field.placeholder,
isMulti: field.fieldWidget === "multi",
}
this._addFieldBaseProps(field.props, field);
}
@@ -1,6 +1,6 @@
/** @odoo-module **/
import { Component, useState } from "@odoo/owl";
import { Component } from "@odoo/owl";
import { Domain } from "@web/core/domain";
import { useService } from "@web/core/utils/hooks";
import { Many2XAutocomplete } from "@web/views/fields/relational_utils";
@@ -12,32 +12,60 @@ export class Many2oneFilter extends Component {
super.setup();
this.rpc = useService("rpc");
this.user = useService("user");
const saved = this.env.searchModel.headerSearchInfo.FieldsFilter[this.props.id]?.activeValues;
if (this.props.isMulti) {
// 多选模式:activeValues 是数组 [{id, name}, ...]
this.state = useState({ multiValues: Array.isArray(saved) ? saved : [] });
this.activeValues = this.env.searchModel.headerSearchInfo.FieldsFilter[this.props.id]?.activeValues || [];
}
/**
* 设置筛选字段的值
*/
setActiveValues(ev) {
/*
* 判断值是否是单选,如果是单选,则替换掉之前的值
* 否则,判断是选中还是取消选中,如果是选中,则添加到数组中,否则从数组中删除
* */
const value = ev.target.value;
const isChecked = ev.target.checked;
if (this.props.isSingle) {
this.activeValues = [value];
} else {
// 单选模式:activeValues 是对象 {id, name} 或 {}
this.activeValues = (saved && !Array.isArray(saved)) ? saved : {};
// 判断是选中还是取消选中,如果是选中,则添加到数组中,否则从数组中删除
if (isChecked) {
// 如果是选中状态,则添加到数组中
this.activeValues.push(value);
} else {
// 如果是取消选中状态,则从数组中删除
const index = this.activeValues.indexOf(value);
if (index !== -1) {
this.activeValues.splice(index, 1);
}
}
}
}
// ===== 单选模式 domain =====
/**
* 获取筛选字段的domain
*/
get domain() {
if (this.props.isMulti) {
const ids = this.state.multiValues.map(v => v.id);
return ids.length ? [[this.props.name, "in", ids]] : [];
}
return this.activeValues.id ? [[this.props.name, "=", this.activeValues.id]] : [];
return this.activeValues.id ? [[this.props.name, "=", this.activeValues.id]]: [];
}
/**
* 点击筛选字段时触发,将筛选字段
*/
selectionOnchange(ev) {
// 设置筛选字段的值
this.setActiveValues(ev);
this.props.fieldOnchange({fieldDomainList: this.domain, activeValues: this.activeValues});
}
/**
* 点击清空筛选时触发
*/
resetFilter() {
if (this.props.isMulti) {
this.state.multiValues = [];
} else {
this.activeValues = {};
}
this.props.fieldOnchange({ fieldDomainList: this.domain, activeValues: this.props.isMulti ? this.state.multiValues : this.activeValues });
// 清空筛选字段的值
this.activeValues = [];
this.props.fieldOnchange({fieldDomainList: this.domain, activeValues: this.activeValues});
}
getDomain() {
@@ -48,24 +76,19 @@ export class Many2oneFilter extends Component {
return new Domain(this.props.domain).toList(evalContext);
}
// ===== 单选 update =====
async update(value) {
if (this.props.isMulti) {
await this.updateMulti(value);
return;
}
if (value) {
value = value[0];
value = value[0]
if (!value.name) {
try {
await this.rpc("/web/dataset/call_kw", {
await this.rpc("/web/dataset/call_kw",{
model: this.props.resModel,
method: "name_get",
args: [[value.id]],
kwargs: {},
}).then((result) => {
value.name = result[0][1];
});
value.name = result[0][1]
})
} catch (error) {
this.activeValues = {};
}
@@ -74,46 +97,16 @@ export class Many2oneFilter extends Component {
} else {
this.activeValues = {};
}
this.props.fieldOnchange({ fieldDomainList: this.domain, activeValues: this.activeValues });
}
// ===== 多选 update =====
async updateMulti(value) {
if (value && value.length) {
let item = value[0];
if (!item.name) {
try {
await this.rpc("/web/dataset/call_kw", {
model: this.props.resModel,
method: "name_get",
args: [[item.id]],
kwargs: {},
}).then((result) => {
item.name = result[0][1];
});
} catch (e) { return; }
}
// 去重
if (!this.state.multiValues.find(v => v.id === item.id)) {
this.state.multiValues = [...this.state.multiValues, item];
}
}
this.props.fieldOnchange({ fieldDomainList: this.domain, activeValues: this.state.multiValues });
}
// ===== 多选:删除某个 tag =====
removeTag(id) {
this.state.multiValues = this.state.multiValues.filter(v => v.id !== id);
this.props.fieldOnchange({ fieldDomainList: this.domain, activeValues: this.state.multiValues });
this.props.fieldOnchange({fieldDomainList: this.domain, activeValues: this.activeValues})
}
get Many2XAutocompleteProps() {
return {
value: this.props.isMulti ? '' : (this.activeValues.name || ''),
value: this.activeValues.name || '',
resModel: this.props.resModel,
getDomain: this.getDomain.bind(this),
update: this.update.bind(this),
activeActions: { create: false, createEdit: false, write: false },
activeActions: {create: false, createEdit: false, write: false},
fieldString: this.props.fieldString,
searchLimit: this.props.searchLimit,
placeholder: this.props.placeholder,
@@ -127,9 +120,8 @@ Many2oneFilter.components = {
};
Many2oneFilter.props = {
...standardFieldFilterProps,
resModel: { type: String, required: true },
domain: { type: String, optional: true },
fieldString: { type: String, optional: true },
searchLimit: { type: Number, optional: true },
isMulti: { type: Boolean, optional: true },
resModel: {type: String, required: true},
domain: {type: String, optional: true},
fieldString: {type: String, optional: true},
searchLimit: {type: Number, optional: true},
};
@@ -1,24 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<!-- many2one字段筛选框 -->
<!-- selection字段筛选框 -->
<t t-name="header_search_panel.Many2oneFilter" owl="1">
<t t-if="props.isMulti">
<!-- 多选模式:已选 tag + 输入框 -->
<div class="o_field_many2many_tags d-flex flex-wrap align-items-center gap-1" style="min-width:80px;">
<t t-foreach="state.multiValues" t-as="tag" t-key="tag.id">
<span class="badge rounded-pill text-bg-primary d-flex align-items-center" style="font-size:12px;gap:3px;">
<t t-esc="tag.name"/>
<i class="fa fa-times" style="cursor:pointer;margin-left:3px;" t-on-click="() => removeTag(tag.id)"/>
</span>
</t>
<Many2XAutocomplete t-props="this.Many2XAutocompleteProps"/>
</div>
</t>
<t t-else="">
<!-- 单选模式 -->
<Many2XAutocomplete t-props="this.Many2XAutocompleteProps"/>
</t>
<Many2XAutocomplete t-props="this.Many2XAutocompleteProps"/>
</t>
</templates>
@@ -481,9 +481,9 @@
</quick_filter>
<field name="year_id" placeholder="年" options="{'no_label': True}"/>
<field name="month_id" placeholder="月" options="{'no_label': True}"/>
<field name="company_id" placeholder="公司" options="{'no_label': True}" widget="multi"/>
<field name="company_id" placeholder="公司" options="{'no_label': True}"/>
<field name="tenure_type" options="{'no_label': True}"/>
<field name="precinct_id" placeholder="片区管理员" options="{'no_label': True}" widget="multi"/>
<field name="precinct_id" placeholder="片区管理员" options="{'no_label': True}"/>
</header_search>
</search>
</field>
@@ -124,7 +124,6 @@
</group>
</group>
</xpath>
<!-- 创建者字段已添加 -->
<xpath expr="//field[@name='name']" position="attributes">
<attribute name="placeholder">购******的事宜</attribute>
</xpath>