设为首页 加入收藏

TOP

Gin-Go学习笔记三:Gin-Web框架 JS分页(二)
2017-12-20 12:50:15 】 浏览:2514
Tags:Gin-Go 学习 笔记 Gin-Web 框架 JS分页
LECT id, first_name, last_name FROM person WHERE id=?", Id).Scan( &person.Id, &person.FirstName, &person.LastName, ) //打印错误 if err != nil { log.Println(err) } //返回对象 return &person }

  

3>     控制器person.go,其代码如下:

    

package apis

import (
	"fmt"
	"strconv"
  "net/http"
  "log"
  "github.com/gin-gonic/gin"
 . "GinLearn/GinLearn/models"
)
//初始页面
func IndexApi(c *gin.Context) {
  c.String(http.StatusOK, "Hello World!")
}
//渲染html页面
func ShowHtmlPage(c *gin.Context) {
  c.HTML(http.StatusOK, "index.html", gin.H{
      "title": "GIN: HTML页面",
  })
}


//列表页面
func ListHtml(c *gin.Context) {  
  c.HTML(http.StatusOK, "list.html", gin.H{
      "title": "GIN: 用户列表页面",
  })
}
//列表页面数据
func GetDataList(c *gin.Context) {  
  //得到请求的参数
  search:=c.PostForm("search")
  num:=c.PostForm("pageno")
  pageno,err:= strconv.Atoi(num) 
  if err!=nil{
    log.Fatalln(err)
  }
  //得到数据集
  datalist:=GetPersonList(pageno,3,search)

  //得到记录的总数
  count:=GetRecordNum(search)
 //返回结果
 c.JSON(http.StatusOK, gin.H{
  "datalist": datalist,
  "count":count,
  "pagesize":3,
  "pageno":pageno,
 })
}
//列表页面数据
func PageNextData(c *gin.Context) {  
  //得到请求的参数
  search:=c.PostForm("search")
  num:=c.PostForm("pageno")
  pageno,err:= strconv.Atoi(num) 
  if err!=nil{
    log.Fatalln(err)
  }
  //得到数据集
  datalist:=GetPersonList(pageno,3,search)

  //得到记录的总数
  count:=GetRecordNum(search)
 //返回结果
 c.JSON(http.StatusOK, gin.H{
  "datalist": datalist,
  "count":count,
  "pagesize":3,
  "pageno":pageno,
 })
}
//新增页面
func AddHtml(c *gin.Context){
  c.HTML(http.StatusOK, "add.html", gin.H{
    "title": "GIN: 新增用户页面",
  })
}
//新增记录
func AddPersonApi(c *gin.Context) {
 
   //得到请求的参数
   firstName := c.PostForm("first_name")
   lastName := c.PostForm("last_name")
   fmt.Println("执行到此处A")
   //赋值
   p := Person{FirstName: firstName, LastName: lastName}
   //调用模型中的新增的方法
   ra:= p.AddPerson()
   //返回结果
   c.JSON(http.StatusOK, gin.H{
    "success": ra,
  })
  c.Redirect(http.StatusOK,"/home/list")
}
//编辑页面
func EditHtml(c *gin.Context){
  //得到URL请求的参数
  num:=c.Query("id")

  id,err:= strconv.Atoi(num) 
 
  if err!=nil{
    log.Fatalln(err)
  }

  p:=GetPersonById(id)
   if p==nil{
    fmt.Println("得到数据错误")
   }else{
    fmt.Println(p)
    fmt.Println("得到数据正确")
   }

  c.HTML(http.StatusOK, "edit.html", gin.H{
    "title": "GIN: 编辑用户页面",
    "id":p.Id,
    "firstname":p.FirstName,
    "lastname":p.LastName,
  })
}
//编辑记录
func EditPersonApi(c *gin.Context) {
  fmt.Println("执行到此处")
  //得到请求的参数
  num:=c.PostForm("id")
  fmt.Println(num)
  id,err:= strconv.Atoi(num) 
  if err!=nil{
    log.Fatalln(err)
  }
  firstName := c.PostForm("first_name")
  lastName := c.PostForm("last_name")
  //赋值
  p := GetPersonById(id)
  p.FirstName=firstName
  p.LastName=lastName
  //调用模型中的编辑的方法
  ra:= p.EditPerson()
  //返回结果
  c.JSON(http.StatusOK, gin
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Gin-Go学习笔记七:Gin-Web框架 .. 下一篇Gin-Go学习笔记一:Hello World

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目