首页 文章

Web2py - 多选项复选框实现

提问于
浏览
2

我正在尝试实现一个表单,其中每个字段都有多个选项,它必须是复选框的行 . 我想在行 Headers 中显示此表单,在相应行中显示复选框选项 . 我还需要有关数据库表实现的帮助 . 任何帮助将不胜感激 . 我是web2py的新手 . 提前致谢 .

1 回答

  • 0

    你都尝试了些什么?听起来你想要一个布尔数据模型:

    (在models / ??? . py中 - 确保它在db.py之后执行并且你已经定义了一个DB)

    db.define_table('table_name', 
       Field('SomeFieldA', 'boolean')
       Field('SomeFieldB', 'boolean')
       Field('SomeFieldC', 'boolean')
       Field('SomeFieldD', 'boolean')
       ... etc ...
    )
    

    你可能需要实现一个自定义表单来获得你想要的布局,因为我无法想到一个开箱即用的方法 .

    阅读custom forms here .

    你需要从以下内容开始:

    <table>
    {{=form.custom.begin}}
      <thead>
        <tr>
        {{ #loop over form field labels... something like:
          for field in form.fields: }}
          <th>{{=field.label}}</th>
        {{ pass }}
        </tr>
      </thead>
    
      <tbody>
        <tr>
        {{for field in form.fields: }}
          <td>{{  =form.custom.widget[field]  }}</td>
          {{ # or directly access it without a loop with form.custom.widget.SomeFieldA }}
        {{ pass }}
        </tr>
      </tbody>      
    
      {{=form.custom.submit}}
    {{=form.custom.end}} 
    </table>
    

    请注意,我没有测试任何此代码,我可能正在访问表单字段并且标签不正确 . 如果需要预先填充表单,请在控制器中执行此操作 . 上面的自定义表单链接也讨论了预填充 .

相关问题