package builder import ( "database/sql" "fmt" "html" "math" "strconv" "golang-fave/engine/sqlw" "golang-fave/engine/utils" "golang-fave/engine/wrapper" ) type DataTableRow struct { DBField string DBExp string NameInTable string Classes string CallBack func(values *[]string) string } func DataTable( wrap *wrapper.Wrapper, table string, order_by string, order_way string, data *[]DataTableRow, action func(values *[]string) string, pagination_url string, custom_sql_count func() (int, error), custom_sql_data func(limit_offset int, pear_page int) (*sqlw.Rows, error), pagination_enabled bool, ) string { var num int var err error if pagination_enabled { if custom_sql_count != nil { num, err = custom_sql_count() wrap.LogCpError(&err) } else { err = wrap.DB.QueryRow(wrap.R.Context(), "SELECT COUNT(*) FROM `"+table+"`;").Scan(&num) if *wrap.LogCpError(&err) != nil { return "" } } } else { num = 0 } pear_page := 10 max_pages := int(math.Ceil(float64(num) / float64(pear_page))) curr_page := 1 p := wrap.R.URL.Query().Get("p") if p != "" { pi, err := strconv.Atoi(p) if err != nil { curr_page = 1 } else { if pi < 1 { curr_page = 1 } else if pi > max_pages { curr_page = max_pages } else { curr_page = pi } } } limit_offset := curr_page*pear_page - pear_page result := `
` + html.EscapeString(column.NameInTable) + ` | ` } if custom_sql_data == nil { if column.DBExp == "" { qsql += " `" + column.DBField + "`" } else { qsql += " " + column.DBExp + " as `" + column.DBField + "`" } if i+1 < len(*data) { qsql += "," } } } if custom_sql_data == nil { qsql += " FROM `" + table + "` ORDER BY `" + order_by + "` " + order_way + " LIMIT ?, ?;" } if action != nil { result += `` } result += ` | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
` + html.EscapeString(string(val.String)) + ` | ` } else { result += `` + (*data)[i].CallBack(utils.SqlNullStringToString(&values)) + ` | ` } } } if action != nil { result += `` + action(utils.SqlNullStringToString(&values)) + ` | ` } result += `|||||||||||||||||||||||||||||||||||||||||||||||
No data | |||||||||||||||||||||||||||||||||||||||||||||||||
No data |