技术问询:在Odoo V11控制面板视图新增空间数据搜索按钮
嘿,针对你在Odoo 11控制面板添加空间数据搜索按钮的需求,我给你整理了一套完整的实现方案,一步步来就能搞定:
实现方案:在Odoo 11搜索框旁添加自定义空间数据搜索按钮
1. 第一步:继承搜索栏模板,插入自定义按钮
Odoo的搜索栏是通过模板渲染的,我们需要继承默认的搜索栏模板,把空间搜索按钮插入到你标注的位置。
在你的自定义模块中,新建views/custom_search_template.xml文件,内容如下:
<odoo> <!-- 继承默认搜索栏模板 --> <template id="extend_search_bar" inherit_id="web.search_bar" name="Extended Search Bar with Spatial Button"> <!-- 定位到搜索选项区域前,插入按钮 --> <xpath expr="//div[hasclass('o_search_options')]" position="before"> <button type="button" class="btn btn-primary o_spatial_search_btn" title="空间数据搜索"> <i class="fa fa-map-marker"/> 空间搜索 </button> </xpath> </template> <!-- 引入前端JS和CSS资源 --> <template id="assets_backend" inherit_id="web.assets_backend"> <xpath expr="." position="inside"> <script type="text/javascript" src="/your_module_name/static/src/js/spatial_search.js"/> <link rel="stylesheet" href="/your_module_name/static/src/css/spatial_search.css"/> </xpath> </template> </odoo>
注意:把your_module_name替换成你实际的模块名称
2. 第二步:编写前端JS逻辑,处理按钮点击事件
接下来需要写JavaScript来监听按钮点击,触发空间搜索的逻辑。在模块的static/src/js/spatial_search.js中添加:
odoo.define('your_module_name.spatial_search', function(require) { "use strict"; var core = require('web.core'); var SearchBar = require('web.SearchBar'); // 继承默认的SearchBar组件,添加自定义逻辑 SearchBar.include({ start: function() { var self = this; this._super.apply(this, arguments); // 绑定空间搜索按钮的点击事件 this.$el.find('.o_spatial_search_btn').on('click', function() { self._openSpatialSearchWizard(); }); }, _openSpatialSearchWizard: function() { var self = this; // 弹出一个自定义向导,让用户输入空间搜索参数(比如地图范围、坐标) this.do_action({ type: 'ir.actions.act_window', res_model: 'spatial.search.wizard', views: [[false, 'form']], target: 'new', context: { active_model: this.model, current_domain: this.getDomain(), } }).then(function(action_result) { // 向导确认后,调用后端的空间搜索方法 if (action_result && action_result.spatial_params) { self._rpc({ model: self.model, method: '_search_spatial', args: [self.getDomain(), action_result.spatial_params], }).then(function(record_ids) { // 更新当前视图的搜索结果 self.trigger_up('search', {domain: [['id', 'in', record_ids]]}); }); } }); } }); return SearchBar; });
3. 第三步:创建空间搜索向导(可选,但推荐)
为了让用户直观输入空间参数,我们可以创建一个向导模型。在模块的models/spatial_search_wizard.py中添加:
from odoo import models, fields, api class SpatialSearchWizard(models.TransientModel): _name = 'spatial.search.wizard' _description = '空间数据搜索向导' polygon = fields.Text(string='空间范围(WKT格式)', required=True, help="输入WKT格式的多边形,例如:POLYGON((x1 y1, x2 y2, ...))") @api.multi def confirm_search(self): self.ensure_one() # 返回空间参数给前端 return { 'spatial_params': { 'polygon': self.polygon, } }
同时在views/spatial_search_wizard_views.xml中添加向导的表单视图:
<odoo> <record id="spatial_search_wizard_form" model="ir.ui.view"> <field name="name">spatial.search.wizard.form</field> <field name="model">spatial.search.wizard</field> <field name="arch" type="xml"> <form string="空间数据搜索"> <group> <field name="polygon"/> </group> <footer> <button name="confirm_search" type="object" string="搜索" class="btn-primary"/> <button string="取消" class="btn-default" special="cancel"/> </footer> </form> </field> </record> </odoo>
4. 第四步:后端实现空间搜索逻辑
最后在你需要搜索的业务模型中,添加自定义的空间搜索方法(假设你的模型有location字段存储PostGIS空间数据):
from odoo import models, api class YourBusinessModel(models.Model): _inherit = 'your.business.model' # 替换成实际的模型名称 @api.model def _search_spatial(self, base_domain, spatial_params): # 结合基础搜索域和空间条件 spatial_domain = base_domain.copy() # 使用PostGIS的&&操作符判断空间相交(根据你的需求调整) spatial_domain.append(('location', '&&', spatial_params['polygon'])) # 执行搜索并返回记录ID列表 records = self.search(spatial_domain) return records.ids
5. 可选:调整按钮样式
如果按钮位置需要微调,在static/src/css/spatial_search.css中添加:
.o_spatial_search_btn { margin-right: 8px; margin-left: 4px; }
最后一步:激活模块
确保你的模块依赖web模块,在__manifest__.py中添加:
'depends': ['web'],
然后升级模块,就能看到搜索框旁的空间搜索按钮啦!
内容的提问来源于stack exchange,提问作者Jerry Zhang
相关产品推荐
相关产品推荐

