Skip to content

Commit

Permalink
Add table row action support
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Jan 11, 2020
1 parent 2c7776d commit 8d395eb
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 47 deletions.
3 changes: 3 additions & 0 deletions examples/datamodel/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/types"
"github.com/GoAdminGroup/go-admin/template/types/action"
"github.com/GoAdminGroup/go-admin/template/types/form"
editType "github.com/GoAdminGroup/go-admin/template/types/table"
)
Expand Down Expand Up @@ -57,6 +58,8 @@ func GetUserTable() (userTable table.Table) {
FieldFilterable(types.FilterType{FormType: form.DatetimeRange})
info.AddField("UpdatedAt", "updated_at", db.Timestamp).FieldEditAble(editType.Datetime)

info.AddActionButton("测试", action.Jump("http://baidu.com"))

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

formList := userTable.GetForm()
Expand Down
2 changes: 1 addition & 1 deletion modules/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Random(strings []string) ([]string, error) {
func CompressedContent(h *template.HTML) {
st := strings.Split(string(*h), "\n")
ss := []string{}
for i := 0;i < len(st);i++ {
for i := 0; i < len(st); i++ {
st[i] = strings.TrimSpace(st[i])
if st[i] != "" {
ss = append(ss, st[i])
Expand Down
1 change: 1 addition & 0 deletions plugins/admin/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func detailContent(form types.FormAttribute, editUrl, deleteUrl string) template
func menuFormContent(form types.FormAttribute) template2.HTML {
return aBox().
SetHeader(form.GetBoxHeaderNoButton()).
SetStyle(" ").
WithHeadBorder().
SetBody(form.GetContent()).
GetContent()
Expand Down
67 changes: 50 additions & 17 deletions plugins/admin/controller/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/types"
"github.com/GoAdminGroup/go-admin/template/types/action"
template2 "html/template"
"net/http"
"path"
Expand Down Expand Up @@ -68,13 +69,45 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
}

var (
body template2.HTML
dataTable types.DataTableAttribute
body template2.HTML
dataTable types.DataTableAttribute
info = panel.GetInfo()
actionBtns = info.Action
actionJs template2.JS
)

btns, actionJs := panel.GetInfo().Buttons.Content()
btns, btnsJs := info.Buttons.Content()

if panel.GetInfo().TabGroups.Valid() {
if actionBtns == template.HTML("") && len(info.ActionButtons) > 0 {
ext := template.HTML("")
if deleteUrl != "" {
ext = template.HTML(`<li class="divider"></li>`)
info.AddActionButtonFront(language.GetFromHtml("delete"), types.NewDefaultAction(`data-id='{%v}' style="cursor: pointer;"`,
ext, ""), "grid-row-delete")
}
ext = template.HTML("")
if detailUrl != "" {
if editUrl == "" && deleteUrl == "" {
ext = template.HTML(`<li class="divider"></li>`)
}
info.AddActionButtonFront(language.GetFromHtml("detail"), action.Jump(detailUrl+"&__goadmin_detail_pk={%v}", ext))
}
if editUrl != "" {
if detailUrl == "" && deleteUrl == "" {
ext = template.HTML(`<li class="divider"></li>`)
}
info.AddActionButtonFront(language.GetFromHtml("edit"), action.Jump(editUrl+"&__goadmin_edit_pk={%v}", ext))
}

var content template2.HTML
content, actionJs = info.ActionButtons.Content()

actionBtns = template.HTML(`<div class="dropdown" style="text-align: center;"><a href="#" class="dropdown-toggle"
data-toggle="dropdown" style="color: #676565;"><i class="fa fa-ellipsis-v"></i></a><ul class="dropdown-menu" role="menu"
aria-labelledby="dLabel" style="min-width: 20px !important;left: -32px;overflow: hidden;">`) + content + template.HTML(`</ul></div>`)
}

if info.TabGroups.Valid() {

dataTable = aDataTable().
SetThead(panelInfo.Thead).
Expand All @@ -83,24 +116,24 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
SetExportUrl(exportUrl)

var (
tabsHtml = make([]map[string]template2.HTML, len(panel.GetInfo().TabHeaders))
infoListArr = panelInfo.InfoList.GroupBy(panel.GetInfo().TabGroups)
theadArr = panelInfo.Thead.GroupBy(panel.GetInfo().TabGroups)
tabsHtml = make([]map[string]template2.HTML, len(info.TabHeaders))
infoListArr = panelInfo.InfoList.GroupBy(info.TabGroups)
theadArr = panelInfo.Thead.GroupBy(info.TabGroups)
)
for key, header := range panel.GetInfo().TabHeaders {
for key, header := range info.TabHeaders {
tabsHtml[key] = map[string]template2.HTML{
"title": template2.HTML(header),
"content": aDataTable().
SetInfoList(infoListArr[key]).
SetInfoUrl(infoUrl).
SetButtons(btns).
SetActionJs(actionJs).
SetActionJs(btnsJs + actionJs).
SetHasFilter(len(panelInfo.FormData) > 0).
SetAction(panel.GetInfo().Action).
SetAction(actionBtns).
SetIsTab(key != 0).
SetPrimaryKey(panel.GetPrimaryKey().Name).
SetThead(theadArr[key]).
SetHideRowSelector(panel.GetInfo().IsHideRowSelector).
SetHideRowSelector(info.IsHideRowSelector).
SetExportUrl(exportUrl).
SetNewUrl(newUrl).
SetEditUrl(editUrl).
Expand All @@ -116,14 +149,14 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
SetInfoList(panelInfo.InfoList).
SetInfoUrl(infoUrl).
SetButtons(btns).
SetActionJs(actionJs).
SetAction(panel.GetInfo().Action).
SetActionJs(btnsJs + actionJs).
SetAction(actionBtns).
SetHasFilter(len(panelInfo.FormData) > 0).
SetPrimaryKey(panel.GetPrimaryKey().Name).
SetThead(panelInfo.Thead).
SetExportUrl(exportUrl).
SetHideRowSelector(panel.GetInfo().IsHideRowSelector).
SetHideFilterArea(panel.GetInfo().IsHideFilterArea).
SetHideRowSelector(info.IsHideRowSelector).
SetHideFilterArea(info.IsHideFilterArea).
SetNewUrl(newUrl).
SetEditUrl(editUrl).
SetUpdateUrl(updateUrl).
Expand All @@ -135,7 +168,7 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
boxModel := aBox().
SetBody(body).
SetNoPadding().
SetHeader(dataTable.GetDataTableHeader() + panel.GetInfo().HeaderHtml).
SetHeader(dataTable.GetDataTableHeader() + info.HeaderHtml).
WithHeadBorder().
SetFooter(panelInfo.Paginator.GetContent())

Expand All @@ -145,7 +178,7 @@ func showTable(ctx *context.Context, panel table.Table, path string, params para
SetContent(panelInfo.FormData).
SetPrefix(config.PrefixFixSlash()).
SetMethod("get").
SetLayout(panel.GetInfo().FilterFormLayout).
SetLayout(info.FilterFormLayout).
SetUrl(infoUrl).
SetOperationFooter(filterFormFooter(infoUrl)).
GetContent())
Expand Down
11 changes: 10 additions & 1 deletion template/components/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (compo *BoxAttribute) SetBody(value template.HTML) types.BoxAttribute {
return compo
}

func (compo *BoxAttribute) SetStyle(value template.HTMLAttr) types.BoxAttribute {
compo.Style = value
return compo
}

func (compo *BoxAttribute) SetFooter(value template.HTML) types.BoxAttribute {
compo.Footer = value
return compo
Expand Down Expand Up @@ -86,7 +91,11 @@ func (compo *BoxAttribute) WithSecondHeadBorder() types.BoxAttribute {

func (compo *BoxAttribute) GetContent() template.HTML {

compo.Style = template.HTMLAttr(fmt.Sprintf(`style="overflow: scroll;%s"`, compo.Padding))
if compo.Style == "" {
compo.Style = template.HTMLAttr(fmt.Sprintf(`style="overflow: scroll;%s"`, compo.Padding))
} else {
compo.Style = template.HTMLAttr(fmt.Sprintf(`style="%s"`, string(compo.Style)+compo.Padding))
}

return ComposeHtml(compo.TemplateList, *compo, "box")
}
3 changes: 3 additions & 0 deletions template/components/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func ComposeHtml(temList map[string]string, compo interface{}, templateName ...s
"isLinkUrl": func(s string) bool {
return (len(s) > 7 && s[:7] == "http://") || (len(s) > 8 && s[:8] == "https://")
},
"render": func(s, repl template.HTML) template.HTML {
return template.HTML(strings.Replace(string(s), "{%v}", string(repl), -1))
},
}).Parse(text)
if err != nil {
panic("ComposeHtml Error:" + err.Error())
Expand Down
15 changes: 6 additions & 9 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,13 @@ func DefaultFuncMap() template.FuncMap {
"isLinkUrl": func(s string) bool {
return (len(s) > 7 && s[:7] == "http://") || (len(s) > 8 && s[:8] == "https://")
},
"render": func(s, repl template.HTML) template.HTML {
return template.HTML(strings.Replace(string(s), "{%v}", string(repl), -1))
},
}
}

type BaseComponent struct {
}
type BaseComponent struct{}

func (b BaseComponent) GetAssetList() []string {
return make([]string, 0)
}

func (b BaseComponent) GetAsset(name string) ([]byte, error) {
return nil, nil
}
func (b BaseComponent) GetAssetList() []string { return make([]string, 0) }
func (b BaseComponent) GetAsset(name string) ([]byte, error) { return nil, nil }
23 changes: 14 additions & 9 deletions template/types/action/jump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ import (
type JumpAction struct {
BtnId string
Url string
Ext template.HTML
JS template.JS
}

func Jump(url string) *JumpAction {
func Jump(url string, ext ...template.HTML) *JumpAction {
if len(ext) > 0 {
return &JumpAction{Url: url, Ext: ext[0]}
}
return &JumpAction{Url: url}
}

func (pop *JumpAction) SetBtnId(btnId string) {
pop.BtnId = btnId
func (jump *JumpAction) SetBtnId(btnId string) {
jump.BtnId = btnId
}

func (pop *JumpAction) Js() template.JS {
return template.JS(``)
func (jump *JumpAction) Js() template.JS {
return jump.JS
}

func (pop *JumpAction) BtnAttribute() template.HTML {
return template.HTML(`href="` + pop.Url + `"`)
func (jump *JumpAction) BtnAttribute() template.HTML {
return template.HTML(`href="` + jump.Url + `"`)
}

func (pop *JumpAction) ExtContent() template.HTML {
return ``
func (jump *JumpAction) ExtContent() template.HTML {
return jump.Ext
}
1 change: 1 addition & 0 deletions template/types/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type BoxAttribute interface {
SetFooter(value template.HTML) BoxAttribute
SetTitle(value template.HTML) BoxAttribute
WithHeadBorder() BoxAttribute
SetStyle(value template.HTMLAttr) BoxAttribute
SetHeadColor(value string) BoxAttribute
SetTheme(value string) BoxAttribute
SetSecondHeader(value template.HTML) BoxAttribute
Expand Down
75 changes: 65 additions & 10 deletions template/types/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,10 @@ type InfoPanel struct {

processChains DisplayProcessFnChains

Action template.HTML
HeaderHtml template.HTML
FooterHtml template.HTML
ActionButtons Buttons
Action template.HTML
HeaderHtml template.HTML
FooterHtml template.HTML
}

type Where struct {
Expand All @@ -416,7 +417,26 @@ type Action interface {
SetBtnId(btnId string)
}

type Button struct {
type DefaultAction struct {
Attr template.HTML
JS template.JS
Ext template.HTML
}

func NewDefaultAction(attr, ext template.HTML, js template.JS) *DefaultAction {
return &DefaultAction{Attr: attr, Ext: ext, JS: js}
}

func (def *DefaultAction) SetBtnId(btnId string) {}
func (def *DefaultAction) Js() template.JS { return def.JS }
func (def *DefaultAction) BtnAttribute() template.HTML { return def.Attr }
func (def *DefaultAction) ExtContent() template.HTML { return def.Ext }

type Button interface {
Content() (template.HTML, template.JS)
}

type DefaultButton struct {
Id string
Title template.HTML
Color template.HTML
Expand All @@ -425,7 +445,7 @@ type Button struct {
Icon string
}

func (b Button) Content() (template.HTML, template.JS) {
func (b DefaultButton) Content() (template.HTML, template.JS) {

color := template.HTML("")
if b.Color != template.HTML("") {
Expand All @@ -443,11 +463,22 @@ func (b Button) Content() (template.HTML, template.JS) {
style = template.HTML(`style="`) + addColor + template.HTML(`"`)
}

h := template.HTML(`<div class="btn-group pull-right" style="margin-right: 10px">
h := `<div class="btn-group pull-right" style="margin-right: 10px">
<a id="` + template.HTML(b.Id) + `" ` + style + `class="btn btn-sm btn-default" ` + b.Action.BtnAttribute() + `>
<i class="fa ` + template.HTML(b.Icon) + `"></i>&nbsp;&nbsp;` + b.Title + `
</a>
</div>`) + b.Action.ExtContent()
</div>` + b.Action.ExtContent()
return h, b.Action.Js()
}

type ActionButton struct {
Id string
Title template.HTML
Action Action
}

func (b ActionButton) Content() (template.HTML, template.JS) {
h := template.HTML(`<li><a class="`+template.HTML(b.Id)+`" `+b.Action.BtnAttribute()+`>`+b.Title+`</a></li>`) + b.Action.ExtContent()
return h, b.Action.Js()
}

Expand Down Expand Up @@ -489,17 +520,41 @@ func (i *InfoPanel) AddButton(title template.HTML, icon string, action Action, c
id := "info-btn-" + utils.Uuid(10)
action.SetBtnId(id)
if len(color) == 0 {
i.Buttons = append(i.Buttons, Button{Title: title, Id: id, Action: action, Icon: icon})
i.Buttons = append(i.Buttons, DefaultButton{Title: title, Id: id, Action: action, Icon: icon})
}
if len(color) == 1 {
i.Buttons = append(i.Buttons, Button{Title: title, Color: color[0], Id: id, Action: action, Icon: icon})
i.Buttons = append(i.Buttons, DefaultButton{Title: title, Color: color[0], Id: id, Action: action, Icon: icon})
}
if len(color) >= 2 {
i.Buttons = append(i.Buttons, Button{Title: title, Color: color[0], TextColor: color[1], Id: id, Action: action, Icon: icon})
i.Buttons = append(i.Buttons, DefaultButton{Title: title, Color: color[0], TextColor: color[1], Id: id, Action: action, Icon: icon})
}
return i
}

func (i *InfoPanel) AddActionButton(title template.HTML, action Action, ids ...string) *InfoPanel {
id := ""
if len(ids) > 0 {
id = ids[0]
} else {
id = "action-info-btn-" + utils.Uuid(10)
}
action.SetBtnId(id)
i.ActionButtons = append(i.ActionButtons, ActionButton{Title: title, Id: id, Action: action})
return i
}

func (i *InfoPanel) AddActionButtonFront(title template.HTML, action Action, ids ...string) *InfoPanel {
id := ""
if len(ids) > 0 {
id = ids[0]
} else {
id = "action-info-btn-" + utils.Uuid(10)
}
action.SetBtnId(id)
i.ActionButtons = append([]Button{ActionButton{Title: title, Id: id, Action: action}}, i.ActionButtons...)
return i
}

func (i *InfoPanel) AddLimitFilter(limit int) *InfoPanel {
i.processChains = addLimit(limit, i.processChains)
return i
Expand Down

0 comments on commit 8d395eb

Please sign in to comment.