Skip to content

Commit 6208741

Browse files
author
cg33
committed
Feature: table inline edit suppport
1 parent f4568ae commit 6208741

File tree

6 files changed

+51
-46
lines changed

6 files changed

+51
-46
lines changed

examples/datamodel/content.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func GetContent() (types.Panel, error) {
2626
SetText("CPU TRAFFIC").
2727
SetColor("#3583af").
2828
SetNumber("100").
29-
SetIcon("fa-tint").
29+
SetIcon(`<svg t="1568904058859" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2216" width="48" height="48"><path d="M864 64l-704 0C142.336 64 128 78.336 128 96l0 832C128 945.664 142.336 960 160 960l704 0c17.664 0 32-14.336 32-32l0-832C896 78.336 881.664 64 864 64zM832 896 192 896 192 128l640 0L832 896z" fill="#e6e6e6" p-id="2217"></path><path d="M353.92 320c17.6 0 32-14.336 32-32S371.584 256 353.92 256L353.28 256C335.616 256 321.6 270.336 321.6 288S336.256 320 353.92 320z" fill="#e6e6e6" p-id="2218"></path><path d="M353.92 512c17.6 0 32-14.336 32-32S371.584 448 353.92 448L353.28 448C335.616 448 321.6 462.336 321.6 480S336.256 512 353.92 512z" fill="#e6e6e6" p-id="2219"></path><path d="M353.92 704c17.6 0 32-14.336 32-32S371.584 640 353.92 640L353.28 640c-17.6 0-31.616 14.336-31.616 32S336.256 704 353.92 704z" fill="#e6e6e6" p-id="2220"></path><path d="M480 320l192 0C689.664 320 704 305.664 704 288S689.664 256 672 256l-192 0C462.336 256 448 270.336 448 288S462.336 320 480 320z" fill="#e6e6e6" p-id="2221"></path><path d="M480 512l192 0C689.664 512 704 497.664 704 480S689.664 448 672 448l-192 0C462.336 448 448 462.336 448 480S462.336 512 480 512z" fill="#e6e6e6" p-id="2222"></path><path d="M480 704l192 0c17.664 0 32-14.336 32-32S689.664 640 672 640l-192 0C462.336 640 448 654.336 448 672S462.336 704 480 704z" fill="#e6e6e6" p-id="2223"></path></svg>`).
3030
GetContent()
3131

3232
infobox2 := infobox.New().

examples/datamodel/posts.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/GoAdminGroup/go-admin/modules/db"
55
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
66
"github.com/GoAdminGroup/go-admin/template/types/form"
7+
editType "github.com/GoAdminGroup/go-admin/template/types/table"
78
)
89

