This question has been flagged
1 Reply
3501 Views

i am creating a new website form 

in that i have 2 many2many fields with multiple selections i wanna use many2many_tags widget but i am not able to use it please help me guys how do i use that widget.

my xml code:

<div t-if="user_id.sudo().is_student" class="row col-md-12">
<div class="col-md-2 mt8">
<lable for="compulsory_subject_ids">Compulsory Subjects:</lable>
</div>
<div class="col-md-10" style="padding-left: 5px;">
<select class="form-control "
name="compulsory_subject_ids" multiple="True" >
<t t-foreach="student_id" t-as="course">
<t t-foreach="course.course_id.subject_ids" t-as="subject">
<option t-att-value="subject.id">
<t t-esc="subject.name"/>
</option>
</t>
</t>
</select>
</div>
</div>
<div t-if="user_id.sudo().is_student" class="row mt8 col-md-12">
<div class="col-md-2 mt8">
<lable for="elective_subject_ids">Elective Subjects:</lable>
</div>
<div class="col-md-10" style="padding-left: 5px;">
<select class="form-control " name="elective_subject_ids"
required="1">
<t t-foreach="subjects" t-as="subject">
<t t-if="subject.subject_type == 'elective'">
<option t-attf-value="subject.id">
<t t-esc="subject.name"/>
</option>
</t>
</t>
</select>
</div>
</div>
Avatar
Discard
Best Answer

Hi Paidy,

In Odoo, they use select2 library to create many2one & many2many selection, you can try to use this library to make your website form do the same.

Here is the sample code:

Xml:

<input type="text" class="many2many_tags"/>

Javascript:

$("input.many2many_tags").select2({
    minimumInputLength: 1,
    multiple: true,
    query: function (q) {
        var model = new Model('courses');

        var def = model.call("name_search", [], {
            name: q.term,
            limit: 500,
        });

        def.then(function(res){
            var results = _.map(res, function (r) {
                return { id: r[0], text: r[1] }
            });
            q.callback({
                results: results,
                more: false
            });
        });
    }
});

You can also check select2 documentation here:

https://select2.org/

Avatar
Discard