2015-03-13 72 views
2

如何根据catalog.results动态添加表单域?z3c.form动态添加字段catalog.results

例如:

catalog.results = ['Channel A', 'Channel B', 'Channel C',] 

form.fields应该是

form.fieldset Channels A { 
    input[type=checkbox].course a1 
    input[type=checkbox].course a2 
    input[type=checkbox].course a3 
} 
form.fieldset Channels B { 
    input[type=checkbox].course b1 
    input[type=checkbox].course b2 
    input[type=checkbox].course b2 
} 

每个信道是folderish,每个通道可含有N-课程,对于每个信道应该是一个字段和用于每赛道应该是一个输入[type = checkbox](或MultiCheckbox)Field generated

对不起,我更新了我的问题,因为我们的设计师发错了图片

回答

1

我不会建议为每个选项创建一个单独的字段。

您可以在z3c.form定义列表字段与CheckBoxFieldWidget

from zope import schema 
from plone.directives import form 
from z3c.form.browser.checkbox import CheckBoxFieldWidget 


class IChannels(form.Schema) 

    form.widget(channels=CheckBoxFieldWidget) 
    channels = schema.List(
     title=_(u'label_channels', default='Channels'), 
     value_type=schema.Choice(
      vocabulary=u'channels.vocabulary'), 
     required=False) 

现在注册vobaulary,名channels.vocabulary,它返回基于目录的查询词。

from zope.interface import directlyProvides 
from zope.schema import vocabulary 
from zope.schema.interfaces import IVocabularyFactory 


def channels_vocabulary(context): 
    catalog = getToolByName(context, 'portal_catalog') 
    terms = [] 
    query = {} # Your query 
    for term in catalog(**query): 
     terms.append(vocabulary.SimpleTerm(value=term.decode('utf8'), 
           token=normalizer.normalize(term.decode('utf8')), 
           title=term.decode('utf8'))) 
    return vocabulary.SimpleVocabulary(terms) 

directlyProvides(channels_vocabulary, IVocabularyFactory) 

注册与ZCML:

<utility 
    component=".vocabularies.channels_vocabulary" 
    name="channels.vocabulary" 
/> 

的例子是基于http://docs.plone.org/develop/plone/forms/schemas.html#multi-choice-example

+0

谢谢,可能是一个好主意,但我要问我们的设计师.... – user966660 2015-03-13 14:11:20

+0

你需要每个复选框周围都有一个单独的字段集? – Mathias 2015-03-13 14:26:58

+0

对不起,我更新/更正了我的问题.... – user966660 2015-03-13 15:10:29