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

时间:2018-06-08 10:57:49

标签: javascript jquery html odoo-10

这是Odoo 10.0 FrontEnd。 在我的网站"当用户选择一些字段并且那些div包含复选框和输入框时,我会动态加载div。

当用户更改它们时,我有一些在DB上写入新值的函数。 我试图仅重新加载包含所有行而不是所有页面的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。

1 个答案:

答案 0 :(得分: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 store 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"...或包含container_lines的高容器更改$(document).on("change"...