简体   繁体   English

从odoo 12中的javascript中选择选择字段的值

[英]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.我想在 odoo 12 中使用 javascript 或 jquery 选择选择字段的值。例如,我有一个来自 javascript 的选择值,我想选择或设置选择字段的值。 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:要更新字段值,您需要触发 Odoo 事件field_change传递您要更改的记录 ID 和将更改的字段以及其他字段的一些选项:

    // 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.此事件由最后的BasicModel处理以更新每条记录。

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.在您的示例中,事情更复杂,因为您想从 one2many 中的字段转到父模型,不幸的是,在问题字段的小部件中,我们没有父记录的 ID,只有页面模型的 ID 或不管叫什么。 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.所以我使用了一个新的自定义 odoo 事件(我将其命名为badge_clicked )从父小部件( One2many field widget )处理它,在这个小部件中我们有父记录的 ID。 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 .注意:我尝试了这个并且它工作得很好我通过单击tax标签更改了发票模型的comment字段,想法是从单击事件处理程序触发一个 Odoo 事件并在父小部件处处理该事件女巫是one2many能够使用正确的ID触发field_change事件。 I hope you this helps you我希望这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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