doudou521125 2017-10-16 09:16
浏览 12
已采纳

转到语言增量编号

I can't make increment number of rows on my table..
I used revel framework, and try make simple crud.

The question is: "How to show increment number with Golang.?".

This my code of controller book.go
This controller to show data from postgresql database.

func allBooks() ([]models.Book, error) {
    //Retrieve
    books := []models.Book{}

    rows, err := db.Query("SELECT id, name, author, pages, publication_date FROM books order by id")
    defer rows.Close()
    if err == nil {
        for rows.Next() {
            var id int
            var name string
            var author string
            var pages int
            var publicationDate pq.NullTime

            err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
            if err == nil {
                currentBook := models.Book{ID: id, Name: name, Author: author, Pages: pages}
                if publicationDate.Valid {
                    currentBook.PublicationDate = publicationDate.Time
                }

                books = append(books, currentBook)
            } else {
                return books, err
            }

        }
    } else {
        return books, err
    }
    return books, err
}  

And this html on my views index.html:

<table class="table table-striped">
  <thead>
    <tr>
      <th>No</th>
      <th>ID</th>
      <th>Name</th>
      <th>Author</th>
      <th>Pages</th>
      <th>Publication Date</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>

    {{range .books}}
    <tr>
      <td>{{.Nomor}}</td>
      <td>{{.ID}}</td>
      <td>{{.Name}}</td>
      <td>{{.Author}}</td>
      <td>{{.Pages}}</td>
      <td>{{.PublicationDateStr}}</td>
    </tr>
    {{end}}
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><a href="book.html" class="btn btn-primary">New book</a></td>
    </tr>
  </tbody>
</table>  

hope result like this

|No.|ID |Name |Author | Pages | Publication Date  |
|1  |2  |Jack |Holly  | 255   | 2017-10-15        |
|2  |4  |hans |Holly  | 255   | 2017-10-15        |
|3  |6  |Juri |Holly  | 255   | 2017-10-15        |
|4  |7  |Hank |Holly  | 255   | 2017-10-15        |

but what I get is

|No.|ID |Name |Author | Pages | Publication Date | 
|0  |2  |Jack |Holly  | 255   | 2017-10-15       | 
|0  |4  |hans |Holly  | 255   | 2017-10-15       | 
|0  |6  |Juri |Holly  | 255   | 2017-10-15       | 
|0  |7  |Hank |Holly  | 255   | 2017-10-15       |
  • 写回答

2条回答 默认 最新

  • douhua1760 2017-10-16 09:52
    关注

    I do not see the property Nomor populated anywhere.

    You should keep a counter and do something like

    ...
    currentBook := models.Book{Nomor: count, ID: id, Name: name, Author: author, Pages: pages}
    count = count + 1
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题