data_table.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package builder
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "html"
  6. "math"
  7. "strconv"
  8. "golang-fave/engine/sqlw"
  9. "golang-fave/engine/utils"
  10. "golang-fave/engine/wrapper"
  11. )
  12. type DataTableRow struct {
  13. DBField string
  14. DBExp string
  15. NameInTable string
  16. Classes string
  17. CallBack func(values *[]string) string
  18. }
  19. func DataTable(
  20. wrap *wrapper.Wrapper,
  21. table string,
  22. order_by string,
  23. order_way string,
  24. data *[]DataTableRow,
  25. action func(values *[]string) string,
  26. pagination_url string,
  27. custom_sql_count func() (int, error),
  28. custom_sql_data func(limit_offset int, pear_page int) (*sqlw.Rows, error),
  29. pagination_enabled bool,
  30. ) string {
  31. var num int
  32. var err error
  33. if pagination_enabled {
  34. if custom_sql_count != nil {
  35. num, err = custom_sql_count()
  36. wrap.LogCpError(&err)
  37. } else {
  38. err = wrap.DB.QueryRow(wrap.R.Context(), "SELECT COUNT(*) FROM `"+table+"`;").Scan(&num)
  39. if *wrap.LogCpError(&err) != nil {
  40. return ""
  41. }
  42. }
  43. } else {
  44. num = 0
  45. }
  46. pear_page := 10
  47. max_pages := int(math.Ceil(float64(num) / float64(pear_page)))
  48. curr_page := 1
  49. p := wrap.R.URL.Query().Get("p")
  50. if p != "" {
  51. pi, err := strconv.Atoi(p)
  52. if err != nil {
  53. curr_page = 1
  54. } else {
  55. if pi < 1 {
  56. curr_page = 1
  57. } else if pi > max_pages {
  58. curr_page = max_pages
  59. } else {
  60. curr_page = pi
  61. }
  62. }
  63. }
  64. limit_offset := curr_page*pear_page - pear_page
  65. result := `<table id="cp-table-` + table + `" class="table data-table table-striped table-bordered table-hover table_` + table + `">`
  66. result += `<thead>`
  67. result += `<tr>`
  68. qsql := ""
  69. if custom_sql_data == nil {
  70. qsql = "SELECT"
  71. }
  72. for i, column := range *data {
  73. if column.NameInTable != "" {
  74. classes := column.Classes
  75. if classes != "" {
  76. classes = " " + classes
  77. }
  78. result += `<th scope="col" class="col_` + column.DBField + classes + `">` + html.EscapeString(column.NameInTable) + `</th>`
  79. }
  80. if custom_sql_data == nil {
  81. if column.DBExp == "" {
  82. qsql += " `" + column.DBField + "`"
  83. } else {
  84. qsql += " " + column.DBExp + " as `" + column.DBField + "`"
  85. }
  86. if i+1 < len(*data) {
  87. qsql += ","
  88. }
  89. }
  90. }
  91. if custom_sql_data == nil {
  92. qsql += " FROM `" + table + "` ORDER BY `" + order_by + "` " + order_way + " LIMIT ?, ?;"
  93. }
  94. if action != nil {
  95. result += `<th scope="col" class="col_action">&nbsp;</th>`
  96. }
  97. result += `</tr>`
  98. result += `</thead>`
  99. result += `<tbody>`
  100. if num > 0 || !pagination_enabled {
  101. have_records := false
  102. var rows *sqlw.Rows
  103. var err error
  104. if custom_sql_data == nil {
  105. rows, err = wrap.DB.Query(wrap.R.Context(), qsql, limit_offset, pear_page)
  106. } else {
  107. rows, err = custom_sql_data(limit_offset, pear_page)
  108. }
  109. if *wrap.LogCpError(&err) == nil {
  110. values := make([]sql.NullString, len(*data))
  111. scan := make([]interface{}, len(values))
  112. for i := range values {
  113. scan[i] = &values[i]
  114. }
  115. for rows.Next() {
  116. err = rows.Scan(scan...)
  117. if *wrap.LogCpError(&err) == nil {
  118. if !have_records {
  119. have_records = true
  120. }
  121. result += `<tr>`
  122. for i, val := range values {
  123. if (*data)[i].NameInTable != "" {
  124. classes := (*data)[i].Classes
  125. if classes != "" {
  126. classes = " " + classes
  127. }
  128. if (*data)[i].CallBack == nil {
  129. result += `<td class="col_` + (*data)[i].DBField + classes + `">` + html.EscapeString(string(val.String)) + `</td>`
  130. } else {
  131. result += `<td class="col_` + (*data)[i].DBField + classes + `">` + (*data)[i].CallBack(utils.SqlNullStringToString(&values)) + `</td>`
  132. }
  133. }
  134. }
  135. if action != nil {
  136. result += `<td class="col_action">` + action(utils.SqlNullStringToString(&values)) + `</td>`
  137. }
  138. result += `</tr>`
  139. }
  140. }
  141. rows.Close()
  142. }
  143. if !have_records {
  144. result += `<tr><td colspan="50">No data</td></tr>`
  145. }
  146. } else {
  147. result += `<tr><td colspan="50">No data</td></tr>`
  148. }
  149. result += `</tbody></table>`
  150. // Show page navigation only if pages more then one
  151. if pagination_enabled {
  152. if max_pages > 1 {
  153. result += `<nav>`
  154. result += `<ul class="pagination" style="margin-bottom:0px;">`
  155. class := ""
  156. if curr_page <= 1 {
  157. class = " disabled"
  158. }
  159. result += `<li class="page-item` + class + `">`
  160. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page-1) + `" aria-label="Previous">`
  161. result += `<span aria-hidden="true">&laquo;</span>`
  162. result += `<span class="sr-only">Previous</span>`
  163. result += `</a>`
  164. result += `</li>`
  165. before := false
  166. after := false
  167. for i := 1; i <= max_pages; i++ {
  168. // Before
  169. if curr_page >= 5 && i > 1 && i+1 < curr_page {
  170. if !before {
  171. before = true
  172. result += `<li class="page-item disabled"><a class="page-link" href="">...</a></li>`
  173. }
  174. continue
  175. }
  176. // After
  177. if i-1 > curr_page && i < max_pages {
  178. if !after {
  179. after = true
  180. result += `<li class="page-item disabled"><a class="page-link" href="">...</a></li>`
  181. }
  182. continue
  183. }
  184. class = ""
  185. if i == curr_page {
  186. class = " active"
  187. }
  188. result += `<li class="page-item` + class + `">`
  189. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", i) + `">` + fmt.Sprintf("%d", i) + `</a>`
  190. result += `</li>`
  191. }
  192. class = ""
  193. if curr_page >= max_pages {
  194. class = " disabled"
  195. }
  196. result += `<li class="page-item` + class + `">`
  197. result += `<a class="page-link" href="` + pagination_url + `?p=` + fmt.Sprintf("%d", curr_page+1) + `" aria-label="Next">`
  198. result += `<span aria-hidden="true">&raquo;</span>`
  199. result += `<span class="sr-only">Next</span>`
  200. result += `</a>`
  201. result += `</li>`
  202. result += `</ul>`
  203. result += `</nav>`
  204. }
  205. }
  206. return result
  207. }