Browse Source

jsonform init

v1
段少珍 3 years ago
parent
commit
d0aa9df245
  1. 79
      controllers/DataController.go
  2. 28
      models/MenuModel.go
  3. 8
      routers/router.go
  4. 2
      views/common/layout.html
  5. 22
      views/common/layout_jfedit.html
  6. 4
      views/data/add.html
  7. 60
      views/data/footerjs.html
  8. 22
      views/data/footerjs_add.html

79
controllers/DataController.go

@ -1,24 +1,91 @@
package controllers
import (
"fmt"
"strconv"
//"github.com/astaxie/beego/orm"
//"github.com/bitly/go-simplejson"
"github.com/ziyoubiancheng/xcms/consts"
"github.com/ziyoubiancheng/xcms/models"
)
type DataController struct {
BaseController
Mid int
}
func (c *DataController) Index() {
func (c *DataController) Prepare() {
c.BaseController.Prepare()
midstr := c.Ctx.Input.Param(":mid")
mid, err := strconv.Atoi(midstr)
if nil == err && mid > 0 {
models.MenuStruct(mid)
c.setTpl()
} else {
c.Data["Mid"] = midstr
if nil != err || mid <= 0 {
//TODO error page
c.setTpl()
}
c.Mid = mid
}
func (c *DataController) Index() {
sj := models.MenuFormatStruct(c.Mid)
//fmt.Println(sj.Get("schema"))
if sj != nil {
title := make(map[string]string)
titlemap := sj.Get("schema")
for k, _ := range titlemap.MustMap() {
stype := titlemap.GetPath(k, "type").MustString()
fmt.Println(k)
fmt.Println(stype)
if "object" != stype && "array" != stype {
title[k] = titlemap.GetPath(k, "title").MustString()
}
}
fmt.Println(title)
c.Data["Title"] = title
}
c.LayoutSections = make(map[string]string)
c.LayoutSections["footerjs"] = "data/footerjs.html"
c.setTpl()
}
func (c *DataController) List() {
c.listJsonResult(consts.JRCodeFailed, "nil", 0, nil)
}
func (c *DataController) Add() {
format := models.MenuFormatStruct(c.Mid)
c.Data["Schema"] = format.Get("schema").MustMap()
c.Data["Form"] = format.Get("form").MustArray()
c.LayoutSections = make(map[string]string)
c.LayoutSections["footerjs"] = "data/footerjs_add.html"
c.setTpl("data/add.html", "common/layout_jfedit.html")
}
func (c *DataController) AddDo() {
fmt.Println(c.Mid)
fmt.Println("+++++++++")
//fmt.Println(c.Ctx.Request)
var m interface{}
if err := c.ParseForm(&m); err == nil {
fmt.Println(m)
}
fmt.Println("---------")
c.listJsonResult(consts.JRCodeFailed, "nil", 0, nil)
}
func (c *DataController) Edit() {
}
func (c *DataController) EditDo() {
}
func (c *DataController) DeleteDo() {
}

28
models/MenuModel.go

@ -2,10 +2,10 @@ package models
import (
"encoding/json"
"fmt"
"sort"
"github.com/astaxie/beego/orm"
"github.com/bitly/go-simplejson"
)
type MenuModel struct {
@ -76,20 +76,20 @@ func ParentMenuList() []*MenuModel {
return data
}
func MenuStruct(mid int) {
func MenuFormatStruct(mid int) *simplejson.Json {
o := orm.NewOrm()
menu := MenuModel{Mid: mid}
o.Read(&menu)
fmt.Println(menu)
var jsonmap map[string]interface{}
err := json.Unmarshal([]byte(menu.Format), &jsonmap)
if err != nil {
fmt.Println(err)
}
//fmt.Println(jsonmap)
for k, v := range jsonmap {
fmt.Print(k + ":")
fmt.Println(v)
err := o.Read(&menu)
//fmt.Println(menu)
if nil == err {
js, _ := simplejson.NewJson([]byte(menu.Format))
return js
}
return nil
}
//func json2map(jsonstr string) {
// // aws, _ := js.Get("schema").Get("friends").Get("type").String()
// // fmt.Println(aws)
//}

8
routers/router.go

@ -36,5 +36,11 @@ func init() {
beego.Router("/login", &controllers.LoginController{}, "*:Index")
//list
beego.Router("/list/?:mid", &controllers.DataController{}, "Get:Index")
beego.Router("/data/?:mid", &controllers.DataController{}, "Get:Index")
beego.Router("/data/list/?:mid", &controllers.DataController{}, "*:List")
beego.Router("/data/add/?:mid", &controllers.DataController{}, "Get:Add")
beego.Router("/data/adddo/?:mid", &controllers.DataController{}, "*:AddDo")
beego.Router("/data/edit/?:mid", &controllers.DataController{}, "Get:Edit")
beego.Router("/data/editdo/?:mid", &controllers.DataController{}, "*:EditDo")
beego.Router("/data/deletedo/?:mid", &controllers.DataController{}, "*:DeleteDo")
}

2
views/common/layout.html

@ -40,7 +40,7 @@
{{if .Child}}
<dl class="layui-nav-child">
{{range .Child}}
<dd><a href="/list/{{.Mid}}">{{.Name}}</a></dd>
<dd><a href="/data/{{.Mid}}">{{.Name}}</a></dd>
{{end}}
</dl>
{{end}}

22
views/common/layout_jfedit.html

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{{.Title}}</title>
<link rel="stylesheet" type="text/css" href="/static/jsonform/deps/opt/bootstrap.css" />
<style>
</style>
</head>
<body>
{{.LayoutContent}}
<script type="text/javascript" src="/static/jsonform/deps/jquery.min.js"></script>
<script type="text/javascript" src="/static/jsonform/deps/underscore.js"></script>
<script type="text/javascript" src="/static/jsonform/deps/opt/jsv.js"></script>
<script type="text/javascript" src="/static/jsonform/lib/jsonform.js"></script>
{{.footerjs}}
</body>
</html>

4
views/data/add.html

@ -0,0 +1,4 @@
<form action='/data/adddo/{{.Mid}}'>
</form>
<div id="res" class="alert"></div>

60
views/data/footerjs.html

@ -0,0 +1,60 @@
<script>
layui.use('table', function(){
var table = layui.table;
table.render({
elem: '#test'
,url:'data/list/'
,toolbar: '#toolbarDemo'
,cellMinWidth: 60 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
,cols: [[
{field:'Did', title: 'ID', sort: true}
,{field:'Name', title: 'Title'}
{{range $k,$v := .Title}}
,{field:'{{$k}}',title:'{{$v}}'}
{{end}}
,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]]
,page:true
});
//头工具栏事件
table.on('toolbar(test)', function(obj){
switch(obj.event){
case 'add':
layer.open({
type:2,
area: ['800px','500px'],
offset: 'auto',
title:'添加',
content:"/data/add/{{.Mid}}"
});
break;
};
});
//监听行工具事件
table.on('tool(test)', function(obj){
var data = obj.data;
//console.log(obj)
if(obj.event === 'del'){
layer.confirm('真的删除行么', function(index){
layui.jquery.get("/data/deletedo/{{.MID}}?userid="+data.UserId);
obj.del();
layer.close(index);
});
} else if(obj.event === 'edit'){
layer.open({
type:2,
area: ['800px','500px'],
offset: 'auto',
title:'编辑',
content:"/data/edit/{{.MID}}?userid="+data.UserId
});
}
});
});
</script>

22
views/data/footerjs_add.html

@ -0,0 +1,22 @@
<script type="text/javascript">
var createForm = {};
createForm.schema={{.Schema}}
createForm.form={{.Form}}
createForm.onSubmitValid = function (values) {
if (console && console.log) {
console.log('Values extracted from submitted form', values);
}
window.alert('OK Form submitted. Values object:\n' + JSON.stringify(values, null, 2));
};
createForm.onSubmit = function (errors, values) {
if (errors) {
console.log('Validation errors', errors);
return false;
}
return true;
};
$('form').jsonForm(createForm);
</script>
Loading…
Cancel
Save