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

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

Golang Revel: Paginating a struct array

问题

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

这是我在视图上的代码:

  1. <div class="tab-content">
  2. <div class="tab-pane active" id="tab1" >
  3. <hr/>
  4. {{range .c}}
  5. <p>Number: {{.Number}}</p>
  6. <p>Name: {{.Name}}</p>
  7. <p>Parties: {{.A}} and {{.B}}</p>
  8. <p>Location: {{.Location}}</p>
  9. <a href="/search">Read More</a>
  10. <hr/>
  11. {{end}}
  12. <div class="paging">
  13. <ul class="pagination">
  14. <li><a href="#"><i class="fa fa-angle-left"></i></a></li>
  15. <li class="active"><a href="#">1</a></li>
  16. <li><a href="#">2</a></li>
  17. <li><a href="#">3</a></li>
  18. <li><a href="#">4</a></li>
  19. <li><a href="#">5</a></li>
  20. <li><a href="#"><i class="fa fa-angle-right"></i></a></li>
  21. </ul>
  22. </div>
  23. </div>

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

提前感谢。

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

  1. func List(bucket string) []Data{
  2. //打开BoltDB数据库
  3. Open()
  4. defer Close()
  5. //使用预定义的结构体创建一个数组
  6. d:=make([]Data, 0)
  7. //将数据解析为字节形式保存
  8. db.View(func(tx *bolt.Tx) error {
  9. cur := tx.Bucket([]byte(bucket)).Cursor()
  10. for k, v := cur.First(); k != nil; k, v = cur.Next() {
  11. d1:=Data{}
  12. err:= json.Unmarshal(v, &d1)
  13. if err !=nil{
  14. return err
  15. }
  16. d=append(d, d1)
  17. }
  18. return nil
  19. })
  20. //返回数据数组
  21. return d
  22. }

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

英文:

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

This is what my code looks like on the view:

  1. &lt;div class=&quot;tab-content&quot;&gt;
  2. &lt;div class=&quot;tab-pane active&quot; id=&quot;tab1&quot; &gt;
  3. &lt;hr/&gt;
  4. {{range .c}}
  5. &lt;p&gt;Number: {{.Number}}&lt;/p&gt;
  6. &lt;p&gt;Name: {{.Name}}&lt;/p&gt;
  7. &lt;p&gt;Parties: {{.A}} and {{.B}}&lt;/p&gt;
  8. &lt;p&gt;Location: {{.Location}}&lt;/p&gt;
  9. &lt;a href=&quot;/search&quot;&gt;Read More&lt;/a&gt;
  10. &lt;hr/&gt;
  11. {{end}}
  12. &lt;div class=&quot;paging&quot;&gt;
  13. &lt;ul class=&quot;pagination&quot;&gt;
  14. &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;
  15. &lt;li class=&quot;active&quot;&gt;&lt;a href=&quot;#&quot;&gt;1&lt;/a&gt;&lt;/li&gt;
  16. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;2&lt;/a&gt;&lt;/li&gt;
  17. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;3&lt;/a&gt;&lt;/li&gt;
  18. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;4&lt;/a&gt;&lt;/li&gt;
  19. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;5&lt;/a&gt;&lt;/li&gt;
  20. &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;
  21. &lt;/ul&gt;
  22. &lt;/div&gt;
  23. &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

  1. func List(bucket string) []Data{
  2. //Open BoltDB database
  3. Open()
  4. defer Close()
  5. //Use a predefined struct to make an array
  6. d:=make([]Data, 0)
  7. //Fetch and unmarshal data as it is saved in byte form
  8. db.View(func(tx *bolt.Tx) error {
  9. cur := tx.Bucket([]byte(bucket)).Cursor()
  10. for k, v := cur.First(); k != nil; k, v = cur.Next() {
  11. d1:=Data{}
  12. err:= json.Unmarshal(v, &amp;d1)
  13. if err !=nil{
  14. return err
  15. }
  16. d=append(d, d1)
  17. }
  18. return nil
  19. })
  20. //Return the array of data
  21. return d
  22. }

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

答案1

得分: 2

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

  1. func paginate(x []Data, skip int, size int) []int {
  2. limit := func() int {
  3. if skip+size > len(x) {
  4. return len(x)
  5. } else {
  6. return skip + size
  7. }
  8. }
  9. start := func() int {
  10. if skip > len(x) {
  11. return len(x)
  12. } else {
  13. return skip
  14. }
  15. }
  16. return x[start():limit()]
  17. }

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

英文:

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

  1. func paginate(x []Data, skip int, size int) []int {
  2. limit := func() int {
  3. if skip+size &gt; len(x) {
  4. return len(x)
  5. } else {
  6. return skip + size
  7. }
  8. }
  9. start := func() int {
  10. if skip &gt; len(x) {
  11. return len(x)
  12. } else {
  13. return skip
  14. }
  15. }
  16. return x[start():limit()]
  17. }

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:

确定