func Selection(Field *Field, vals ...interface{}) *Field { switch Field.Type { case Int: allowed := []int{} for _, v := range vals { var i int typeconverter.Convert(v, &i) allowed = append(allowed, i) } Field.Selection = allowed case Float: allowed := []float32{} for _, v := range vals { iv, ok := v.(float32) if !ok { iv = float32(v.(float64)) } allowed = append(allowed, iv) } Field.Selection = allowed case String: allowed := []string{} for _, v := range vals { allowed = append(allowed, v.(string)) } Field.Selection = allowed } if sel := Field.Element.Any(h.Tag("select")); sel != nil { options := sel.All(h.Tag("option")) for i, opt := range options { var str string typeconverter.Convert(vals[i], &str) opt.Add(h.Attr("value", str)) } } return Field }
// sets all true and false values to the given text func (ø *TableForm) SetBoolTexts(trueText string, falseText string) { for n, f := range ø.Fields { if f.Type == Bool { elem := ø.FieldElement(n) trueOpt := elem.Any(h.And_(h.Attr("value", "true"), h.Tag("option"))) if trueOpt != nil { trueOpt.SetContent(trueText) } falseOpt := elem.Any(h.And_(h.Attr("value", "false"), h.Tag("option"))) if falseOpt != nil { falseOpt.SetContent(falseText) } } } }
func (ø *TableForm) SetValues(row *pgsql.Row) { props := row.AsStrings() for k, v := range props { if !ø.HasFieldDefinition(k) { continue } elem := ø.FieldElement(k) if elem.Tag() == "select" { option := elem.Any(h.And_(h.Attr("value", v), h.Tag("option"))) if option != nil { option.Add(h.Attr("selected", "selected")) } } else { if elem.Tag() == "textarea" { elem.Add(v) } else { //tp := elem.Attribute("type") //if tp == "date" { if elem.HasClass("date") { var tme time.Time field := row.Table.Field(k) row.Get(field, &tme) year, month, day := tme.Date() // %02.0f.%02.0f.%4.0f v = fmt.Sprintf("%4.0f-%02.0f-%02.0f", float64(year), float64(int(month)), float64(day)) } elem.Add(h.Attr("value", v)) } } } }
func (ø *Field) setLabelInfos() { label := ø.Element.Any(h.Tag("label")) if label != nil { label.Add(h.Attr("for", ø.Name)) if ø.Required { label.Add(h.Class("required")) } } }
func Optional(name string, t Typer, html ...interface{}) (ø *Field) { e := h.NewElement(h.Tag("form"), h.WithoutDecoration) e.Add(html...) ø = &Field{Name: name, Type: t.Type(), Element: e, Required: false} if c, ok := t.(Constructor); ok { ø.Constructor = c } ø.setFieldInfos() return }
func (ø *TableForm) Select(fld string) (e *h.Element) { e = ø.Any(h.And_(h.Tag("select"), h.Id(fld))) return }
func (ø *TableForm) Label(fld string) (e *h.Element) { e = ø.Any(h.And_(h.Tag("label"), h.Attr("for", fld))) return }