简体   繁体   中英

Select value of selection field from javascript in odoo 12

I want to select the value of the selection field using javascript or jquery in odoo 12. eg I have a value of selection from javascript I want to select or set the value of the selection field. For that I am writing the following code :

var type_option = result['type'];
$('.select_question select').find('option[value="free_text"]').show();
$('.select_question select').val('option[value="free_text"]');          
$('.select_question select').val(type_option);
$('.select_question select').text(type_option);

But in selection field value is not selected. can any one please help.

Update

var FieldMany2ManyTags = relationalField.FieldMany2ManyTags.include({   
    supportedFieldTypes: ['selection'],
    events: _.extend({}, relationalField.FieldMany2ManyTags.prototype.events, {
        'click .badge': '_onClickTag',
    }),

    _onClickTag: function(event){
        event.preventDefault();
            event.stopPropagation();
        var self = this;
        var id = $(event.target).parent().data('id');

        var data = this._rpc({
            model: 'survey.survey',
            method: 'get_id',
            args: [id],
        }).then(function (result) { 


            //Display Question In question field
            $('.question_class').val(result['question']);   

            //Show selection field
            var type_option = result['type'];//selection field value
            var type_option = result['type'];
            $('.select_question select').find('option[value="free_text"]').show();
            $('.select_question select').val('option[value="free_text"]');          
            $('.select_question select').val(type_option);
            $('.select_question select').text(type_option);             


            //For check Box 
            $('.mandatory_class .custom-control-input input').val(result['constr_mandatory']);

            if(result['constr_mandatory'] === true){

                 var $checkbox = $('.custom-control-input');
                         $checkbox.prop('checked', true);

                if ($checkbox.is(':checked')) {
                            $('.mandatory_msg_class').val(result['constr_error_msg']);
                    $('.o_form_label').show();                  
                    $('.mandatory_msg_class').show();       
                     }
            }else{

                var $checkbox = $('.custom-control-input');
                         $checkbox.prop('checked', false);
                if ($checkbox.not(':checked')) {                                    
                    $('.mandatory_msg_class').hide();           
                     }//close if
            }

        });

        return data;

    },
});//close FieldMany2ManyTags

在此处输入图片说明

To update a field value you need to trigger Odoo event field_change passing the record ID that you want to change and the field that will be changed along with some option for other fields:

    // this is the widget object
    this.trigger_up('field_changed', 
                    dataPointID: this.dataPointID,
                    changes: changes, // change is an object {field_name: value, other_field_name: value}
                    viewType: this.viewType});

This event is handled by the BasicModel at the end to update each record.

In you example things is more complicated because you want to go from a field in your one2many to the parent model, unfortunately in the widget of the question field we don't have the ID of the parent record only the ID of the page model or whatever is called. so I used a new custom odoo event (I named it badge_clicked ) to handle it from the parent widget ( One2many field widget ) in this widget we have the ID of the parent record. Hope comments clears things for you:

/*
   Handle event trigged by the method that handle the badge click event
*/
FieldX2Many.include({
    custom_events: _.extend({}, FieldX2Many.prototype.custom_events, {
          badge_clicked: '_OnBadgeClicked',
    }),
    _OnBadgeClicked : function(ev){
        // do this only in edit mode
        if (this.mode != 'edit')
                return undefined;

        var result = ev.data.result; 
        var changes = {};
        // this will trigger an update on the field it self,
        // odoo will wait for the type of operation

        //Display Question In question field ( I don't know the field names)
        // Very Big Note: the values must be valid you cannot give an interger value to char field for example
        changes['field_name'] = result['question'];
        changes['some_other_field'] = result['some_ther_field_to_set'];
        // if you want to check some field of the current record don't use JQuery to retrieve the value use the record attribute of the widget.
        // if (this.record.data['some_field'] === 'some_value'){
        //        do some thing
        // }      

        // this is required(a dummy update to avoid unexpected behavior by the field_changed event)
        changes[this.name] = {
                operation: 'UPDATE',
                id: ev.data.dataPointID,
                changes: {} };


        // we did all of this just to trigger the field_changed from the One2many field because in the many2many tags we don't have the ID of the parent record.
        this.trigger_up('field_changed', {
            dataPointID: this.dataPointID,  // here the ID is the parent(global) record
            changes: changes,
            viewType: this.viewType});
     },
});

/*
   Handle click event to inform the parent widget.
*/
FieldMany2ManyTags.include({
    events: _.extend({}, FieldMany2ManyTags.prototype.events, {
        'click .badge': '_onClickTag',
    }),
     _onClickTag: function(event){
            var self = this;
            event.preventDefault();
            event.stopPropagation();
            // deactivate this for other model.
            // I tried the same thing with account.invoice and it worked perfectly
            // I don't know the name of the model in your one2many field in my case is invoice.line ^^
            if (self.model != 'account.invoice.line')
                return undefined;
            var self = this;
        var id = $(event.target).parent().data('id');

        var data = this._rpc({
            model: 'survey.survey',
            method: 'get_id',
            args: [id],
        }).then(function (result) { 
           // trigger an event that will be handled by the parent widget
           self.trigger_up('badge_clicked', {
                result: result,
                // when we click the One2many field in edit mode, a field_change will be trigger
                // we need the ID of this record to trigger a dummy update
                dataPointID: self.dataPointID,  // here the ID is the record in the line of one2many field and we don't have the ID of the parent record at this point this why we are triggering this event to be handled by the one2many
           });
        }
     },
});

Note : I tried this and it worked perfectly I changed the comment field of the invoice model by clicking on the tax tags, the Idea is to trigger an Odoo event from the click event handler and handle that event at the parent widget witch is one2many to be able to trigger field_change event using the correct ID . I hope you this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM