我尝试使用beego进行电路板分页。
所以我喜欢这样,
var articles []*models.Board
board.Data["startCount"] = startCount
board.Data["endCount"] = endCount
/* Template Test Query
num, err := o.QueryTable("board").Filter("Idx", 21).OrderBy("-idx").All(&articles)
*/
num, err := o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).QueryRows(&articles)
if num == 0 || err == orm.ErrNoRows {
board.Data["user_error"] = err
}
if err != orm.ErrNoRows && num > 0 {
board.Data["nums"] = num
board.Data["articles"] = articles
}
<tbody>
{{ if not .articles }}
<tr>
<td colspan="5">No articles.</td>
</tr>
{{ else }}
{{ $uri := .article_uri}}
{{range $articles := .articles}}
<tr>
<td>{{$articles.Idx}}</td>
<td><a href="{{urlfor "BoardController.Content" ":id" $articles.Idx}}">{{$articles.Title}}</a></td>
<td>{{$articles.Nick}}</td>
<td>{{date $articles.Date "y.m.d H:i"}}</td>
<td>0</td>
</tr>
{{ end }}
{{ end }}
</tbody>
当我使用模板测试查询时,我看到成功打印。
但我只能看到空表,因为要改为o.Raw(...)。。QueryRows(&amp; articles)
Plz有人帮助我..
type Board struct{
Idx int64 `orm:"pk";form:"-"`
Id string `form:"id"`
Title string `form:"title"`
Body string `form:"content"`
Nick string `form:"nick"`
Date time.Time `orm:"auto_now_add;type(datetime)"`
}
答案 0 :(得分:0)
```
o := orm.NewOrm()
var maps []orm.Params
num, err := o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).Values(&maps)
fmt.Println(maps)
if num == 0 || err == orm.ErrNoRows {
board.Data["user_error"] = err
}
if err != orm.ErrNoRows && num > 0 {
board.Data["nums"] = num
board.Data["articles"] = articles
}
```
```
var list orm.ParamsList
num, err = o.Raw(""SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).ValuesFlat(&list)
if err == nil && num > 0 {
fmt.Println(list) // []{"1","2","3",...}
}
```
```
o := orm.NewOrm()
var maps []orm.Params
o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).Values(&maps)
list := make([]map[string]interface{}, len(maps))
for k, v := range maps {
row := make(map[string]interface{})
row["Idx"]=v["workid"]
row["Id"] = v["droneid"]
row["Title"]=v["starttime"]
row["Body"]=v["worktime"]
row["Nick"]=v["modes"]
row["Date"]=v["tele"]
paths := make([]interface{}, 0)
patharray := strings.Split(v["path"].(string),"],[")
for _, vv := range patharray {
path := make([]string, 2)
vv1 := strings.Replace(vv,"[", "", -1)
vv2 := strings.Replace(vv1,"]","",-1)
//fmt.Println(vv2)
vvarr := strings.Split(vv2,",")
// fmt.Println("++++++++++++++")
// fmt.Println(vvarr)
path[0] = vvarr[0]
path[1] = vvarr[1]
paths = append(paths, path)
}
row["path"] = paths
list[k] = row
}
this.Data["json"] = list
this.ServeJSON();
```