Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
9 Trả lời
18703 Lượt xem

I try to add an onchage function on a field.selection, but it doenst work, here the code:

class my_class(osv.osv):

    _name = "my.class"

    _columns = {
        'field1': fields.selection([('1', 'A'), ('1', 'B')], 'my filed 1'),
        'field2': fields.boolean('My field 2'),
    }

    def onchange_field1(self, cr, uid, ids, field1, field2, context=None):
        if field1=='A':
            v = {'field2': 1}
            return {'value': v}
        return {}
res_partner()

in xml file :

 <field name="field1" on_change="onchange_field1(field1, field2)"/>
<field name="field2"/>
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Your are checking field1 with the label 'A' check it with the value '1'. Change your code to:

if field1=='1':

Now it will work.

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks Nishant

My Pleasure!

Nic,1 vote

Câu trả lời hay nhất

It's not neccesary to send the field2, you can do this:

class my_class(osv.osv):

_name = "my.class"

_columns = {
    'field1': fields.selection([('1', 'A'), ('1', 'B')], 'my filed 1'),
    'field2': fields.boolean('My field 2'),
}

def onchange_field1(self, cr, uid, ids, field1):
    if field1=='1':
        v = {'field2': True}
        return {'value': v}
    return {}

res_partner()

in xml file :

<field name="field1" on_change="onchange_field1(field1)"/>
<field name="field2"/>

As you can see you have to compare field1 with the key not with the name

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks Grover..it works now

Nic,1 tick & 1 vote

Câu trả lời hay nhất

Hello,

I need to change my slection elements after change field1 .

this code doesn't work , who has an idea how to fix it ?

_columns = {

'field1': fields.selection([('1', 'A'), ('1', 'B')], 'my filed 1'),

'field2': fields.selection([('aa', 'C'), ('bb', 'D')], 'my filed 2'),

}

def onchange_field2(self, cr, uid, ids,field1,context=None):

    lst= [('5', '5'), ('6', '6'), ('7', '7'), ('8', '8')]

    try:

        lst.remove([item for item in lst if item[0] == '5'][0])

    except IndexError as e:

        pass

    v = {'field2': lst}

    return {'value': v}

thank you

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

What if I have to change string of boolean field instead of value?How will I then achieve?

Ảnh đại diện
Huỷ bỏ