odoo10 js实现全局搜索某个模型
-
借鉴gooderp的搜索的JS代码和XML代码如下:
问题:
1、show_query_board
这个函数中的是使用new Model('product.template').call()
。
实际是想查询字段code=XXX
值的model
并跳转到查询到的form
视图上,现在都是跳转到的视图都是新建的视图且是编辑状态的,怎么跳转到通过name_search
查询到的code的有具体信息的form视图?
2、'values': _.map(results, function(result) {}
渲染视图的 时候,这个方法_.map()
什么意思,是python中map()
函数的翻版使用吗?都有哪些这种函数,官方文档中没有找到?
3、查询的数据返回json数据的时候,key是不是就是在xml中引用的?xml代码如下。看应用是不是有问题。或者有都介绍下这个使用的都行。
JS代码:
odoo.define('web_stock_query', function(require) { var Client = require('web.WebClient'); var Model = require('web.DataModel'); var Core = require('web.core'); // var modell = new instance.web.Model("product.template"); Client.include({ show_application: function() { this._super.apply(this, arguments); this.show_stock_query(); this.$board = false; }, show_stock_query: function() { var self = this, $query = $('<ul class="nav navbar-nav navbar-right nav-stock-query"><li><input type="text" placeholder="查询"/><a class="query"></a><a class="destroy"></a><div class="stock-query-search-list"/></li></ul>'), $input = $query.find('input'), $destroy = $query.find('.destroy'); $input.on('focus', function(event) { $input.addClass('editable'); if ($input.val() !== '' && self.$board) self.$board.fadeIn('fast'); }).on('blur', function(event) { if ($input.val() === '') { $input.removeClass('editable'); } self.hide_query_board(); }).on('input', function(event) { if ($input.val() === '') { $destroy.fadeOut('fast'); self.hide_query_board(); } else { $destroy.fadeIn('fast'); self.show_query_board($input); } }).on('keydown', function(event) { switch (event.which) { case $.ui.keyCode.ENTER: self.select_query(); break; case $.ui.keyCode.DOWN: self.query_board_move('down'); event.preventDefault(); break; case $.ui.keyCode.UP: self.query_board_move('up'); event.preventDefault(); break; } }); $query.on('mousedown', '.stock-query-search-list li:not(.search-list-more)', function(event) { // alert('mousedown====') self.select_query($(this)); }).on('mousedown', '.search-list-more', function(event) { // alert('mousedown') self.open_report_stock_balance(); }).on('hover', '.stock-query-search-list li', function(event) { self.query_board_move($(this)); }).on('click', '.destroy', function(event) { $input.val(''); $input.focus(); $destroy.fadeOut('fast'); }); $('.oe_systray').before($query); }, show_query_board: function($input) { var self = this; new Model('product.template').call('name_search', {name: $input.val()} ).then(function(results) { var self = this; if (results.length <= 0) return self.hide_query_board(); self.$board = $(Core.qweb.render('web_stock_query.search_list', {'values': _.map(results, function(result) { return {id: result[0], default_code: result[1]}; })})); self.$board.attr('top', $input.height() + 2 + 'px'); $input.parent().find('.stock-query-search-list').html(self.$board); }); }, hide_query_board: function() { if (this.$board) this.$board.fadeOut('fast'); }, query_board_move: function(direction) { // alert('query board move'); if (this.$board) { var current_move = this.$board.find('li.select'), next_move = false; if (_.contains(['up', 'down'], direction)) { next_move = direction === 'down'? current_move.next(): current_move.prev(); if (next_move && next_move.is('li')) { var offset_y = next_move.offset().top - (direction === 'down'? this.$board.height(): 40); this.$board.scrollTop(this.$board.scrollTop() + offset_y); } } else if (direction.jquery) { next_move = direction; } if (next_move && next_move.is('li')) { next_move.addClass('select').siblings('.select').removeClass('select'); } } }, open_report_stock_balance: function() { // alert('===open report stock balance=='); this.action_manager.do_action({ type: 'ir.actions.act_window', res_model: 'product.template', views: [[381, 'form'], [false, 'list']], // limit:80000, target: 'current', name: 'Parts', }); }, select_query: function($target) { // alert('----select query----'); var self = this; if (self.$board) { $target = $target || self.$board.find('li.select'); if ($target.hasClass('search-list-more')) { return self.open_report_stock_balance(); } self.action_manager.do_action({ type: 'ir.actions.act_window', res_model: 'product.template', views: [[381, 'form'], [false, 'list']], // domain: [['default_code', '=', $target.data('default_code')]], target: 'current', // name: 'Parts', name: '搜索:' + $target.text().trim(), }); } } }); });
XML代码:
<?xml version='1.0' encoding='UTF-8'?> <templates id='template' xml:space='preserve'> <t t-name='web_stock_query.search_list'> <ul > <li t-foreach='values' t-as='product_number' t-attf-class='{{ default_code and "select" or ""}}' t-att-data-id='id' > <t t-esc='default_code' /> </li> <li class='search-list-more'>查询</li> </ul> </t> </templates>