Golang Revel:对结构数组进行分页
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{
//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
}
这个数组就是我想在视图上迭代的数组。
您可以从列表函数中收集您 return 的全部数据。
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()]
}
虽然你会得到你想要的行为,但是这在内存方面是非常浪费的,特别是如果你的数据数组很大。
希望这有帮助。
我有一个需要在视图端分页的结构数组。
这是我的代码在视图中的样子:
<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{
//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
}
这个数组就是我想在视图上迭代的数组。
您可以从列表函数中收集您 return 的全部数据。
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()]
}
虽然你会得到你想要的行为,但是这在内存方面是非常浪费的,特别是如果你的数据数组很大。 希望这有帮助。