重新加载 div onchange 事件后,Odoo Javascript 不再触发

问题描述 投票:0回答:1

这是 Odoo 10.0 前端。 在我的“站点”中,当用户选择某些字段时,我会动态加载 div,并且这些 div 包含复选框和输入框。

当用户更改它们时,我有一些函数可以在数据库上写入新值。 我试图仅重新加载包含所有行而不是所有页面的 div,但如果我使用

则使用我的代码
$('#container_lines').load(location.href + " #container_lines"); 

而不是

window.location.reload();

如果我再次单击该复选框,则不会触发 onchange 事件!

这是Javascript

odoo.define('website_sale_order_checklist.checklist_line', function (require) {
        'use strict';

        var ajax = require('web.ajax');
        var core = require('web.core');
        var session = require('web.session');
        var Model = require('web.Model');
        var _t = core._t;

        //var body = $("body");

        //IF some ajax start the windows show a loading bar
        $(document).on({
            ajaxStart: function () {
                $("body").addClass("loading");
            },
            ajaxStop: function () {
                $("body").removeClass("loading");
            }
        });

        console.debug('website_sale_order_checklist');
        $(document).ready(function () {
            //If user select another stage save it
            function update_line_value(checklist_line_id, checklist_line_view_id, checklist_line_view_type) {

                //Take the id of the checklist line int
                var checklist_line_id_int = Number(checklist_line_id);
                //Take the value of the input tag for text and value

                //take the model
                var Checklists = new Model('sale.order.checklist.line');

                //if the selected view is a checklist, check is checked or not
                if (checklist_line_view_type === "checkbox") {
                    var checkboxValue = false;
                    if ($('#' + checklist_line_view_id).prop('checked') === true) {
                        checkboxValue = true;
                    } else {
                        checkboxValue = false;
                    }

                    if (checklist_line_view_id.match(/answer_yes_type/gi)) {
                        console.debug('Write on yes_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_yes: checkboxValue}]
                        ).then(function () {
                            //Call a method in backend that manage the subsections related to a section
                            Checklists.call('subsections_management_frontend', [checklist_line_id_int]).then(function () {
                                window.location.reload();
                            });
                        });
                    } else if (checklist_line_view_id.match(/answer_no_type/gi)) {
                        console.debug('Write on no_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_no: checkboxValue}]
                        ).then(function () {
                            //Reload full page
                            $('#container_lines').load(location.href + " #container_lines");
                        });
                    } else if (checklist_line_view_id.match(/answer_undecided_type/gi)) {
                        console.debug('Write on undecided_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_undecided: checkboxValue}]
                        ).then(function () {
                            //Reload
                            window.location.reload();
                            //$('#container_lines').load(location.href + " #container_lines");
                        });
                    }
                } else if (checklist_line_view_type === "text") {

                    var value = $('#' + checklist_line_view_id).val();

                    if (checklist_line_view_id.match(/answer_text_type/gi)) {
                        console.debug('Write on answer_text_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_text: value}]
                        ).then(function () {
                            //Reload full page
                            window.location.reload();
                        });
                    }

                } else if (checklist_line_view_type === "number") {

                    var value = $('#' + checklist_line_view_id).val();
                    if (checklist_line_view_id.match(/answer_value_type/gi)) {
                        console.debug('Write on answer_value_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_value: value}]
                        ).then(function () {
                            //Reload full page
                            window.location.reload();
                        });
                    }
                }

            }


            //Intercept witch row and view is clicked
            $('.section_line').on("change", "input, textarea", function () {

                //Get row id of checklist
                var checklist_line_id = $(this).parent().parent().attr('id');
                //Get the id of clicked element
                var checklist_line_view_id = $(this).attr('id');
                //Get the type of clicked element
                var checklist_line_view_type = $(this).attr('type');

                console.debug('checklist_line_id: ' + checklist_line_id);
                console.debug('checklist_line_view_id: ' + checklist_line_view_id);
                console.debug('checklist_line_view_type: ' + checklist_line_view_type);

                //Function that store the changed field
                update_line_value(checklist_line_id, checklist_line_view_id, checklist_line_view_type);

            });

            //Intercept if the user click some Sections to add to checklist
            $('.sectionAddRemove').on("click", function () {
                setTimeout(function () {
                    window.location.reload();
                    //$('#container_lines').load(location.href + " #container_lines");
                    //$('#container_sections').load(location.href + " #container_sections");

                }, 100);

                //$('#container_lines').load(location.href + " #container_lines");
            });
        });

    }
);

我这样做哪里错了? 我的目标是仅重新加载包含行而不是整个页面的 div。

javascript jquery html odoo-10
1个回答
0
投票

解决了问题。

//Intercept witch row and view is clicked
        $('.section_line').on("change", "input, textarea", function () {

            //Get row id of checklist
            var checklist_line_id = $(this).parent().parent().attr('id');
            //Get the id of clicked element
            var checklist_line_view_id = $(this).attr('id');
            //Get the type of clicked element
            var checklist_line_view_type = $(this).attr('type');

            console.debug('checklist_line_id: ' + checklist_line_id);
            console.debug('checklist_line_view_id: ' + checklist_line_view_id);
            console.debug('checklist_line_view_type: ' + checklist_line_view_type);

            //Function that stores the changed field
            update_line_value(checklist_line_id, checklist_line_view_id, checklist_line_view_type);

        });

$('.section_line').on("change"...
.section_line
位于 div 内部
$('#container_lines')
,因此当 div 重新加载时,它会丢失处理程序。

只需将

$('.section_line').on("change"...
更改为
$(document).on("change"...
或包含container_lines的高容器

© www.soinside.com 2019 - 2024. All rights reserved.