Skip to content

Commit

Permalink
[fix] table widget bug
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 18, 2022
1 parent 4c78f7c commit cd7a776
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 177 deletions.
78 changes: 39 additions & 39 deletions data/bindata.go

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions widgets/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ func (dsl DSL) Map() map[string]interface{} {
"props": map[string]interface{}(dsl.Props),
}
}

// Clone Component
func (dsl *DSL) Clone() *DSL {
new := DSL{
Type: dsl.Type,
In: dsl.In,
Out: dsl.Out,
Props: PropsDSL{},
}
if dsl.Props != nil {
for key, val := range dsl.Props {
new.Props[key] = val
}
}
return &new
}
74 changes: 0 additions & 74 deletions widgets/component/lang.go

This file was deleted.

9 changes: 8 additions & 1 deletion widgets/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,14 @@ func valueOf(name string, data maps.MapStrAny) (interface{}, bool) {

// label / comment
if data.Has(name) {
return data.Get(name), true
value := data.Get(name)
if valstr, ok := value.(string); ok {
// ::value
if strings.HasPrefix(valstr, "::") {
return fmt.Sprintf("$L(%s)", strings.TrimPrefix(valstr, "::")), true
}
}
return value, true
}

return nil, false
Expand Down
40 changes: 12 additions & 28 deletions widgets/field/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,24 @@ func (column ColumnDSL) Replace(data map[string]interface{}) (*ColumnDSL, error)
return &new, nil
}

// Trans trans
func (column *ColumnDSL) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
if column.Edit != nil {
if column.Edit.Trans(widgetName, inst, trans) {
res = true
}
// Clone column
func (column *ColumnDSL) Clone() *ColumnDSL {
new := ColumnDSL{
Key: column.Key,
Bind: column.Bind,
Link: column.Link,
In: column.In,
Out: column.Out,
}

if column.View != nil {
if column.View.Trans(widgetName, inst, trans) {
res = true
}
new.View = column.View.Clone()
}

return res
}

// Trans column trans
func (columns Columns) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false

for key, column := range columns {
if trans(widgetName, inst, &key) {
res = true
}
newPtr := &column
if newPtr.Trans(widgetName, inst, trans) {
res = true
}
columns[key] = *newPtr
if column.Edit != nil {
new.Edit = column.Edit.Clone()
}

return res
return &new
}

// Map cast to map[string]inteface{}
Expand Down
31 changes: 8 additions & 23 deletions widgets/field/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,16 @@ func (filter FilterDSL) Replace(data map[string]interface{}) (*FilterDSL, error)
return &new, nil
}

// Trans trans
func (filter *FilterDSL) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
if filter.Edit != nil {
if filter.Edit.Trans(widgetName, inst, trans) {
res = true
}
// Clone column
func (filter *FilterDSL) Clone() *FilterDSL {
new := FilterDSL{
Key: filter.Key,
Bind: filter.Bind,
}
return res
}

// Trans column trans
func (filters Filters) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
for key, filter := range filters {
if trans(widgetName, inst, &key) {
res = true
}
newPtr := &filter
if newPtr.Trans(widgetName, inst, trans) {
res = true
}
filters[key] = *newPtr
if filter.Edit != nil {
new.Edit = filter.Edit.Clone()
}
return res
return &new
}

// Map cast to map[string]inteface{}
Expand Down
7 changes: 5 additions & 2 deletions widgets/field/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func (t *Transform) column(column *ColumnDSL, data map[string]interface{}) (*Col
data[k] = v
}
}
return column.Replace(data)

new := column.Clone()
return new.Replace(data)
}

