Skip to content

Commit 55b4e84

Browse files
author
cg33
committed
support table edit
1 parent c9e185d commit 55b4e84

File tree

14 files changed

+128
-13
lines changed

14 files changed

+128
-13
lines changed

examples/datamodel/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func GetUserTable() (userTable table.Table) {
2727

2828
info := userTable.GetInfo()
2929
info.AddField("ID", "id", db.Int).FieldSortable()
30-
info.AddField("Name", "name", db.Varchar)
30+
info.AddField("Name", "name", db.Varchar).FieldEditAble()
3131
info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} {
3232
if model.Value == "0" {
3333
return "men"

plugins/admin/controller/Update.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package controller
2+
3+
import (
4+
"github.com/GoAdminGroup/go-admin/context"
5+
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/guard"
6+
"net/http"
7+
)
8+
9+
func Update(ctx *context.Context) {
10+
11+
param := guard.GetUpdateParam(ctx)
12+
13+
err := param.Panel.UpdateDataFromDatabase(param.Value)
14+
15+
if err != nil {
16+
ctx.Json(http.StatusInternalServerError, map[string]interface{}{
17+
"msg": "fail",
18+
})
19+
return
20+
}
21+
22+
ctx.Json(http.StatusOK, map[string]interface{}{
23+
"msg": "ok",
24+
})
25+
}

plugins/admin/controller/edit.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ func EditForm(ctx *context.Context) {
100100
exportUrl := modules.AorB(param.Panel.GetExportable(), param.GetExportUrl(), "")
101101
newUrl := modules.AorB(param.Panel.GetCanAdd(), param.GetNewUrl(), "")
102102
infoUrl := param.GetInfoUrl()
103+
updateUrl := modules.AorB(param.Panel.GetEditable(), param.GetUpdateUrl(), "")
103104

104-
buf := showTable(ctx, param.Panel, param.Path, param.Param, exportUrl, newUrl, deleteUrl, infoUrl, editUrl)
105+
buf := showTable(ctx, param.Panel, param.Path, param.Param, exportUrl, newUrl, deleteUrl, infoUrl, editUrl, updateUrl)
105106

106107
ctx.Html(http.StatusOK, buf.String())
107108
ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath)

plugins/admin/controller/new.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ func NewForm(ctx *context.Context) {
9696
exportUrl := modules.AorB(param.Panel.GetExportable(), param.GetExportUrl(), "")
9797
newUrl := modules.AorB(param.Panel.GetCanAdd(), param.GetNewUrl(), "")
9898
infoUrl := param.GetInfoUrl()
99+
updateUrl := modules.AorB(param.Panel.GetEditable(), param.GetUpdateUrl(), "")
99100

100-
buf := showTable(ctx, param.Panel, param.Path, param.Param, exportUrl, newUrl, deleteUrl, infoUrl, editUrl)
101+
buf := showTable(ctx, param.Panel, param.Path, param.Param, exportUrl, newUrl, deleteUrl, infoUrl, editUrl, updateUrl)
101102

102103
ctx.Html(http.StatusOK, buf.String())
103104
ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath)

plugins/admin/controller/show.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ func ShowInfo(ctx *context.Context) {
4141
newUrl := modules.AorB(panel.GetCanAdd() && !panel.GetInfo().IsHideNewButton,
4242
config.Url("/info/"+prefix+"/new"+params.GetRouteParamStr()), "")
4343
infoUrl := config.Url("/info/" + prefix)
44+
updateUrl := config.Url("/update/" + prefix)
4445

45-
buf := showTable(ctx, panel, ctx.Path(), params, exportUrl, newUrl, deleteUrl, infoUrl, editUrl)
46+
buf := showTable(ctx, panel, ctx.Path(), params, exportUrl, newUrl, deleteUrl, infoUrl, editUrl, updateUrl)
4647
ctx.Html(http.StatusOK, buf.String())
4748
}
4849

4950
func showTable(ctx *context.Context, panel table.Table, path string, params parameter.Parameters,
50-
exportUrl, newUrl, deleteUrl, infoUrl, editUrl string) *bytes.Buffer {
51+
exportUrl, newUrl, deleteUrl, infoUrl, editUrl, updateUrl string) *bytes.Buffer {
5152

5253
panelInfo, err := panel.GetDataFromDatabase(path, params)
5354

@@ -97,6 +98,7 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
9798
SetExportUrl(exportUrl).
9899
SetNewUrl(newUrl).
99100
SetEditUrl(editUrl).
101+
SetUpdateUrl(updateUrl).
100102
SetDeleteUrl(deleteUrl).GetContent(),
101103
}
102104
}
@@ -112,6 +114,7 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
112114
SetExportUrl(exportUrl).
113115
SetNewUrl(newUrl).
114116
SetEditUrl(editUrl).
117+
SetUpdateUrl(updateUrl).
115118
SetDeleteUrl(deleteUrl)
116119
body = dataTable.GetContent()
117120
}

