dousong2967 2016-01-06 06:04
浏览 279
已采纳

Golang Revel:对结构数组进行分页

I have a struct array that I need to paginate on the view end.

This is what my code looks like on the view:

<div class="tab-content">
        <div class="tab-pane active" id="tab1" >
          <hr/>
          {{range .c}}                
            <p>Number: {{.Number}}</p>
            <p>Name: {{.Name}}</p>
            <p>Parties: {{.A}} and {{.B}}</p>
            <p>Location: {{.Location}}</p>
          <a href="/search">Read More</a>
          <hr/>
          {{end}}
          <div class="paging">
            <ul class="pagination">
              <li><a href="#"><i class="fa fa-angle-left"></i></a></li>
              <li class="active"><a href="#">1</a></li>
              <li><a href="#">2</a></li>
              <li><a href="#">3</a></li>
              <li><a href="#">4</a></li>
              <li><a href="#">5</a></li>
              <li><a href="#"><i class="fa fa-angle-right"></i></a></li>
            </ul>
          </div>
        </div>

I have tried looking for solution to paginate this because the results are in the hundreds. The only golang solutions I have come across so far are SQL related. I would highly appreciate a solution for a struct array.

Thank you in advance.

EDIT My back end storage is BoltDB. I get the data on the controller by calling this method

func List(bucket string)  []Data{
    //Open BoltDB database
    Open()
    defer Close()
    //Use a predefined struct to make an array
    d:=make([]Data, 0)
    //Fetch and unmarshal data as it is saved in byte form
    db.View(func(tx *bolt.Tx) error {
        cur := tx.Bucket([]byte(bucket)).Cursor()
        for k, v := cur.First(); k != nil; k, v = cur.Next() {            
            d1:=Data{}
            err:= json.Unmarshal(v, &d1)
            if err !=nil{
                return err
            }
            d=append(d, d1)
        } 
        return nil  
    })
    //Return the array of data
    return d
}

This array is what I would like to iterate on the view.

  • 写回答

1条回答 默认 最新

  • doty58493 2016-01-06 07:12
    关注

    You could collect the full array of data that you return from the list function.

    func paginate(x []Data, skip int, size int) []int {
    limit := func() int {
        if skip+size > len(x) {
            return len(x)
        } else {
            return skip + size
        }
    
    }
    
    start := func() int {
        if skip > len(x) {
            return len(x)
        } else {
            return skip
        }
    
    }
      return x[start():limit()]
    }
    

    Although you will get the behavior you want, this is very wasteful in terms of memory specially if your data array is huge. Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效