簡體   English   中英

從odoo 12中的javascript中選擇選擇字段的值

[英]Select value of selection field from javascript in odoo 12

我想在 odoo 12 中使用 javascript 或 jquery 選擇選擇字段的值。例如,我有一個來自 javascript 的選擇值,我想選擇或設置選擇字段的值。 為此,我正在編寫以下代碼:

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);

但是在選擇字段中沒有選擇值。 任何人都可以幫忙。

更新

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

在此處輸入圖片說明

要更新字段值,您需要觸發 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});

此事件由最后的BasicModel處理以更新每條記錄。

在您的示例中,事情更復雜,因為您想從 one2many 中的字段轉到父模型,不幸的是,在問題字段的小部件中,我們沒有父記錄的 ID,只有頁面模型的 ID 或不管叫什么。 所以我使用了一個新的自定義 odoo 事件(我將其命名為badge_clicked )從父小部件( One2many field widget )處理它,在這個小部件中我們有父記錄的 ID。 希望評論為您清除事情:

/*
   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
           });
        }
     },
});

注意:我嘗試了這個並且它工作得很好我通過單擊tax標簽更改了發票模型的comment字段,想法是從單擊事件處理程序觸發一個 Odoo 事件並在父小部件處處理該事件女巫是one2many能夠使用正確的ID觸發field_change事件。 我希望這對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM