简体   繁体   English

Odoo Web客户端。 无法使用jquery选择器选择html元素

[英]Odoo web client. Unable to select an html element with the jquery selector

The following refer to Odoo v9 enterprise version. 以下是指Odoo v9企业版。

Task : When opening the timesheet form from Main Menu -> timesheets, send a .click() event on the Details tab so that the details tab opens. 任务 :从“主菜单”->“时间表”打开时间表表单时,在“详细信息”选项卡上发送一个.click()事件,以便打开“详细信息”选项卡。

Problem : When I use the selector to select the element I get nothing in return. 问题 :当我使用选择器选择元素时,没有任何回报。 ( I have confirmed that the selector I use is correct for the dom element that interests me) (我已经确认我使用的选择器对我感兴趣的dom元素是正确的)

In accordance with the official documentation for the odoo web client, I have extended the web.FormView widget, and overriden it's start method. 根据odoo Web客户端的官方文档,我扩展了web.FormView小部件,并覆盖了它的启动方法。 From taking a look at the examples, it seems to me that when the start method is invoked the DOM has already been rendered. 通过查看示例,在我看来,当调用start方法时, DOM已经呈现。

When I use console.log(this) from within the start method I can see an object that has the DOM in the $el variable. 当我从start方法中使用console.log(this) ,我可以看到在$el变量中具有DOM的对象。

I am trying to make a selection using a jquery selector but I am unable to get my object. 我正在尝试使用jquery选择器进行选择,但无法获取对象。

My custom widget js: 我的自定义小部件js:

odoo.define('t9397.form_override', function(require) {
    "use strict";
    var core = require('web.core');
    var web_widget = require('web.FormView')

    var FormWidgetOverride = web_widget.extend({

    start : function(record) {
        console.log('overridden')
        console.log(this)
        this.$("a[data-toggle='tab']:contains('Details')").click()
        $("a[data-toggle='tab']:contains('Details')").click()// trying both
        return this._super()

        }
    })
    core.view_registry.add('form', FormWidgetOverride);
});

The DOM object I want to send a click event to: 我想将click事件发送到的DOM对象:

< a data-toggle="tab" disable_anchor="true" role="tab" href="#notebook_page_22">Details< /a >

My knowledge of promises, Jquery , and especially the web client of odoo is somewhat rather limited. 我对odooJquery (尤其是odoo的Web客户端)的odoo有些有限。 Any help will be much appreciated. 任何帮助都感激不尽。

start() may perform rendering tasks including asynchronous procedures, so it's necessary to wait while it finishes it's execution. start()可能会执行包括异步过程在内的渲染任务,因此必须等待其完成执行。 As per documentation : 根据文档

A widget is not guaranteed to work correctly until its start() method has finished executing. 在其start()方法完成执行之前,不能保证小部件可以正常工作。 The widget's parent/creator must wait for a widget to be fully started before interacting with it. 小部件的父/创建者在与小部件进行交互之前必须等待其完全启动。

Normally, start should return a Deferred() jQuery object , and if start() itself is implemented properly in super, than you can wait it using the Deferred object received from it's call: 通常,start应该返回一个Deferred()jQuery对象 ,并且如果start()本身在super中正确实现,那么您可以使用从调用中收到的Deferred对象来等待它:

First case to use for a simple code as yours: 第一种情况,像您一样使用简单的代码:

start : function() {
    var sup_ready = this._super();
    sup_ready.done(function(){
        //your code:
        console.log('overridden');
        console.log(this);
        this.$("a[data-toggle='tab']:contains('Details')").click();
        $("a[data-toggle='tab']:contains('Details')").click(); // trying both
    });
    return $.when( sup_ready );
    }
});

Second case, to use for asynchronous procedures and more complicated code: 第二种情况,用于异步过程和更复杂的代码:

start : function() {
    var sup_ready = this._super();
    var me_ready = $.Deferred();
    . . .
    sup_ready.done(function(){
        . . .
        . . .
        //finally Resolve or Reject your deferred object:
        //Resovle:
        me_ready.resolve(); //use it if all go ok
        // *OR* Reject (in case something go wong) to indicate failure:
//      me_ready.reject();
    });
    . . . 
    return $.when( sup_ready, me_ready );
});

Note that I put me_ready.resolve() inside sup_ready.done() but it is NOT absolutely necessary and in general it may be other way around ie you can Reject or Resolve me_ready outside of this sup_ready.done() , you can resolve or reject me_ready deferred object anywhere in your code. 请注意,我将me_ready.resolve()放在me_ready.resolve()内, sup_ready.done()不是绝对必要的,通常它可能是另一种方式,即您可以在此sup_ready.done()之外拒绝或解决me_ready ,您可以解析或在代码中的任何地方拒绝me_ready延迟对象。

The second case above does not apply to your current issue, but it's a pitfall to avoid in the future, just in case. 以上第二种情况不适用于您当前的问题,但是以防万一,这是将来应避免的陷阱。

As an alternative to the start() way you choose, consider also an events property of the widget and check if you can apply it to your case... 作为选择 start()方式的替代方法,还请考虑小部件的events属性 ,并检查是否可以将其应用于案例...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM