Skip to content

Commit

Permalink
fix(admin): fixed button init error
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Mar 16, 2020
1 parent e3b5ff7 commit 56cc8a8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 43 deletions.
8 changes: 4 additions & 4 deletions examples/datamodel/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func GetUserTable(ctx *context.Context) (userTable table.Table) {
{Value: "0", Text: "men"},
{Value: "1", Text: "women"},
})
info.AddColumn("个性", func(value types.FieldModel) interface{} {
return "帅气"
info.AddColumn("personality", func(value types.FieldModel) interface{} {
return "handsome"
})
info.AddColumnButtons("查看更多", types.GetDefaultButton("see more", icon.Info,
info.AddColumnButtons("see more", types.GetDefaultButton("see more", icon.Info,
action.PopUp("/see/more/example", "see more", func(ctx *context.Context) (success bool, msg string, data interface{}) {
return true, "ok", "<h1>详情</h1><p>balabala</p>"
return true, "ok", "<h1>Detail</h1><p>balabala</p>"
})))
info.AddField("Phone", "phone", db.Varchar).FieldFilterable()
info.AddField("City", "city", db.Varchar).FieldFilterable()
Expand Down
1 change: 1 addition & 0 deletions plugins/admin/controller/detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (h *Handler) ShowDetail(ctx *context.Context) {
Field: field.Field,
TypeName: field.TypeName,
Head: field.Head,
Join: field.Join,
FormType: form.Default,
FieldDisplay: field.FieldDisplay,
}
Expand Down
33 changes: 12 additions & 21 deletions plugins/admin/modules/table/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,40 +503,31 @@ func (tb DefaultTable) getDataFromDatabase(params parameter.Parameters) (PanelIn
}, nil
}

func getDataRes(list []map[string]interface{}, i int) map[string]interface{} {
if len(list) > 0 {
return list[0]
}
return nil
}

// GetDataWithId query the single row of data.
func (tb DefaultTable) GetDataWithId(param parameter.Parameters) (FormInfo, error) {

var (
res map[string]interface{}
columns Columns
custom = false
custom = tb.getDataFun != nil || tb.sourceURL != "" || tb.Info.GetDataFn != nil
id = param.PK()
)

if tb.getDataFun != nil {
list, _ := tb.getDataFun(param)
if len(list) > 0 {
res = list[0]
}
custom = true
res = getDataRes(tb.getDataFun(param))
} else if tb.sourceURL != "" {
list, _ := tb.getDataFromURL(param)
if len(list) > 0 {
res = list[0]
}
custom = true
res = getDataRes(tb.getDataFromURL(param))
} else if tb.Detail.GetDataFn != nil {
list, _ := tb.Detail.GetDataFn(param)
if len(list) > 0 {
res = list[0]
}
custom = true
res = getDataRes(tb.Detail.GetDataFn(param))
} else if tb.Info.GetDataFn != nil {
list, _ := tb.Info.GetDataFn(param)
if len(list) > 0 {
res = list[0]
}
custom = true
res = getDataRes(tb.Info.GetDataFn(param))
} else {

columns, _ = tb.getColumns(tb.Form.Table)
Expand Down
46 changes: 44 additions & 2 deletions template/types/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func GetDefaultButton(title template.HTML, icon string, action Action, colors ..

var color, textColor template.HTML
if len(colors) > 0 {
color = colors[1]
color = colors[0]
}
if len(colors) > 1 {
textColor = colors[2]
textColor = colors[1]
}
return &DefaultButton{
BaseButton: &BaseButton{
Expand Down Expand Up @@ -128,3 +128,45 @@ func (b Buttons) Content() (template.HTML, template.JS) {
}
return h, j
}

type NavButton struct {
*BaseButton
Icon string
}

func GetNavButton(title template.HTML, icon string, action Action) *NavButton {

id := btnUUID()
action.SetBtnId(id)

return &NavButton{
BaseButton: &BaseButton{
Id: id,
Title: title,
Action: action,
},
Icon: icon,
}
}

func (n *NavButton) Content() (template.HTML, template.JS) {

icon := template.HTML("")
title := template.HTML("")

if n.Icon != "" {
icon = template.HTML(`<i class="fa ` + n.Icon + `"></i>`)
}

if n.Title != "" {
title = `<span>` + n.Title + `</span>`
}

h := template.HTML(`<li>
<a class="` + template.HTML(n.Id) + `" ` + n.Action.BtnAttribute() + `>
` + icon + `
` + title + `
</a>
</li>`) + n.Action.ExtContent()
return h, n.Action.Js()
}
26 changes: 14 additions & 12 deletions template/types/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ type FormField struct {

Width int

Join Join

HelpMsg template.HTML

OptionExt template.JS
Expand Down Expand Up @@ -669,7 +671,7 @@ func searchJS(ext template.JS, url string, handler Handler, delay ...int) (templ
return template.JS(`{
`) + ext + template.JS(`
ajax: {
    url: "`+url+`",
    url: "` + url + `",
    dataType: 'json',
    data: function (params) {
      var query = {
Expand All @@ -678,17 +680,17 @@ func searchJS(ext template.JS, url string, handler Handler, delay ...int) (templ
      }
      return query;
    },
    delay: `+delayStr+`,
    delay: ` + delayStr + `,
    processResults: function (data, params) {
      return data.data;
    }
  }
}`), context.Node{
Path: url,
Method: "get",
Handlers: context.Handlers{handler.Wrap()},
Value: map[string]interface{}{constant.ContextNodeNeedAuth: 1},
}
Path: url,
Method: "get",
Handlers: context.Handlers{handler.Wrap()},
Value: map[string]interface{}{constant.ContextNodeNeedAuth: 1},
}
}

func chooseCustomJS(field string, js template.HTML) template.HTML {
Expand Down Expand Up @@ -789,11 +791,11 @@ $(".` + template.HTML(field) + `").on("select2:select",function(e){
})
})
</script>`, context.Node{
Path: url,
Method: "post",
Handlers: context.Handlers{handler.Wrap()},
Value: map[string]interface{}{constant.ContextNodeNeedAuth: 1},
}
Path: url,
Method: "post",
Handlers: context.Handlers{handler.Wrap()},
Value: map[string]interface{}{constant.ContextNodeNeedAuth: 1},
}
}

func chooseHideJS(field, value string, chooseFields ...string) template.HTML {
Expand Down
18 changes: 14 additions & 4 deletions template/types/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ type Page struct {

// Components assets
AssetsList template.HTML

// Top Nav Buttons
NavButtons Buttons
}

func NewPage(user models.UserModel, menu menu.Menu, panel Panel, cfg config.Config, assetsList template.HTML) Page {
return Page{
func NewPage(user models.UserModel, menu menu.Menu, panel Panel, cfg config.Config, assetsList template.HTML, buttons ...Button) *Page {
return &Page{
User: user,
Menu: menu,
Panel: panel,
Expand All @@ -86,11 +89,18 @@ func NewPage(user models.UserModel, menu menu.Menu, panel Panel, cfg config.Conf
CustomHeadHtml: cfg.CustomHeadHtml,
CustomFootHtml: cfg.CustomFootHtml,
AssetsList: assetsList,
NavButtons: buttons,
}
}

func NewPagePanel(panel Panel) Page {
return Page{
func (page *Page) AddButton(title template.HTML, icon string, action Action) *Page {
page.NavButtons = append(page.NavButtons, GetNavButton(title, icon, action))
page.CustomFootHtml += action.FooterContent()
return page
}

func NewPagePanel(panel Panel) *Page {
return &Page{
Panel: panel,
System: SystemInfo{
Version: system.Version(),
Expand Down

0 comments on commit 56cc8a8

Please sign in to comment.