如何在Odoo中聚焦以TransientModel形式的字段?

时间:2018-09-10 09:33:40

标签: javascript xml focus odoo odoo-10

我刚刚意识到default_focus="1"不适用于瞬态模型视图中的字段(至少使用target="new"打开的字段)。我认为应该仅将autofocus="autofocus"buttons使用的pages适用于那些“瞬态模型”字段,但仅在您第一次打开视图时适用。除非您重新加载浏览器,否则它将无法正常工作。

在我的情况下,我为Char字段(在瞬态模型内)创建了一个小部件,每次打开弹出窗口时,都需要将该字段聚焦。由于无法通过XML进行操作,因此我尝试使用JavaScript进行操作。

这是我的小部件的XML模板,该模板继承自FieldChar

<t t-name="FieldRed" t-extend="FieldChar">
    <t t-jquery="input" t-operation="attributes">
        <attribute name="id">barcode_input</attribute>
        <attribute name="class">o_form_input bg-red</attribute>
    </t>
</t>

现在,我的小部件的JS代码。我试图将其集中在start方法上,因为这是渲染之后执行的第一个方法:

start: function() {
    console.log('START');
    this._super.apply(this, arguments);
    this.$el.parent().find('input').focus();
},

但这不起作用。我也尝试过:

this.setupFocus(this.$el.parent().find('input'));

但是什么也没发生。但是,例如,如果我包含这一行,则该字段会按预期自动填写:

this.$el.parent().find('input').val('START');

所以我的问题是:

每次打开表单时,如何聚焦小部件的input字段?

2 个答案:

答案 0 :(得分:0)

最后,我刚刚找到了一个简单的解决方法。我只需等待50毫秒,然后再通过const express = require("express"); const app = express(); const port = 9999; const { getFromPHP } = require('./middleware.js'); const apachePHPconfig = { host: 'localhost', port: 80, urlpath: 'path-to-php-script' } app.get( '/', getFromPHP(apachePHPconfig.host, apachePHPconfig.port, apachePHPconfig.urlpath , {givemeanumber: 1}), function (req, res) { // here is your php object console.log('php', req.php); res.end(); }) app.listen(port, () => { console.clear(); console.log(`Example app listening on port ${port}!`) }) 调用/** * Middleware to get data from PHP */ const getFromPHP = (phpHost, phpPort, phpPath, phpObject) => { if (typeof phpHost === 'undefined') { throw new Error('phpHost was not defined'); } if (typeof phpPort === 'undefined') { throw new Error('phpPort was not defined'); } if (typeof phpPath === 'undefined') { phpPath = '/'; } if (typeof phpObject !== 'object' ) { phpObject = {}; } return (req, res, next) => { if (typeof req.php === 'undefined') { req.php = {}; } const options = { hostname: phpHost, // change this to your php server host port: phpPort, // change this with your php server port path: phpPath, // change this with your php server path to script method: 'POST', headers: { // here we send 'NODE-REQUEST', it will be available in php unde $_SERVER global prefixed with HTTP_ string because is a custom client request header. 'NODE-REQUEST': JSON.stringify(phpObject) } }; const isJSON = (str ) => { try { let j = JSON.parse(str); return typeof j === 'object' && j !== null; } catch (e) { return false; } }; const httpModule = require('http'); let reqHttp = httpModule.request(options, (response) => { if( typeof response.headers['node-response'] === 'undefined' || !isJSON(response.headers['node-response'])){ req.php = {}; }else{ req.php = JSON.parse(response.headers['node-response']); } // START - Remove this code when everything runs as expected let dataStack = []; response.on('data', (data)=>{ dataStack.push(data.toString()); }) response.on('end', ()=>{ console.log("PHP HEADERS", response.headers) console.log('PHP OUTPUT', dataStack.join('')); }) // END next(); }); reqHttp.on('error', (e) => { console.error(`problem with request to php server: ${e.message}`); next(); }); reqHttp.on('end', () => { next(); }); reqHttp.end(); } } exports.getFromPHP = getFromPHP; 。我猜想在调用focus时,Odoo核心会自动将焦点移到某个位置,如果我等待该动作的结束,然后将焦点移到所需的位置,它将起作用。不过,这不是我喜欢的解决方案,但目前是我唯一的解决方案:

setTimeout

答案 1 :(得分:0)

我用一个简单的jquery做到了。

我在字段中添加一个类,并在弹出窗口中编写脚本以供查看。

就像下面一样。

  

<form string="Stock Disposal">
                <sheet>
                    <script>
                        $('.my-focus-field').focus()

                    </script>

                    <div class="oe_title">
                        <h1>
                            Stock Disposal
                        </h1>
                    </div>
                    <group>
                        <field name="serial_number" class="my-focus-field"/>
                    </group>
                </sheet>
            </form>

让我知道这是错误的方法。