美文网首页pythonodoo
odoo12 在页面按钮js调用路径

odoo12 在页面按钮js调用路径

作者: 隔壁小红馆 | 来源:发表于2020-01-18 15:30 被阅读0次
    • 第一种写法
    # xml 文件(form表单)
    <div class="media-body">
                <form class="o_portal_chatter_composer_form" t-attf-action="/product/comment" method="POST">
                    <div class="mb32">
                        <t t-set="attributes" t-value="my_order._get_my_order_sale(my_order)"/>
                        <t t-if="not attributes">
                            <textarea rows="4" name="message" class="form-control"
                                      placeholder="Write a message..."></textarea>
                            <input type="hidden" name="order" t-att-value="my_order.id"/>
                            <button t-attf-class="o_product_comment_btn btn btn-primary mt8
                                    o_website_message_post_helper" type="submit">Send
                            </button>
                        </t>
                    </div>
                </form>
            </div>
    
    # js文件
    var ajax = require('web.ajax');
    var core = require('web.core');
    
    $('button.o_product_comment_btn').on('click', function (event) {
            event.stopPropagation();
            ajax.rpc('/product/comment', {
                id: $('#orderId').val(),
                message:$('#message').val(),
                action: 'submit',
            }).then(function (res) {
                return $.Deferred();
            });
        });
    
    # py文件
    class ProductCommentMessage(http.Controller):
        @http.route('/product/comment', type='http', methods=['POST'], auth='public', csrf=False)
        def product_comment(self, **kw):
            url = request.httprequest.referrer
            sale_order = request.env()['sale.order'].sudo().search([('id', '=', int(kw['order']))])
            data = {'name': sale_order.name, 'author_id': request.env.user.partner_id.id, 'body': kw['message'],
                    'res_id': sale_order.id, 'model': 'sale.order', 'create_date': datetime.date.today()}
            request.env()['product.comment.message'].sudo().create(data)
            return redirect(url)   #return 刷新当前页面
    
    • 第二种写法
      通过按钮 ID 去调用方法及路径,获取验证码按钮,开始倒计时


      图片.png
    图片.png
    #py文件里
    @http.route(['/validate/signup/'], type='json', auth='public', method=['POST'])
        def phone_signup(self, **kw):
            if len(request.params.get('login')) == 11:
                validate = self.validate_phone(request.params.get('login'))
                request.session['_phone_validate'] = validate[0]
                request.session['_validate_time'] = fields.Datetime.now()
                if validate[1] == 'OK':
                    return {
                        'status': 'OK',
                        'msg': _('params'),
                        'validates': validate[0]
                    }
                else:
                    return {
                        'status': 'No',
                        'msg': _('params'),
                        'validates': validate[0]
                    }
    
    #js 文件里
    document.getElementById("codeBtn").onclick = function () {
            var login = $('#login').val();
            var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
            if (!myreg.test(login)) {
                alert("请输入正确的手机号码!")
                return false;
            }
            time(this, login);
            if (login.length === 11) {
                ajax.rpc('/validate/signup/', {
                    id: $('#orderId').val(),
                    login: login,
                }).then(function (res) {
                    if (res['status'] == 'OK') {
                        $('#validates').val(res['validates']);
                    } else {
                        alert('获取验证码失败');
                    }
                });
            } else {
                return false
            }
        }
    
    var wait = 60;
        function time(btn, login) {
            if (login.length === 11) {
                if (wait == 0) {
                    btn.removeAttribute("disabled");
                    btn.value = "重新获取"
                    wait = 60;
                } else {
                    btn.setAttribute("disabled", true);
                    btn.value = "重新获取(" + wait + ")";
                    wait--;
                    setTimeout(function () {
                        time(btn, login)
                    }, 1000);
                }
                ;
            } else {
                console.log(login);
                return false
            }
        }
    
    

    相关文章

      网友评论

        本文标题:odoo12 在页面按钮js调用路径

        本文链接:https://www.haomeiwen.com/subject/qmikzctx.html