910
func GetPostsTable() (postsTable table.Table) {
@@ -14,7 +15,7 @@ func GetPostsTable() (postsTable table.Table) {
1415
info.AddField("ID", "id", db.Int).FieldSortable()
1516
info.AddField("Title", "title", db.Varchar)
1617
info.AddField("Description", "description", db.Varchar)
17-
info.AddField("Content", "content", db.Varchar)
18+
info.AddField("Content", "content", db.Varchar).FieldEditAble(editType.Textarea)
1819
info.AddField("Date", "date", db.Varchar)
1920

2021
info.SetTable("posts").SetTitle("Posts").SetDescription("Posts")

examples/datamodel/user.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/GoAdminGroup/go-admin/template"
99
"github.com/GoAdminGroup/go-admin/template/types"
1010
"github.com/GoAdminGroup/go-admin/template/types/form"
11-
table2 "github.com/GoAdminGroup/go-admin/template/types/table"
11+
editType "github.com/GoAdminGroup/go-admin/template/types/table"
1212
)
1313

1414
func GetUserTable() (userTable table.Table) {
@@ -28,7 +28,7 @@ func GetUserTable() (userTable table.Table) {
2828

2929
info := userTable.GetInfo()
3030
info.AddField("ID", "id", db.Int).FieldSortable()
31-
info.AddField("Name", "name", db.Varchar).FieldEditAble()
31+
info.AddField("Name", "name", db.Varchar).FieldEditAble(editType.Text)
3232
info.AddField("Gender", "gender", db.Tinyint).FieldDisplay(func(model types.FieldModel) interface{} {
3333
if model.Value == "0" {
3434
return "men"
@@ -37,16 +37,19 @@ func GetUserTable() (userTable table.Table) {
3737
return "women"
3838
}
3939
return "unknown"
40+
}).FieldEditAble(editType.Select).FieldEditOptions([]map[string]string{
41+
{"value": "0", "text": "men"},
42+
{"value": "1", "text": "women"},
4043
})
41-
info.AddField("Phone", "phone", db.Varchar)
42-
info.AddField("City", "city", db.Varchar)
44+
info.AddField("Phone", "phone", db.Varchar).FieldEditAble()
45+
info.AddField("City", "city", db.Varchar).FieldEditAble()
4346
info.AddField("Avatar", "avatar", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} {
4447
return template.Default().Image().
4548
SetSrc(`//quick.go-admin.cn/demo/assets/dist/img/gopher_avatar.png`).
4649
SetHeight("120").SetWidth("120").GetContent()
4750
})
48-
info.AddField("createdAt", "created_at", db.Timestamp).FieldEditAble(table2.Datetime)
49-
info.AddField("updatedAt", "updated_at", db.Timestamp).FieldEditAble(table2.Datetime)
51+
info.AddField("createdAt", "created_at", db.Timestamp).FieldEditAble(editType.Datetime)
52+
info.AddField("updatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime)
5053

5154
info.SetTable("users").SetTitle("Users").SetDescription("Users")
5255

plugins/admin/modules/table/table.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,14 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet
338338
editable = modules.AorB(tb.info.FieldList[i].EditAble, "true", "false")
339339
hide = modules.AorB(modules.InArrayWithoutEmpty(params.Columns, headField), "0", "1")
340340
thead = append(thead, map[string]string{
341-
"head": tb.info.FieldList[i].Head,
342-
"sortable": sortable,
343-
"field": headField,
344-
"hide": hide,
345-
"editable": editable,
346-
"edittype": tb.info.FieldList[i].EditType.String(),
347-
"width": strconv.Itoa(tb.info.FieldList[i].Width),
341+
"head": tb.info.FieldList[i].Head,
342+
"sortable": sortable,
343+
"field": headField,
344+
"hide": hide,
345+
"editable": editable,
346+
"edittype": tb.info.FieldList[i].EditType.String(),
347+
"editoption": tb.info.FieldList[i].GetEditOptions(),
348+
"width": strconv.Itoa(tb.info.FieldList[i].Width),
348349
})
349350
}
350351

@@ -511,13 +512,14 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
511512
editable = modules.AorB(tb.info.FieldList[i].EditAble, "true", "false")
512513
hide = modules.AorB(modules.InArrayWithoutEmpty(params.Columns, headField), "0", "1")
513514
thead = append(thead, map[string]string{
514-
"head": tb.info.FieldList[i].Head,
515-
"sortable": sortable,
516-
"field": headField,
517-
"hide": hide,
518-
"editable": editable,
519-
"edittype": tb.info.FieldList[i].EditType.String(),
520-
"width": strconv.Itoa(tb.info.FieldList[i].Width),
515+
"head": tb.info.FieldList[i].Head,
516+
"sortable": sortable,
517+
"field": headField,
518+
"hide": hide,
519+
"editable": editable,
520+
"edittype": tb.info.FieldList[i].EditType.String(),
521+
"editoption": tb.info.FieldList[i].GetEditOptions(),
522+
"width": strconv.Itoa(tb.info.FieldList[i].Width),
521523
})
522524
}
523525

template/types/table/table.go

+9-24
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ const (
88
Select
99
Date
1010
Datetime
11-
Dateui
12-
Combodate
13-
Html5types
14-
Checklist
15-
Wysihtml5
16-
Typeahead
17-
Typeaheadjs
18-
Select2
11+
Year
12+
Month
13+
Day
1914
)
2015

2116
func (t Type) String() string {
@@ -28,24 +23,14 @@ func (t Type) String() string {
2823
return "textarea"
2924
case Date:
3025
return "date"
26+
case Year:
27+
return "year"
28+
case Month:
29+
return "month"
30+
case Day:
31+
return "day"
3132
case Datetime:
3233
return "datetime"
33-
case Dateui:
34-
return "dateui"
35-
case Combodate:
36-
return "combodate"
37-
case Html5types:
38-
return "html5types"
39-
case Checklist:
40-
return "checklist"
41-
case Wysihtml5:
42-
return "wysihtml5"
43-
case Typeahead:
44-
return "typeahead"
45-
case Typeaheadjs:
46-
return "typeaheadjs"
47-
case Select2:
48-
return "select2"
4934
default:
5035
panic("wrong form type")
5136
}

template/types/types.go

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package types
66

77
import (
8+
"encoding/json"
89
"github.com/GoAdminGroup/go-admin/modules/config"
910
"github.com/GoAdminGroup/go-admin/modules/db"
1011
"github.com/GoAdminGroup/go-admin/modules/menu"
@@ -332,6 +333,19 @@ type Field struct {
332333
FieldDisplay
333334
}
334335

336+
func (f Field) GetEditOptions() string {
337+
if len(f.EditOptions) == 0 {
338+
return ""
339+
}
340+
eo, err := json.Marshal(f.EditOptions)
341+
342+
if err != nil {
343+
return ""
344+
}
345+
346+
return string(eo)
347+
}
348+
335349
type Join struct {
336350
Table string
337351
Field string

0 commit comments

Comments
 (0)