[ADD] survey_dropdown_simple_choice
Display "Multiple choice: only one answer" (simple_choice) survey questions as a dropdown list instead of radio buttons, per question. The select is mirrored on hidden radios so that the native survey form pipeline (validation, conditional questions, "Other" comment, AJAX submit, correct answers display) keeps working. Native radio rendering is kept when suggested answers contain images and in readonly mode. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
96
survey_dropdown_simple_choice/views/survey_templates.xml
Normal file
96
survey_dropdown_simple_choice/views/survey_templates.xml
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<!-- Redirect the render of simple_choice questions to the dropdown
|
||||
widget when the display_dropdown flag is set. The native radio
|
||||
rendering is kept when a suggestion carries an image, and in
|
||||
readonly mode (print / review) so that corrections are displayed. -->
|
||||
<template id="question_container" inherit_id="survey.question_container" name="Survey question container: dropdown dispatch">
|
||||
<xpath expr="//t[@t-if="question.question_type == 'simple_choice'"]" position="replace">
|
||||
<t t-if="question.question_type == 'simple_choice'">
|
||||
<t t-if="question.display_dropdown and not answers_contain_image and not survey_form_readonly"
|
||||
t-call="survey_dropdown_simple_choice.question_simple_choice_dropdown"/>
|
||||
<t t-else="" t-call="survey.question_simple_choice"/>
|
||||
</t>
|
||||
</xpath>
|
||||
</template>
|
||||
|
||||
<!-- Dropdown rendering for simple_choice questions.
|
||||
The native JS (survey_form.js) still reads its answers from the
|
||||
checked radios inside the .o_survey_form_choice wrapper:
|
||||
the select and a hidden mirror group of radios coexist, a JS patch
|
||||
keeps them in sync, so conditional questions, score display,
|
||||
auto-submit and the 'other' comment keep working. -->
|
||||
<template id="question_simple_choice_dropdown" name="Question: simple choice (dropdown)">
|
||||
<t t-set="answer_line" t-value="answer_lines.filtered(lambda line: line.suggested_answer_id)"/>
|
||||
<t t-set="comment_line" t-value="answer_lines.filtered(lambda line: line.value_char_box)"/>
|
||||
<t t-set="other_selected" t-value="comment_line and not answer_line"/>
|
||||
<div class="o_survey_answer_wrapper o_survey_form_choice"
|
||||
t-att-data-name="question.id"
|
||||
t-att-data-is-skipped-question="is_skipped_question or None"
|
||||
data-question-type="simple_choice_radio">
|
||||
<div class="row g-2 o_survey_dropdown_select">
|
||||
<div class="col-sm-12">
|
||||
<select class="form-select o_survey_dropdown_choice"
|
||||
t-att-name="question.id">
|
||||
<option value="" t-att-selected="'selected' if not answer_line and not other_selected else None"/>
|
||||
<t t-foreach="question.suggested_answer_ids" t-as="label">
|
||||
<t t-set="answer_selected" t-value="answer_line and answer_line.suggested_answer_id.id == label.id"/>
|
||||
<option t-att-value="label.id" t-att-selected="'selected' if answer_selected else None"
|
||||
t-out="label.value"/>
|
||||
</t>
|
||||
<t t-if="question.comments_allowed and question.comment_count_as_answer">
|
||||
<option value="-1" class="o_survey_dropdown_other_option"
|
||||
t-att-selected="'selected' if other_selected else None"
|
||||
t-out="question.comments_message or default_comments_message"/>
|
||||
</t>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Hidden mirror radios: no 'name' attribute on purpose, they must
|
||||
not flow into the submitted form data (the select carries the
|
||||
answer for validation); only their checked state is read by
|
||||
_prepareSubmitChoices. They are shown in place of the select
|
||||
when correct answers are displayed after the page. -->
|
||||
<div class="row g-2 d-none o_survey_dropdown_mirror">
|
||||
<t t-foreach="question.suggested_answer_ids" t-as="label">
|
||||
<t t-set="answer_selected" t-value="answer_line and answer_line.suggested_answer_id.id == label.id"/>
|
||||
<div class="col-sm-12">
|
||||
<label t-attf-class="o_survey_choice_btn py-1 px-3 w-100 h-100 rounded #{'o_survey_selected' if answer_selected else ''}">
|
||||
<i class="fa fa-check-circle float-end mt-1 ms-1 position-relative"/>
|
||||
<i class="fa fa-circle-thin float-end mt-1 ms-1 position-relative"/>
|
||||
<input type="radio" t-att-value="label.id"
|
||||
t-att-id="str(question.id) + '_' + str(label.id)"
|
||||
t-attf-class="o_survey_form_choice_item invisible position-absolute #{'o_survey_form_choice_item_selected' if answer_selected else ''}"
|
||||
t-att-checked="'checked' if answer_selected else None"/>
|
||||
<span class="ms-2 text-break" t-field="label.value"/>
|
||||
</label>
|
||||
</div>
|
||||
</t>
|
||||
<t t-if="question.comments_allowed and question.comment_count_as_answer">
|
||||
<div class="col-sm-12">
|
||||
<label t-attf-class="o_survey_choice_btn py-1 px-3 h-100 w-100 rounded #{'o_survey_selected' if other_selected else ''}">
|
||||
<i class="fa fa-check-circle float-end mt-1 ms-1 position-relative"/>
|
||||
<i class="fa fa-circle-thin float-end mt-1 ms-1 position-relative"/>
|
||||
<input type="radio" value="-1"
|
||||
t-attf-class="o_survey_form_choice_item o_survey_js_form_other_comment invisible position-absolute #{'o_survey_form_choice_item_selected' if other_selected else ''}"
|
||||
t-att-checked="'checked' if other_selected else None"/>
|
||||
<span class="ms-2" t-out="question.comments_message or default_comments_message"/>
|
||||
</label>
|
||||
</div>
|
||||
</t>
|
||||
</div>
|
||||
<!-- Comment areas: exact copy of the native structure so that
|
||||
_applyCommentAreaVisibility and _prepareSubmitComment work. -->
|
||||
<t t-if="question.comments_allowed and question.comment_count_as_answer">
|
||||
<div t-attf-class="o_survey_comment_container mt-3 py-0 px-1 h-auto #{'d-none' if not comment_line else ''}">
|
||||
<textarea type="text" class="form-control o_survey_question_text_box bg-transparent rounded-0 p-0"
|
||||
t-att-disabled="None if comment_line else 'disabled'"><t t-esc="comment_line.value_char_box if comment_line else ''"/></textarea>
|
||||
</div>
|
||||
</t>
|
||||
<div t-if="question.comments_allowed and not question.comment_count_as_answer" class="mb-2 o_survey_comment_container mt-3">
|
||||
<textarea type="text" class="col form-control o_survey_comment o_survey_question_text_box bg-transparent rounded-0 p-0"
|
||||
t-att-placeholder="question.comments_message or default_comments_message if not survey_form_readonly else ''"><t t-esc="comment_line.value_char_box if comment_line else ''"/></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user