plugins/admin/modules/guard/edit.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func ShowForm(ctx *context.Context) {
4040
return
4141
}
4242

43-
id := ctx.Query("id")
43+
id := ctx.Query(panel.GetPrimaryKey().Name)
4444
if id == "" {
45-
alert(ctx, panel, "wrong id")
45+
alert(ctx, panel, "wrong "+panel.GetPrimaryKey().Name)
4646
ctx.Abort()
4747
return
4848
}
@@ -81,6 +81,10 @@ func (e EditFormParam) GetEditUrl() string {
8181
return e.getUrl("edit")
8282
}
8383

84+
func (e EditFormParam) GetUpdateUrl() string {
85+
return config.Get().Url("/update/" + e.Prefix)
86+
}
87+
8488
func (e EditFormParam) HasAlert() bool {
8589
return e.Alert != template2.HTML("")
8690
}
@@ -149,7 +153,7 @@ func EditForm(ctx *context.Context) {
149153
}
150154
}
151155

152-
param := parameter.GetParamFromUrl(previous)
156+
param := parameter.GetParamFromUrl(previous, panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name, panel.GetInfo().GetSort())
153157

154158
ctx.SetUserValue("edit_form_param", &EditFormParam{
155159
Panel: panel,

plugins/admin/modules/guard/new.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func (e NewFormParam) GetNewUrl() string {
7373
return e.getUrl("new")
7474
}
7575

76+
func (e NewFormParam) GetUpdateUrl() string {
77+
return config.Get().Url("/update/" + e.Prefix)
78+
}
79+
7680
func (e NewFormParam) GetDeleteUrl() string {
7781
return config.Get().Url("/delete/" + e.Prefix)
7882
}
@@ -123,7 +127,7 @@ func NewForm(ctx *context.Context) {
123127
return
124128
}
125129

126-
param := parameter.GetParamFromUrl(previous)
130+
param := parameter.GetParamFromUrl(previous, panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name, panel.GetInfo().GetSort())
127131

128132
ctx.SetUserValue("new_form_param", &NewFormParam{
129133
Panel: panel,

plugins/admin/modules/guard/update.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package guard
2+
3+
import (
4+
"github.com/GoAdminGroup/go-admin/context"
5+
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
6+
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
7+
"net/http"
8+
)
9+
10+
type UpdateParam struct {
11+
Panel table.Table
12+
Prefix string
13+
Value form.Values
14+
}
15+
16+
func Update(ctx *context.Context) {
17+
prefix := ctx.Query("__prefix")
18+
panel := table.List[prefix]
19+
20+
if !panel.GetEditable() {
21+
ctx.Json(http.StatusForbidden, map[string]interface{}{
22+
"msg": "operation not allow",
23+
})
24+
ctx.Abort()
25+
return
26+
}
27+
28+
pname := panel.GetPrimaryKey().Name
29+
30+
id := ctx.FormValue("pk")
31+
32+
if id == "" {
33+
ctx.Json(http.StatusBadRequest, map[string]interface{}{
34+
"msg": "wrong " + pname,
35+
})
36+
ctx.Abort()
37+
return
38+
}
39+
40+
var f = make(form.Values)
41+
f.Add(pname, id)
42+
f.Add(ctx.FormValue("name"), ctx.FormValue("value"))
43+
44+
ctx.SetUserValue("update_param", &UpdateParam{
45+
Panel: panel,
46+
Prefix: prefix,
47+
Value: f,
48+
})
49+
ctx.Next()
50+
}
51+
52+
func GetUpdateParam(ctx *context.Context) *UpdateParam {
53+
return ctx.UserValue["update_param"].(*UpdateParam)
54+
}

plugins/admin/modules/parameter/parameter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ func GetParam(values url.Values, defaultPageSize int, primaryKey, defaultSort st
5959
}
6060
}
6161

62-
func GetParamFromUrl(value string) Parameters {
62+
func GetParamFromUrl(value string, defaultPageSize int, primaryKey, defaultSort string) Parameters {
6363
prevUrlArr := strings.Split(value, "?")
6464
paramArr := strings.Split(prevUrlArr[1], "&")
6565

6666
var (
6767
page = "1"
68-
pageSize = "10"
69-
sortField = "id"
70-
sortType = "desc"
68+
pageSize = strconv.Itoa(defaultPageSize)
69+
sortField = primaryKey
70+
sortType = defaultSort
7171
columns = make([]string, 0)
7272
)
7373

plugins/admin/modules/table/table.go

+6
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet
306306

307307
var (
308308
sortable string
309+
editable string
309310
hide string
310311
joins string
311312
headField string
@@ -334,12 +335,14 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet
334335
continue
335336
}
336337
sortable = modules.AorB(tb.info.FieldList[i].Sortable, "1", "0")
338+
editable = modules.AorB(tb.info.FieldList[i].EditAble, "true", "false")
337339
hide = modules.AorB(modules.InArrayWithoutEmpty(params.Columns, headField), "0", "1")
338340
thead = append(thead, map[string]string{
339341
"head": tb.info.FieldList[i].Head,
340342
"sortable": sortable,
341343
"field": headField,
342344
"hide": hide,
345+
"editable": editable,
343346
"width": strconv.Itoa(tb.info.FieldList[i].Width),
344347
})
345348
}
@@ -475,6 +478,7 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
475478

476479
var (
477480
sortable string
481+
editable string
478482
hide string
479483
joins string
480484
headField string
@@ -503,12 +507,14 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
503507
continue
504508
}
505509
sortable = modules.AorB(tb.info.FieldList[i].Sortable, "1", "0")
510+
editable = modules.AorB(tb.info.FieldList[i].EditAble, "true", "false")
506511
hide = modules.AorB(modules.InArrayWithoutEmpty(params.Columns, headField), "0", "1")
507512
thead = append(thead, map[string]string{
508513
"head": tb.info.FieldList[i].Head,
509514
"sortable": sortable,
510515
"field": headField,
511516
"hide": hide,
517+
"editable": editable,
512518
"width": strconv.Itoa(tb.info.FieldList[i].Width),
513519
})
514520
}

plugins/admin/router.go

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func InitRouter(prefix string) *context.App {
5454
authRoute.POST("/export/:__prefix", guard.Export, controller.Export)
5555
authRoute.GET("/info/:__prefix", controller.ShowInfo)
5656

57+
authRoute.POST("/update/:__prefix", guard.Update, controller.Update)
58+
5759
return app
5860
}
5961

template/components/table.go

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type TableAttribute struct {
1616
EditUrl string
1717
MinWidth int
1818
DeleteUrl string
19+
UpdateUrl string
1920
IsTab bool
2021
ExportUrl string
2122
types.Attribute
@@ -52,6 +53,7 @@ type DataTableAttribute struct {
5253
TableAttribute
5354
EditUrl string
5455
NewUrl string
56+
UpdateUrl string
5557
DeleteUrl string
5658
PrimaryKey string
5759
IsTab bool
@@ -98,6 +100,11 @@ func (compo *DataTableAttribute) SetFilterUrl(value string) types.DataTableAttri
98100
return compo
99101
}
100102

103+
func (compo *DataTableAttribute) SetUpdateUrl(value string) types.DataTableAttribute {
104+
compo.UpdateUrl = value
105+
return compo
106+
}
107+
101108
func (compo *DataTableAttribute) SetPrimaryKey(value string) types.DataTableAttribute {
102109
compo.PrimaryKey = value
103110
return compo

template/types/components.go

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type DataTableAttribute interface {
8282
SetFilterUrl(value string) DataTableAttribute
8383
SetInfoUrl(value string) DataTableAttribute
8484
SetExportUrl(value string) DataTableAttribute
85+
SetUpdateUrl(value string) DataTableAttribute
8586
SetFilters(value []map[string]string) DataTableAttribute
8687
GetContent() template.HTML
8788
}

template/types/types.go

+7
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ type Field struct {
320320

321321
Width int
322322
Sortable bool
323+
EditAble bool
323324
Fixed bool
324325
Filterable bool
325326
Hide bool
@@ -407,6 +408,7 @@ func (i *InfoPanel) AddField(head, field string, typeName db.DatabaseType) *Info
407408
Field: field,
408409
TypeName: typeName,
409410
Sortable: false,
411+
EditAble: false,
410412
FieldDisplay: FieldDisplay{
411413
Display: func(value FieldModel) interface{} {
412414
return value.Value
@@ -436,6 +438,11 @@ func (i *InfoPanel) FieldSortable() *InfoPanel {
436438
return i
437439
}
438440

441+
func (i *InfoPanel) FieldEditAble() *InfoPanel {
442+
i.FieldList[i.curFieldListIndex].EditAble = true
443+
return i
444+
}
445+
439446
func (i *InfoPanel) FieldFixed() *InfoPanel {
440447
i.FieldList[i.curFieldListIndex].Fixed = true
441448
return i

0 commit comments

Comments
 (0)