// trans transform to filter
Expand All @@ -103,5 +105,6 @@ func (t *Transform) filter(filter *FilterDSL, data map[string]interface{}) (*Fil
data[k] = v
}
}
return filter.Replace(data)
new := filter.Clone()
return new.Replace(data)
}
30 changes: 28 additions & 2 deletions widgets/table/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/yao/i18n"
"github.com/yaoapp/yao/widgets/action"
"github.com/yaoapp/yao/widgets/hook"
)
Expand Down Expand Up @@ -296,13 +297,13 @@ func processHandler(p *action.Process, process *gou.Process) (interface{}, error
// Execute Process
act, err := gou.ProcessOf(name, args...)
if err != nil {
log.Error("[table] %s %s -> %s %s", tab.ID, p.Name, name, err.Error())
log.Error("[table] %s %s -> %s %s %v", tab.ID, p.Name, name, err.Error(), args)
return nil, fmt.Errorf("[table] %s %s -> %s %s", tab.ID, p.Name, name, err.Error())
}

res, err := act.WithGlobal(process.Global).WithSID(process.Sid).Exec()
if err != nil {
log.Error("[table] %s %s -> %s %s", tab.ID, p.Name, name, err.Error())
log.Error("[table] %s %s -> %s %s %v", tab.ID, p.Name, name, err.Error(), args)
return nil, fmt.Errorf("[table] %s %s -> %s %s", tab.ID, p.Name, name, err.Error())
}

Expand Down Expand Up @@ -363,5 +364,30 @@ func processHandler(p *action.Process, process *gou.Process) (interface{}, error
}
}

// Tranlate
if p.Name == "yao.table.Setting" {

res, err = i18n.Trans(process.Lang(), "table", tab.ID, res)
if err != nil {
return nil, fmt.Errorf("[table] Trans.table.%s %s", tab.ID, err.Error())
}

if tab.Action.Bind.Table != "" {
res, err = i18n.Trans(process.Lang(), "table", tab.Action.Bind.Table, res)
if err != nil {
return nil, fmt.Errorf("[table] Trans.bind.table.%s %s", tab.ID, err.Error())
}
}

if tab.Action.Bind.Model != "" {
m := gou.Select(tab.Action.Bind.Model)
res, err = i18n.Trans(process.Lang(), "model", m.ID, res)
if err != nil {
return nil, fmt.Errorf("[table] Trans.model.%s %s", m.ID, err.Error())
}
}

}

return res, nil
}
12 changes: 10 additions & 2 deletions widgets/table/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package table

import (
"fmt"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL, option map[s

if layout.Table == nil && len(fields.Table) > 0 {
layout.Table = &ViewLayoutDSL{
Props: component.PropsDSL{},
Props: component.PropsDSL{"scroll": map[string]interface{}{"x": "max-content"}},
Columns: component.Instances{},
Operation: OperationTableDSL{
Hide: true,
Expand Down Expand Up @@ -106,9 +107,16 @@ func (layout *LayoutDSL) BindModel(m *gou.Model, fields *FieldsDSL, option map[s
name, ok := namev.(string)
if ok && name != "deleted_at" {
if col, has := fields.tableMap[name]; has {
width := 160
if c, has := m.Columns[name]; has {
typ := strings.ToLower(c.Type)
if typ == "id" || strings.Contains(typ, "integer") || strings.Contains(typ, "float") {
width = 100
}
}
layout.Table.Columns = append(layout.Table.Columns, component.InstanceDSL{
Name: col.Key,
Width: 160,
Width: width,
})
}
}
Expand Down
16 changes: 10 additions & 6 deletions yao/fields/model.trans.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@
"table": {
"key": "${label || comment || name}",
"bind": "${name}",
"in": "TagEdit",
"out": "TagView",
"view": { "type": "Tag", "props": { "pure": true } },
"view": { "type": "Text", "props": {} },
"edit": {
"type": "Select",
"props": {
Expand Down Expand Up @@ -233,7 +231,7 @@
"table": {
"key": "${label || comment || name}",
"bind": "${name}",
"view": { "type": "Text", "props": {} },
"view": { "type": "Text", "props": { "format": "YYYY-MM-DD" } },
"edit": {
"type": "DatePicker",
"props": {
Expand Down Expand Up @@ -269,7 +267,10 @@
"table": {
"key": "${label || comment || name}",
"bind": "${name}",
"view": { "type": "Text", "props": {} },
"view": {
"type": "Text",
"props": { "format": "YYYY-MM-DD HH:mm:ss" }
},
"edit": {
"type": "DatePicker",
"props": {
Expand Down Expand Up @@ -305,7 +306,10 @@
"table": {
"key": "${label || comment || name}",
"bind": "${name}",
"view": { "type": "Text", "props": {} },
"view": {
"type": "Text",
"props": { "format": "YYYY-MM-DD HH:mm:ss" }
},
"edit": {
"type": "DatePicker",
"props": {
Expand Down

0 comments on commit cd7a776

Please sign in to comment.