簡體   English   中英

如何為組(字段集)內的字段自定義z3c.form小部件

[英]How to customize z3c.form widget for fields inside groups (fieldsets)

我有一個Plone定制控制面板注冊表,我正在嘗試使用一種眾所周知的方法來定制zope.schema.Textzope.schema.TextField一些小部件屬性。

我通常以這種方式自定義updateWidgets

def updateWidgets(self):
    super(MyEditForm, self).updateWidgets()
    self.widgets['my_text_area'].style = 'width: 100%'
    self.widgets['my_text_area'].rows = 7

但是現在我正在處理將字段分為兩個字段集的表單:

class MySettingsEditForm(controlpanel.RegistryEditForm):
    schema = IMySettingsSchema
    groups = (Form1, Form2)
    # fields = nothing

如果我嘗試訪問self.widgets['my_text_area']我得到KeyError 似乎因為我沒有定義fields屬性,所以無法直接訪問小部件。

我發現我有groups因此可以調用諸如self.groups[0].fields['my_text_area']但仍然找不到訪問組內字段的窗口小部件的方法。

使用組時如何自定義小部件屬性?

我認為您需要使用的是小部件子窗體,請參見以下代碼:

def fix_table_widget(self, name, widgets):
    sub_widgets = widgets[name].widgets
    for widget in sub_widgets:
        new_label = widget.subform.widgets['weekday'].value
        widget.subform.widgets['selected'].items[0]['label'] = new_label
        widget.subform.widgets['weekday'].mode = 'hidden'

def schoolrequest_customizations(self):
    ''' Customizations for the schoolrequest base views
    '''
    for group in self.groups:
        widgets = group.widgets
        if 'table_bus_to_school' in widgets:
            self.block_widget_table('table_bus_to_school', widgets)
            self.fix_table_widget('table_bus_to_school', widgets)

        if 'table_bus_to_home' in widgets:
            self.block_widget_table('table_bus_to_home', widgets)
            self.fix_table_widget('table_bus_to_home', widgets)

def update(self):
    super(MyForm, self).update()
    self.schoolrequest_customizations()

暫無
暫無

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

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