Esempio n. 1
0
func OutputHTML(content []byte, ctx *X.Context, args ...int) (err error) {
	code := ctx.Code
	if len(args) > 0 {
		code = args[0]
	}
	return ctx.HTML(code, string(content))
}
Esempio n. 2
0
func OutputXML(content []byte, ctx *X.Context, args ...int) (err error) {
	code := ctx.Code
	if len(args) > 0 {
		code = args[0]
	}
	ctx.Object().XMLBlob(code, content)
	return nil
}
Esempio n. 3
0
func OutputJSON(content []byte, ctx *X.Context, args ...int) (err error) {
	callback := ctx.Query(`callback`)
	code := ctx.Code
	if len(args) > 0 {
		code = args[0]
	}
	if callback != `` {
		content = []byte(callback + "(" + string(content) + ");")
	}
	ctx.Object().JSONBlob(code, content)
	return nil
}
Esempio n. 4
0
func RenderHTML(ctx *X.Context) (b []byte, err error) {
	if ctx.Tmpl == `` {
		return
	}
	ctx.Context.SetFunc(`Status`, func() int {
		return ctx.Output.Status
	})
	ctx.Context.SetFunc(`Message`, func() interface{} {
		return ctx.Output.Message
	})
	b, err = ctx.Object().Fetch(ctx.Tmpl, ctx.Output.Data)
	return
}
Esempio n. 5
0
func Output(content []byte, ctx *X.Context) (err error) {
	ctx.Code = http.StatusOK
	switch ctx.Format {
	case `xml`:
		return OutputXML(content, ctx)
	case `json`:
		return OutputJSON(content, ctx)
	default:
		return OutputHTML(content, ctx)
	}
}
Esempio n. 6
0
func (a *DataTable) Init(c *X.Context, orm *database.Orm, m interface{}) client.Client {
	a.Context = c
	a.Orm = orm
	a.pageSize = com.Int64(c.Form(`length`))
	a.offset = com.Int64(c.Form(`start`))
	if a.pageSize < 1 || a.pageSize > 1000 {
		a.pageSize = 10
	}
	if a.offset < 0 {
		a.offset = 0
	}
	a.fields = make([]string, 0)
	a.tableFields = make([]string, 0)
	a.searches = make(map[string]string)
	a.page = (a.offset + a.pageSize) / a.pageSize
	a.orders = Sorts{}
	a.fieldsInfo = make(map[string]*core.Column)
	a.search = c.Form(`search[value]`)
	a.draw = c.Form(`draw`)
	a.totalRows = com.Int64(a.Context.Form(`totalrows`))
	if a.totalRows < 1 && a.countFn != nil {
		a.totalRows = a.countFn()
	}
	table := orm.TableInfo(m)
	if table == nil {
		return a
	}
	pks := table.PKColumns()
	if len(pks) > 0 {
		for _, col := range pks {
			if col.IsPrimaryKey && col.IsAutoIncrement {
				a.idFieldName = col.Name
				break
			}
		}
	}
	var fm []string = strings.Split(`columns[0][data]`, `0`)
	for k, _ := range c.Request().Form().All() {
		if !strings.HasPrefix(k, fm[0]) || !strings.HasSuffix(k, fm[1]) {
			continue
		}
		idx := strings.TrimSuffix(k, fm[1])
		idx = strings.TrimPrefix(idx, fm[0])

		//要查询的所有字段
		field := c.Form(k)

		column := table.GetColumn(field)
		if column != nil && column.FieldName == field {
			a.fields = append(a.fields, field)
			field = column.Name
			a.tableFields = append(a.tableFields, field)

			//搜索本字段
			kw := c.Form(`columns[` + idx + `][search][value]`)
			if kw != `` {
				a.searches[field] = kw
			}
			a.fieldsInfo[field] = column
		}

		//要排序的字段
		fidx := c.Form(`order[` + idx + `][column]`)
		if fidx == `` {
			continue
		}
		field = c.Form(fm[0] + fidx + fm[1])
		if field == `` {
			continue
		}
		column = table.GetColumn(field)
		if column != nil && column.FieldName == field {
			sort := c.Form(`order[` + idx + `][dir]`)
			if sort != `asc` {
				sort = `desc`
			}
			a.orders.Insert(com.Int(idx), field, column.Name, sort)
		}
	}
	//a.Form(`search[regex]`)=="false"
	//columns[0][search][regex]=false / columns[0][search][value]
	return a
}