فهرست منبع

Merge pull request #168 from qor5/presets-append-fields-to-layout

presets: append fields to layout
xuxinx 1 سال پیش
والد
کامیت
6e2a60affb
1فایلهای تغییر یافته به همراه33 افزوده شده و 14 حذف شده
  1. 33 14
      presets/field.go

+ 33 - 14
presets/field.go

@@ -511,33 +511,40 @@ func (b *FieldsBuilder) getField(name string) (r *FieldBuilder) {
 	return
 }
 
-func (b *FieldsBuilder) Only(vs ...interface{}) (r *FieldsBuilder) {
-	if len(vs) == 0 {
-		return b
-	}
-
-	r = b.Clone()
-
-	r.fieldsLayout = vs
-	for _, iv := range vs {
+func (b *FieldsBuilder) getFieldNamesFromLayout() []string {
+	var ns []string
+	for _, iv := range b.fieldsLayout {
 		switch t := iv.(type) {
 		case string:
-			r.appendFieldAfterClone(b, t)
+			ns = append(ns, t)
 		case []string:
 			for _, n := range t {
-				r.appendFieldAfterClone(b, n)
+				ns = append(ns, n)
 			}
 		case *FieldsSection:
 			for _, row := range t.Rows {
 				for _, n := range row {
-					r.appendFieldAfterClone(b, n)
+					ns = append(ns, n)
 				}
 			}
 		default:
 			panic("unknown fields layout, must be string/[]string/*FieldsSection")
 		}
 	}
+	return ns
+}
 
+func (b *FieldsBuilder) Only(vs ...interface{}) (r *FieldsBuilder) {
+	if len(vs) == 0 {
+		return b
+	}
+
+	r = b.Clone()
+
+	r.fieldsLayout = vs
+	for _, fn := range r.getFieldNamesFromLayout() {
+		r.appendFieldAfterClone(b, fn)
+	}
 	return
 }
 
@@ -596,12 +603,24 @@ func (b *FieldsBuilder) toComponentWithFormValueKey(info *ModelInfo, obj interfa
 		edit = true
 	}
 
-	layout := b.fieldsLayout
-	if layout == nil {
+	var layout []interface{}
+	if b.fieldsLayout == nil {
 		layout = make([]interface{}, 0, len(b.fields))
 		for _, f := range b.fields {
 			layout = append(layout, f.name)
 		}
+	} else {
+		layout = b.fieldsLayout[:]
+		layoutFM := make(map[string]struct{})
+		for _, fn := range b.getFieldNamesFromLayout() {
+			layoutFM[fn] = struct{}{}
+		}
+		for _, f := range b.fields {
+			if _, ok := layoutFM[f.name]; ok {
+				continue
+			}
+			layout = append(layout, f.name)
+		}
 	}
 	for _, iv := range layout {
 		var comp h.HTMLComponent