我有一个很少领域的向导。最初它只显示一个选择字段,并根据选择的内容(我使用过 on_change),它会显示其他字段。
现在我在这种情况下遇到问题。用户最初选择“选择 1”并显示(比如说)姓名、地址、年龄并为该字段赋值,但用户决定将初始字段更改为“选择 2”,这将隐藏其他字段并显示字段主题,时间,导师。但是当我再次尝试选择“选择 1”时,这些值不会被删除。
我想删除隐藏字段的现有值。
你对如何做到这一点有任何想法吗?
谢谢,
您可以为选择类型字段创建 on_change 方法。
在该方法中,您可以根据选择字段值返回字段的 '' 值。
i.e if select=1 then return {'value':{'subject':'', 'time':'' , 'instructure': False}}
& if select=2 then return {'value':{'name':'', address:'' , 'age': 0}}
希望这会帮助你。
def get_status_reason(self, cr, uid, field_val, question_id, status_reason_id=False):
pool = self.pool.get
question_obj = pool('jfs.question.template.answer').read(cr, uid, question_id, ['name', 'status_reason_id','question_template_ids'])
if str(question_obj['name']) != field_val:
for child_question in question_obj['question_template_ids']:
status_reason_id = self.get_status_reason(cr, uid, field_val, child_question, status_reason_id)
else:
if question_obj['status_reason_id']:
status_reason_id = question_obj['status_reason_id'][0]
else:
status_reason_id = False
return status_reason_id
def onchange_field_val(self, cr, uid, ids, field_val, task_id, status_reason_id):
pool = self.pool.get
task_obj = pool('aybabtu.task').read(cr, uid, task_id, ['question_set_id'])
status_reason_id = self.get_status_reason(cr, uid, field_val, task_obj['question_set_id'][0], status_reason_id)
res = {'status_reason_id':status_reason_id}
#self.get_child_questions(cr, uid, field_val, task_id)
return {'value':res}
def generate_xml(self, cr, uid, view_type, data, sorted_field_list, result, context):
xml = '''<?xml version="1.0"?>
<%s>''' % (view_type)
context_val = {}
context_val['task_id'] = context['active_id']
task_obj = self.pool.get('aybabtu.task').read(cr, uid, context_val['task_id'], ['state'])
for field_name in sorted_field_list:
attribute = {}
context_val[field_name] = str(field_name)
#print "result['fields'][field_name]", result['fields'][field_name]
if result['fields'][field_name]['context'] != 'none' and result['fields'][field_name]['context'] != 'task' and eval(result['fields'][field_name]['context'])[4] == 'required':
require = 'required="1"'
else:
require = ''
if result['fields'][field_name]['context'] != 'none' and result['fields'][field_name]['context'] != 'task' and eval(result['fields'][field_name]['context'])[7] and task_obj['state'] in ['done']:
readonly = 'readonly="1"'
else: readonly = ''
if result['fields'][field_name]['context'] != 'none' and result['fields'][field_name]['context'] != 'task' and eval(result['fields'][field_name]['context'])[5] == 'widget':
widget = 'widget="selection"'
filter = "context=\"{'custom_domain_timeslot':[ticket_id,type_id,state]}\""
else:
widget = ''
filter = ''
if field_name == str(data['name']):
xml += '''<field name="%s" select="1" required="1" nolabel="1" on_change="onchange_field_val(%s,task_id, status_reason_id)" colspan="4" sequence="1" %s/>''' % (field_name, field_name, readonly)
elif result['fields'][field_name]['context'] == 'none':
xml += '''<field name="%s"/>''' % (field_name)
elif result['fields'][field_name]['context'] == 'task':
xml += '''<field name="%s" invisible="1"/>''' % (field_name)
else:
attribute['invisible'] = [(eval(result['fields'][field_name]['context'])[2],'!=',eval(result['fields'][field_name]['context'])[1])]
if require == 'required="1"':
attribute['required'] = [(eval(result['fields'][field_name]['context'])[2],'=',eval(result['fields'][field_name]['context'])[1])]
if result['fields'][field_name]['context'] != 'none' and result['fields'][field_name]['context'] != 'task' and eval(result['fields'][field_name]['context'])[5] == 'widget':
filter_option = self.get_domain(cr, uid, context_val['task_id'], result['fields'][field_name]['context'])
xml += '''<field name="%s" attrs="%s" colspan="4" on_change="onchange_field_val(%s,task_id, status_reason_id)" %s %s %s/>''' % (field_name, attribute, field_name, widget, filter, readonly)
xml += '''<button name="question_save_task" string="Save and Close Task" colspan="4" type="object" icon="gtk-convert" context="%s"/>''' % (context_val)
xml += '''</%s>''' % (view_type,)
return xml
@anoma我还重载了调用 generate_xml() 的 field_view_get() ,其中不可见的字段属性也是动态的