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

huangapple go评论72阅读模式
英文:

Golang Revel: Paginating a struct array

问题

我有一个结构体数组,我需要在视图端进行分页。

这是我在视图上的代码:

<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>

我已经尝试寻找分页的解决方案,因为结果有几百个。到目前为止,我找到的唯一的Golang解决方案与SQL相关。我非常感谢提供一个适用于结构体数组的解决方案。

提前感谢。

编辑 我的后端存储是BoltDB。我通过调用以下方法在控制器上获取数据:

func List(bucket string)  []Data{
    //打开BoltDB数据库
    Open()
    defer Close()
    //使用预定义的结构体创建一个数组
    d:=make([]Data, 0)
    //将数据解析为字节形式保存
    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 d
}

我希望在视图上迭代这个数组。

英文:

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

This is what my code looks like on the view:

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

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, &amp;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

得分: 2

你可以收集从列表函数返回的完整数据数组。

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()]
}

虽然你可以得到你想要的行为,但如果你的数据数组很大,这样做在内存方面非常浪费。
希望对你有所帮助。

英文:

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 &gt; len(x) {
		return len(x)
	} else {
		return skip + size
	}

}

start := func() int {
	if skip &gt; 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.

huangapple
  • 本文由 发表于 2016年1月6日 14:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/34626654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定