[IMP] survey_event_registration_generation: several dependant event questions in the same page (ajax)
This commit is contained in:
@@ -3,8 +3,17 @@
|
||||
|
||||
from odoo.addons.survey.controllers import main
|
||||
from odoo.http import request
|
||||
from odoo import http
|
||||
|
||||
class Survey(main.Survey):
|
||||
|
||||
@http.route(['/survey_event/get_events_from_product'], type='json', auth="public", methods=['POST'])
|
||||
def get_events_from_product(self, product_id, **kw):
|
||||
tickets = request.env['event.event.ticket'].sudo().search([('product_id','=',product_id)])
|
||||
events = set([ticket.event_id for ticket in tickets])
|
||||
return [{'id':event.id,'name':event.name} for event in events]
|
||||
|
||||
|
||||
def _prepare_survey_data(self, survey_sudo, answer_sudo, **post):
|
||||
result = super(Survey, self)._prepare_survey_data(survey_sudo, answer_sudo, **post)
|
||||
result['event_products'] = request.env['product.product'].sudo().search([('detailed_type','=','event')])
|
||||
|
@@ -40,7 +40,10 @@ class SurveyUserInput(models.Model):
|
||||
Save event product to user_input.line
|
||||
"""
|
||||
vals = self._get_line_answer_values(question, answer, question.question_type)
|
||||
if 'value_event_product' in vals and vals['value_event_product'].isnumeric():
|
||||
vals['value_event_product'] = int(vals['value_event_product'])
|
||||
else:
|
||||
vals['value_event_product'] = 0
|
||||
if old_answers:
|
||||
old_answers.write(vals)
|
||||
return old_answers
|
||||
@@ -52,7 +55,10 @@ class SurveyUserInput(models.Model):
|
||||
Save event to user_input.line
|
||||
"""
|
||||
vals = self._get_line_answer_values(question, answer, question.question_type)
|
||||
if 'value_event' in vals and vals['value_event'].isnumeric():
|
||||
vals['value_event'] = int(vals['value_event'])
|
||||
else:
|
||||
vals['value_event'] = 0
|
||||
if old_answers:
|
||||
old_answers.write(vals)
|
||||
return old_answers
|
||||
|
@@ -2,6 +2,11 @@ odoo.define('survey_event_registration_generation.survey.form', function (requir
|
||||
'use strict';
|
||||
|
||||
var SurveyFormWidget = require('survey.form');
|
||||
var ajax = require('web.ajax');
|
||||
var core = require('web.core');
|
||||
|
||||
var _t = core._t;
|
||||
var _lt = core._lt;
|
||||
|
||||
|
||||
SurveyFormWidget.include({
|
||||
@@ -19,6 +24,44 @@ SurveyFormWidget.include({
|
||||
return result;
|
||||
},
|
||||
|
||||
_onChangeChoiceItem: function (event) {
|
||||
var event_select = $('select[data-question-type="event"]:visible')
|
||||
|
||||
//Check if event product selection change
|
||||
if ($(event.currentTarget).data('questionType') == 'event_product') {
|
||||
//Check if event selection visible
|
||||
if (event_select) {
|
||||
//Disable event selection
|
||||
event_select.prop('disabled', 'disabled');
|
||||
|
||||
//Get event product id
|
||||
var eventProductId = parseInt($(event.currentTarget).find(":selected").val())
|
||||
|
||||
//Ajax : get new events
|
||||
ajax.jsonRpc('/survey_event/get_events_from_product', 'call', {
|
||||
'product_id': eventProductId,
|
||||
}).then((new_events) => {
|
||||
// Delete old events
|
||||
$(event_select).find('option').remove()
|
||||
|
||||
$(event_select).append(new Option(_t('Please select...')))
|
||||
|
||||
//Populate new events
|
||||
for (var i in new_events) {
|
||||
$(event_select).append(new Option(new_events[i].name, new_events[i].id))
|
||||
}
|
||||
|
||||
//Enable event selection
|
||||
event_select.prop('disabled', false);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var result = this._super.apply(this, arguments);
|
||||
return result;
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
|
@@ -15,7 +15,11 @@
|
||||
|
||||
<template id="question_event_product" name="Question: event product">
|
||||
<div class="o_survey_comment_container p-0">
|
||||
<select t-att-name="question.id" t-att-placeholder="question.question_placeholder" t-att-data-question-type="question.question_type">
|
||||
<select class="o_survey_form_choice_item"
|
||||
t-att-name="question.id"
|
||||
t-att-placeholder="question.question_placeholder"
|
||||
t-att-data-question-type="question.question_type">
|
||||
<option>Please select...</option>
|
||||
<t t-foreach="event_products" t-as="event_product">
|
||||
<option
|
||||
t-att-value="event_product.id"
|
||||
@@ -28,7 +32,11 @@
|
||||
|
||||
<template id="question_event" name="Question: event">
|
||||
<div class="o_survey_comment_container p-0">
|
||||
<select t-att-name="question.id" t-att-placeholder="question.question_placeholder" t-att-data-question-type="question.question_type">
|
||||
<select class="o_survey_form_choice_item"
|
||||
t-att-name="question.id"
|
||||
t-att-placeholder="question.question_placeholder"
|
||||
t-att-data-question-type="question.question_type">
|
||||
<option>Please select...</option>
|
||||
<t t-foreach="events" t-as="event">
|
||||
<option
|
||||
t-att-value="event.id"
|
||||
|
Reference in New Issue
Block a user