Example #1
2
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
}
Example #2
1
func (ø *TableForm) Selection(fld string, vals ...interface{}) {
	field := ø.Field(fld)
	Selection(field, vals...)
	label := ø.Label(fld)
	sel := ø.FieldElement(fld)

	if sel.Tag() != "select" {
		innerSelect := SELECT()
		label.SetContent(innerSelect)
		field.Element = label
		field.setFieldInfos()
		sel = innerSelect
	}
	sel.Clear()
	for _, v := range vals {
		r := ""
		typeconverter.Convert(v, &r)
		sel.Add(OPTION(h.Attr("value", r), h.Text(r)))
	}
}