data_table.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package builder
  2. import (
  3. "fmt"
  4. "html"
  5. "math"
  6. "strconv"
  7. "golang-fave/engine/wrapper"
  8. )
  9. type DataTableRow struct {
  10. DBField string
  11. NameInTable string
  12. CallBack func(values *[]string) string
  13. }
  14. func DataTable(wrap *wrapper.Wrapper, table string, order_by string, order_way string, data []DataTableRow, action func(values *[]string) string, pagination_url string) string {
  15. var num int
  16. err := wrap.DB.QueryRow("SELECT COUNT(*) FROM `" + table + "`;").Scan(&num)
  17. if err != nil {
  18. return ""
  19. }
  20. pear_page := 10
  21. max_pages := int(math.Ceil(float64(num) / float64(pear_page)))
  22. curr_page := 1
  23. p := wrap.R.URL.Query().Get("p")
  24. if p != "" {
  25. pi, err := strconv.Atoi(p)
  26. if err != nil {
  27. curr_page = 1
  28. } else {
  29. if pi < 1 {
  30. curr_page = 1
  31. } else if pi > max_pages {
  32. curr_page = max_pages
  33. } else {
  34. curr_page = pi
  35. }
  36. }
  37. }
  38. limit_offset := curr_page*pear_page - pear_page
  39. result := `<table class="table data-table table-striped table-bordered table-hover table_` + table + `">`
  40. result += `<thead>`
  41. result += `<tr>`
  42. sql := "SELECT"
  43. for i, column := range data {
  44. if column.NameInTable != "" {
  45. result += `<th scope="col" class="col_` + column.DBField + `">` + html.EscapeString(column.NameInTable) + `</th>`
  46. }
  47. sql += " `" + column.DBField + "`"
  48. if i+1 < len(data) {
  49. sql += ","
  50. }
  51. }
  52. sql += " FROM `" + table + "` ORDER BY `" + order_by + "` " + order_way + " LIMIT ?, ?;"
  53. if action != nil {
  54. result += `<th scope="col" class="col_action">&nbsp;</th>`
  55. }
  56. result += `</tr>`
  57. result += `</thead>`
  58. result += `<tbody>`
  59. rows, err := wrap.DB.Query(sql, limit_offset, pear_page)
  60. if err == nil {
  61. values := make([]string, len(data))
  62. scan := make([]interface{}, len(values))
  63. for i := range values {
  64. scan[i] = &values[i]
  65. }
  66. for rows.Next() {
  67. err = rows.Scan(scan...)
  68. if err == nil {
  69. result += `<tr>`
  70. for i, val := range values {
  71. if data[i].NameInTable != "" {
  72. if data[i].CallBack == nil {
  73. result += `<td class="col_` + data[i].DBField + `">` + html.EscapeString(string(val)) + `</td>`
  74. } else {
  75. result += `<td class="col_` + data[i].DBField + `">` + data[i].CallBack(&values) + `</td>`
  76. }
  77. }
  78. }
  79. if action != nil {
  80. result += `<td class="col_action">` + action(&values) + `</td>`
  81. }
  82. result += `</tr>`
  83. }
  84. }
  85. }
  86. result += `</tbody></table>`
  87. // Show page navigation only if pages more then one
  88. if max_pages > 1 {
  89. result += `<nav>`
  90. result += `<ul class="pagination" style="margin-bottom:0px;">`
  91. class := ""
  92. if curr_page <= 1 {
  93. class = " disabled"
  94. }
  95. result += `<li class="page-item` + class + `">`
  96. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page-1) + `" aria-label="Previous">`
  97. result += `<span aria-hidden="true">&laquo;</span>`
  98. result += `<span class="sr-only">Previous</span>`
  99. result += `</a>`
  100. result += `</li>`
  101. for i := 1; i <= max_pages; i++ {
  102. class = ""
  103. if i == curr_page {
  104. class = " active"
  105. }
  106. result += `<li class="page-item` + class + `">`
  107. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", i) + `">` + fmt.Sprintf("%d", i) + `</a>`
  108. result += `</li>`
  109. }
  110. class = ""
  111. if curr_page >= max_pages {
  112. class = " disabled"
  113. }
  114. result += `<li class="page-item` + class + `">`
  115. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page+1) + `" aria-label="Next">`
  116. result += `<span aria-hidden="true">&raquo;</span>`
  117. result += `<span class="sr-only">Next</span>`
  118. result += `</a>`
  119. result += `</li>`
  120. result += `</ul>`
  121. result += `</nav>`
  122. }
  123. return result
  124. }