README
ยถ
gormssp
Using Datatables pagination with golang
Pre-requisites ๐
Database compatible: Postgres (stable), SQLite (whitout REGEXP)
- Obviously use it in a golang project
- Gorm package (https://gorm.io/) (https://github.com/jinzhu/gorm)
- Beego package (https://beego.me/) (https://github.com/astaxie/beego)
Installation ๐ง
Install with the next command:
go get github.com/juaismar/gormssp
and import the package with:
import ("github.com/juaismar/gormssp")
Working example ๐
A working example on https://github.com/juaismar/GormSSP_Example
-This is a simple code that sends data to the Datatables JS client.
import ("github.com/juaismar/gormssp")
func (c *User) Pagination() {
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes but can be a string
// Formatter is a function to customize the value of field , can be nil.
columns := make(map[int]SSP.Data)
columns[0] = SSP.Data{Db: "name", Dt: 0, Formatter: nil}
columns[1] = SSP.Data{Db: "role", Dt: 1, Formatter: nil}
columns[2] = SSP.Data{Db: "email", Dt: 2, Formatter: nil}
// Send the data to the client
// "users" is the name of the table
c.Data["json"], _ = SSP.Simple(c, model.ORM, "users", columns)
c.ServeJSON()
}
-This is a example of data formatting.
columns[3] = SSP.Data{Db: "registered", Dt: 3, Formatter: func(
data interface{}, row map[string]interface{}) (interface{}, error) {
//data is the value id column, row is a map whit the values of all columns
if data != nil {
return data.(time.Time).Format("2006-01-02 15:04:05"), nil
}
return "", nil
}}
-This is a complex example.
import ("github.com/juaismar/gormssp")
func (c *User) Pagination() {
columns := make(map[int]SSP.Data)
columns[0] = SSP.Data{Db: "id", Dt: "id", Formatter: nil}
//whereResult is a WHERE condition to apply to the result set
//whereAll is a WHERE condition to apply to all queries
var whereResult []string
var whereAll []string
whereAll = append(whereAll, "deleted_at IS NULL")
c.Data["json"], _ = SSP.Complex(c, model.ORM, "events", columns, whereResult, whereAll)
c.ServeJSON()
}
-This project is based in the PHP version of datatables pagination in https://datatables.net/examples/data_sources/server_side -Original file can be found in https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php
Author โ๏ธ
- Juan Iscar - (https://github.com/juaismar)
Thanks ๐
- All my friends at work.
- Sergio(https://github.com/serveba) and Mario (https://github.com/mapreal19) who taught me how to program golang and showed me the wonderful world of good practices.
- Juan, Juan and Joaquin.
Readme.md based in https://gist.github.com/Villanuevand/6386899f70346d4580c723232524d35a
Documentation
ยถ
Index ยถ
Constants ยถ
This section is empty.
Variables ยถ
This section is empty.
Functions ยถ
This section is empty.
Types ยถ
type Controller ยถ added in v0.2.4
Controller emulate the beego controller
type Data ยถ
type Data struct { Db string //name of column Dt interface{} //id of column in client (int or string) Cs bool //case sensitive - optional default false Formatter func(data interface{}, row map[string]interface{}) (interface{}, error) // - optional }
Data is a line in map that link the database field with datatable field
type MessageDataTable ยถ
type MessageDataTable struct { Draw int `json:"draw"` RecordsTotal int `json:"recordsTotal"` RecordsFiltered int `json:"recordsFiltered"` Data []interface{} `json:"data,nilasempty"` }
MessageDataTable is theresponse object
func Complex ยถ
func Complex(c Controller, conn *gorm.DB, table string, columns map[int]Data, whereResult []string, whereAll []string) (responseJSON MessageDataTable, err error)
Complex is a main method, externally called
func Simple ยถ
func Simple(c Controller, conn *gorm.DB, table string, columns map[int]Data) (responseJSON MessageDataTable, err error)
Simple is a main method, externally called