[add] table widget bind model & form support

This commit is contained in:
Max 2022-10-07 16:57:31 +08:00
parent d7d723bae7
commit f5a6f0412f
5 changed files with 87 additions and 19 deletions

View file

@ -46,7 +46,7 @@ func (dsl *DSL) bindModel() error {
return err return err
} }
err = dsl.Layout.BindModel(m, dsl.Fields) err = dsl.Layout.BindModel(m, dsl.Fields, dsl.Action.Bind.Option)
if err != nil { if err != nil {
return err return err
} }

View file

@ -10,6 +10,9 @@ import (
// BindModel cast model to fields // BindModel cast model to fields
func (fields *FieldsDSL) BindModel(m *gou.Model) error { func (fields *FieldsDSL) BindModel(m *gou.Model) error {
fields.filterMap = map[string]field.FilterDSL{}
fields.tableMap = map[string]field.ColumnDSL{}
trans, err := field.ModelTransform() trans, err := field.ModelTransform()
if err != nil { if err != nil {
return err return err
@ -24,6 +27,7 @@ func (fields *FieldsDSL) BindModel(m *gou.Model) error {
// append columns // append columns
if _, has := fields.Table[tableField.Key]; !has { if _, has := fields.Table[tableField.Key]; !has {
fields.Table[tableField.Key] = *tableField fields.Table[tableField.Key] = *tableField
fields.tableMap[col.Name] = fields.Table[tableField.Key]
} }
// Index as filter // Index as filter
@ -34,6 +38,7 @@ func (fields *FieldsDSL) BindModel(m *gou.Model) error {
} }
if _, has := fields.Filter[filterField.Key]; !has { if _, has := fields.Filter[filterField.Key]; !has {
fields.Filter[tableField.Key] = *filterField fields.Filter[tableField.Key] = *filterField
fields.filterMap[col.Name] = fields.Filter[tableField.Key]
} }
} }
} }

View file

@ -9,7 +9,13 @@ import (
) )
// BindModel bind model // BindModel bind model
func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL) error { func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL, option map[string]interface{}) error {
if option == nil {
option = map[string]interface{}{}
}
formName, hasForm := option["form"]
if layout.Primary == "" { if layout.Primary == "" {
layout.Primary = m.PrimaryKey layout.Primary = m.PrimaryKey
@ -17,22 +23,30 @@ func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL) error {
if layout.Filter == nil && len(fields.Filter) > 0 { if layout.Filter == nil && len(fields.Filter) > 0 {
layout.Filter = &FilterLayoutDSL{ layout.Filter = &FilterLayoutDSL{Columns: component.Instances{}}
BtnAddText: "::Create", if hasForm {
Columns: component.Instances{}, layout.Filter.BtnAddText = "创建"
} }
max := 3 max := 3
curr := 0 curr := 0
for name := range fields.Filter { for _, namev := range m.ColumnNames {
name, ok := namev.(string)
if ok {
if fli, has := fields.filterMap[name]; has {
curr++ curr++
if curr >= max { if curr >= max {
break break
} }
layout.Filter.Columns = append(layout.Filter.Columns, component.InstanceDSL{ layout.Filter.Columns = append(layout.Filter.Columns, component.InstanceDSL{
Name: name, Name: fli.Key,
}) })
} }
} }
}
}
if layout.Table == nil && len(fields.Table) > 0 { if layout.Table == nil && len(fields.Table) > 0 {
layout.Table = &ViewLayoutDSL{ layout.Table = &ViewLayoutDSL{
@ -43,12 +57,59 @@ func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL) error {
Actions: component.Actions{}, Actions: component.Actions{},
}, },
} }
for name := range fields.Table {
if hasForm {
layout.Table.Operation.Actions = append(
layout.Table.Operation.Actions,
component.ActionDSL{
Title: "查看",
Icon: "icon-eye",
Action: map[string]component.ParamsDSL{
"Common.openModal": {
"width": 640,
"Form": map[string]interface{}{"type": "view", "model": formName},
},
},
},
component.ActionDSL{
Title: "编辑",
Icon: "icon-edit-2",
Action: map[string]component.ParamsDSL{
"Common.openModal": {
"width": 640,
"Form": map[string]interface{}{"type": "edit", "model": formName},
},
},
},
component.ActionDSL{
Title: "删除",
Icon: "icon-trash-2",
Style: "danger",
Action: map[string]component.ParamsDSL{
"Table.delete": {"model": formName},
},
Confirm: &component.ConfirmActionDSL{
Title: "提示",
Desc: "确认删除,删除后数据无法恢复?",
},
},
)
}
for _, namev := range m.ColumnNames {
name, ok := namev.(string)
if ok && name != "deleted_at" {
if col, has := fields.tableMap[name]; has {
layout.Table.Columns = append(layout.Table.Columns, component.InstanceDSL{ layout.Table.Columns = append(layout.Table.Columns, component.InstanceDSL{
Name: name, Name: col.Key,
}) })
} }
} }
}
}
return nil return nil

View file

@ -22,7 +22,7 @@ func TestLoad(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
assert.Equal(t, 5, len(Tables)) assert.Equal(t, 6, len(Tables))
} }
func TestLoadID(t *testing.T) { func TestLoadID(t *testing.T) {

View file

@ -133,4 +133,6 @@ type OperationTableDSL struct {
type FieldsDSL struct { type FieldsDSL struct {
Filter field.Filters `json:"filter,omitempty"` Filter field.Filters `json:"filter,omitempty"`
Table field.Columns `json:"table,omitempty"` Table field.Columns `json:"table,omitempty"`
filterMap map[string]field.FilterDSL
tableMap map[string]field.ColumnDSL
} }