[add] table widget bind model & form support
This commit is contained in:
parent
d7d723bae7
commit
f5a6f0412f
5 changed files with 87 additions and 19 deletions
|
|
@ -46,7 +46,7 @@ func (dsl *DSL) bindModel() error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = dsl.Layout.BindModel(m, dsl.Fields)
|
||||
err = dsl.Layout.BindModel(m, dsl.Fields, dsl.Action.Bind.Option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ import (
|
|||
// BindModel cast model to fields
|
||||
func (fields *FieldsDSL) BindModel(m *gou.Model) error {
|
||||
|
||||
fields.filterMap = map[string]field.FilterDSL{}
|
||||
fields.tableMap = map[string]field.ColumnDSL{}
|
||||
|
||||
trans, err := field.ModelTransform()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -24,6 +27,7 @@ func (fields *FieldsDSL) BindModel(m *gou.Model) error {
|
|||
// append columns
|
||||
if _, has := fields.Table[tableField.Key]; !has {
|
||||
fields.Table[tableField.Key] = *tableField
|
||||
fields.tableMap[col.Name] = fields.Table[tableField.Key]
|
||||
}
|
||||
|
||||
// Index as filter
|
||||
|
|
@ -34,6 +38,7 @@ func (fields *FieldsDSL) BindModel(m *gou.Model) error {
|
|||
}
|
||||
if _, has := fields.Filter[filterField.Key]; !has {
|
||||
fields.Filter[tableField.Key] = *filterField
|
||||
fields.filterMap[col.Name] = fields.Filter[tableField.Key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,13 @@ import (
|
|||
)
|
||||
|
||||
// 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 == "" {
|
||||
layout.Primary = m.PrimaryKey
|
||||
|
|
@ -17,20 +23,28 @@ func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL) error {
|
|||
|
||||
if layout.Filter == nil && len(fields.Filter) > 0 {
|
||||
|
||||
layout.Filter = &FilterLayoutDSL{
|
||||
BtnAddText: "::Create",
|
||||
Columns: component.Instances{},
|
||||
layout.Filter = &FilterLayoutDSL{Columns: component.Instances{}}
|
||||
if hasForm {
|
||||
layout.Filter.BtnAddText = "创建"
|
||||
}
|
||||
|
||||
max := 3
|
||||
curr := 0
|
||||
for name := range fields.Filter {
|
||||
curr++
|
||||
if curr >= max {
|
||||
break
|
||||
for _, namev := range m.ColumnNames {
|
||||
name, ok := namev.(string)
|
||||
|
||||
if ok {
|
||||
|
||||
if fli, has := fields.filterMap[name]; has {
|
||||
curr++
|
||||
if curr >= max {
|
||||
break
|
||||
}
|
||||
layout.Filter.Columns = append(layout.Filter.Columns, component.InstanceDSL{
|
||||
Name: fli.Key,
|
||||
})
|
||||
}
|
||||
}
|
||||
layout.Filter.Columns = append(layout.Filter.Columns, component.InstanceDSL{
|
||||
Name: name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,10 +57,57 @@ func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL) error {
|
|||
Actions: component.Actions{},
|
||||
},
|
||||
}
|
||||
for name := range fields.Table {
|
||||
layout.Table.Columns = append(layout.Table.Columns, component.InstanceDSL{
|
||||
Name: name,
|
||||
})
|
||||
|
||||
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{
|
||||
Name: col.Key,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ func TestLoad(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, 5, len(Tables))
|
||||
assert.Equal(t, 6, len(Tables))
|
||||
}
|
||||
|
||||
func TestLoadID(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -131,6 +131,8 @@ type OperationTableDSL struct {
|
|||
|
||||
// FieldsDSL the table fields DSL
|
||||
type FieldsDSL struct {
|
||||
Filter field.Filters `json:"filter,omitempty"`
|
||||
Table field.Columns `json:"table,omitempty"`
|
||||
Filter field.Filters `json:"filter,omitempty"`
|
||||
Table field.Columns `json:"table,omitempty"`
|
||||
filterMap map[string]field.FilterDSL
|
||||
tableMap map[string]field.ColumnDSL
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue