2012-02-19 3 views
2

나는 선택 위젯이있는 CRUD 양식이 있습니다. 위젯의 옵션은 동적입니다. 다른 테이블에서 값을 가져 오므로 태그를 사용할 수 없습니다. 나는이 방법을 사용하여 컨트롤러의 값을 변경하려고 : 나는 메소드를 호출하고 양식을 검사web2py : 가벼운 형태로 위젯 수정

def change_widget(form, id, widget): 
"Tries to find a widget in the given form with the given id and swaps it with the given widget" 
for i in range(len(form[0].components)): 
    if hasattr(form[0].components[i].components[1].components[0], 'attributes'): 
     if '_id' in form[0].components[i].components[1].components[0].attributes: 
      if form[0].components[i].components[1].components[0].attributes['_id'] == id: 
       form[0].components[i].components[1].components[0] = widget 
       return True 

return False 

후, 나는 양식이 성공적으로 수정 된 것을 볼 수 있습니다. 뷰 측면에서, 나는 사용자 지정보기를 사용하는 등 같은 폼을 표시하기 위해 노력하고있어 :

{{=form.custom.begin}} {{=form.custom.widget.customized_field}} 
{{=form.custom.submit}} {{=form.custom.end}} 

을하지만, 그것은 여전히 ​​내게는 수정되지 않은 원래 위젯을 보여줍니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 이 작업을 수행하는 더 좋은 방법이 있습니까?

답변

4

먼저, 여기에 위젯을 대체하기 훨씬 쉬운 방법 : 당신은 완전히 새로운 위젯 개체를 원래의 위젯을 대체하는 것이 아니라 단순히 원래의 위젯을 수정하고 있기 때문에, 그러나

form.element(_id=id).parent[0] = widget 

form.custom.widget.customized_field은 여전히 ​​참조 할 원래 위젯에. 따라서 form.custom.widget.customized_field을 명시 적으로 바꾸어야합니다. 시도 :

def change_widget(form, name, widget): 
    form.element(_name=name).parent[0] = form.custom.widget[name] = widget 

여기에서 name은 필드 이름입니다.

서버 측 DOM 검색에 대한 자세한 내용은 here을 참조하십시오.

db.define_table('mytable', Field('myfield', widget=custom_widget)) 

또는

db.mytable.myfield.widget = custom_widget 

을 그런 때 또한

, 당신은 나중에 양식도 생성되기 전에 두 테이블 정의 내에서 필드에 대한 사용자 정의 위젯을 지정하거나 할 수 있습니다 양식을 만들면 기본 위젯보다는 사용자 정의 위젯이 사용됩니다.

위젯에 대한 자세한 내용은 here을 참조하십시오.