odoo16 实现扫码枪连续扫描功能

发布时间 2023-09-08 18:41:32作者: CrossPython

其实要实现这个功能很简单,只要在前端js里监控扫码字段的change事件。 当满足要求,调用保存按钮的click方法,自动保存。这样扫码工人就不需要操作电脑了,可是由于对odoo前段代码不熟悉,这么个小功能花了我一周左右的时间,虽然问题解决了,但是实现的方式很暴力。不管怎么样,先解决问题再说吧。

addons/web/static/src/views/fields/input_field_hook.js

修改这个源文件,改其中的 onchange方法

  /**
     * On blur, we consider the field no longer dirty, even if it were to be invalid.
     * However, if the field is invalid, the new value will not be committed to the model.
     */
    function onChange(ev) {
        if (isDirty) {
            .......
        }
        // add by fatux  20230615
        // 调用自己的函数
        _myOnChange(ev)
    }
    
    

  

	// add by fatux
    // 自动保存数据
    function _myOnChange(ev){
    
        console.log(ev)
    
        let uri = ev.srcElement.baseURI
        let arr = uri.split("&")
        if (arr.length>=2 && arr[arr.length-1]==="view_type=form" && arr[arr.length-2]==="model=hx.tsl.info" ){
            if (ev.srcElement.id==="scanner_no"){
                setTimeout(()=>{
                debugger
                    if ($("div[name='is_pass']").text() === "合格"){
                        $(".o_form_button_create").click()
                    }
                },2000)
            }
        }
    }

  

当scanner_no扫描完之后,后台会执行一些onchange方法,然后is_pass会显示合格或者不合格
如果合格就自动保存,这里做了一个2秒钟的延时。 如果不合格需要手工处理。