英文:
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:
<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
得分: 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 > 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论