表单组件使用


Default

  1. formList.AddField("name", "name", db.Varchar, form.Default)

Text

  1. formList.AddField("name", "name", db.Varchar, form.Text)

SelectSingle

  1. formList.AddField("sex", "sex", db.Int, form.SelectSingle).
  2. // 单选的选项,field代表显示内容,value代表对应值
  3. FieldOptions([]map[string]string{
  4. {"field": "man","value": "0"},
  5. {"field": "women","value": "1"},
  6. }).
  7. // 这里返回一个[]string,对应的值是本列的sex字段的值,即编辑表单时会显示的对应值
  8. FieldDisplay(func(model types.FieldModel) interface{} {
  9. return []string{"0"}
  10. })

Select

  1. formList.AddField("drink", "drink", db.Int, form.Select).
  2. // 多选的选项,field代表显示内容,value代表对应值
  3. FieldOptions([]map[string]string{
  4. {
  5. "field": "beer",
  6. "value": "beer",
  7. }, {
  8. "field": "juice",
  9. "value": "juice",
  10. }, {
  11. "field": "water",
  12. "value": "water",
  13. }, {
  14. "field": "red bull",
  15. "value": "red bull",
  16. },
  17. }).
  18. // 这里返回一个[]string,对应的值是本列的drink字段的值,即编辑表单时会显示的对应值
  19. FieldDisplay(func(model types.FieldModel) interface{} {
  20. return []string{"beer"}
  21. })

IconPicker

  1. formList.AddField("icon", "icon", db.Varchar, form.IconPicker)

SelectBox

  1. formList.AddField("fruit", "fruit", db.Int, form.SelectBox).
  2. // 多选的选项,field代表显示内容,value代表对应值
  3. FieldOptions([]map[string]string{
  4. {
  5. "field": "apple",
  6. "value": "apple",
  7. }, {
  8. "field": "banana",
  9. "value": "banana",
  10. }, {
  11. "field": "watermelon",
  12. "value": "watermelon",
  13. }, {
  14. "field": "pear",
  15. "value": "pear",
  16. },
  17. }).
  18. // 这里返回一个[]string,对应的值是本列的fruit字段的值,即编辑表单时会显示的对应值
  19. FieldDisplay(func(model types.FieldModel) interface{} {
  20. return []string{"pear"}
  21. })

File

  1. formList.AddField("file", "file", db.Varchar, form.File)

Password

  1. formList.AddField("password", "password", db.Varchar, form.Password)

RichText

  1. formList.AddField("content", "content", db.Varchar, form.RichText)

Datetime

  1. formList.AddField("birthday", "birthday", db.Varchar, form.Datetime)

Radio

  1. formList.AddField("gender", "gender", db.Int, form.SelectBox).
  2. // radio的选项,field代表字,label代表显示内容,value代表对应值
  3. FieldOptions([]map[string]string{
  4. {
  5. "field": "gender",
  6. "label": "male",
  7. "value": "0",
  8. "selected": "checked",
  9. },
  10. {
  11. "field": "gender",
  12. "label": "female",
  13. "value": "1",
  14. "selected": "",
  15. },
  16. }).FieldDefault("0") // 设置默认的值

Email

  1. formList.AddField("email", "email", db.Varchar, form.Email)

Url

  1. formList.AddField("url", "url", db.Varchar, form.Url)

Ip

  1. formList.AddField("ip", "ip", db.Varchar, form.Ip)

Color

  1. formList.AddField("color", "color", db.Varchar, form.Color)

Currency

  1. formList.AddField("money", "money", db.Varchar, form.Currency)

Number

  1. formList.AddField("num", "num", db.Varchar, form.Number)

TextArea

  1. formList.AddField("content", "content", db.Varchar, form.TextArea)

Custom

自定义表单内容

  1. formList.AddField("content", "content", db.Varchar, form.Custom).
  2. FieldCustomContent(template.HTML(``)).
  3. FieldCustomCss(template.CSS(``)).
  4. FieldCustomJs(template.JS(``))

以下表单自定义的模板文件结构,设置的CustomContentCustomCssCustomJs将插入到对应的位置。

  1. {{define "form_custom"}}
  2. {{if eq .Must true}}
  3. <label for="{{.Field}}" class="col-sm-2 asterisk control-label">{{.Head}}</label>
  4. {{else}}
  5. <label for="{{.Field}}" class="col-sm-2 control-label">{{.Head}}</label>
  6. {{end}}
  7. <div class="col-sm-8">
  8. <div class="input-group">
  9. {{.CustomContent}}
  10. </div>
  11. </div>
  12. {{if .CustomJs}}
  13. <script>
  14. {{.CustomJs}}
  15. </script>
  16. {{end}}
  17. {{if .CustomCss}}
  18. <style>
  19. {{.CustomCss}}
  20. </style>
  21. {{end}}
  22. {{end}}