Пример #1
0
func (a *defaultClient) Init(ctx *X.Context, orm *database.Orm, m interface{}) Client {
	a.Context = ctx
	a.Orm = orm
	a.pageRows = com.Int64(a.Context.Form(`pagerows`))
	if a.pageRows < 1 || a.pageRows > 1000 {
		a.pageRows = 10
	}
	a.totalRows = com.Int64(a.Context.Form(`totalrows`))
	a.pageno = com.Int64(a.Context.Form(`page`))
	if a.pageno < 1 {
		a.pageno = 1
	}
	a.offset = (a.pageno - 1) * a.pageRows
	return a
}
Пример #2
0
func (a *Comment) Edit() error {
	id := com.Int64(a.Form(`id`))
	m, has, err := a.cmtM.Get(id)
	if err != nil {
		return err
	} else if !has {
		return a.NotFoundData().Display()
	}
	if a.IsPost() {
		err = a.Bind(m)
		if err != nil {
			return err
		}
		affected, err := a.cmtM.Edit(m.Id, m)
		if err != nil {
			a.SetErr(err.Error())
		} else if affected < 1 {
			a.NotModified()
		} else {
			a.Done()
		}
	}
	a.Assign(`Detail`, m)
	return a.Display()
}
Пример #3
0
func (a *Comment) Delete() error {
	id := com.Int64(a.Form(`id`))
	if id < 1 {
		return a.NotFoundData().Display()
	}
	affected, err := a.cmtM.Delete(id)
	if err != nil {
		return err
	}
	if affected < 1 {
		return a.NotFoundData().Display()
	}
	a.Done()
	return a.Display()
}
Пример #4
0
func Default(defaultV interface{}, v interface{}) interface{} {
	switch v.(type) {
	case nil:
		return defaultV
	case string:
		val, _ := v.(string)
		if val == `` {
			return defaultV
		}
	case uint8, int8, uint, int, uint32, int32, int64, uint64:
		if com.Int64(v) == 0 {
			return defaultV
		}
	case float32, float64:
		if com.Float64(v) == 0.0 {
			return defaultV
		}
	default:
		if com.Str(v) == `` {
			return defaultV
		}
	}
	return v
}
Пример #5
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
}