0

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.

7
  • If you do not wish to implement a SQL based solution (most efficient, least flexible). You can do math and iterate over the result set in javascript or do same math in your revel controller. You will need to calculate things like total_pages = rows_returned/pagesize etc. Things get a bit complicated if u want to combine filtering and grouping. Commented Jan 6, 2016 at 6:14
  • Also, tell us what package you are using for data access. What does the controller look like ? Commented Jan 6, 2016 at 6:15
  • Thank you @Perpetualcoder for the fast response. I have updated the question. I hope it covers all you asked for. Commented Jan 6, 2016 at 6:31
  • I just wrote some super untested code, it should give you a hint as to what can be done. Cheers! Commented Jan 6, 2016 at 7:14
  • 1
    Let me know if you want me to tweak answer. The skip value will be size * (pageneeded-1) which you will need to pass it to controller from your view. In programming community we grow by helping others. Some day you will help me out too. Commented Jan 7, 2016 at 20:10

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.