Skip to content

Datatable Server Side pagination for golang whit gorm fork from jinzhu and any beego

License

Notifications You must be signed in to change notification settings

juaismar/gormssp

Repository files navigation

gormssp

Using Datatables pagination with golang

Build Status Go Report Card codecov MIT licensed GoDoc PkgGoDev GitHub release (latest by date)

Pre-requisites 📋

This is for old gorm, for the new gorm (https://github.com/go-gorm/gorm) use this https://github.com/juaismar/go-gormssp

Database compatible: Postgres (stable), SQLite (without REGEXP)

Installation 🔧

Install with the next command:

go get github.com/juaismar/gormssp/v2

and import the package with:

import ("github.com/juaismar/gormssp/v2")

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 (
	SSP "github.com/juaismar/gormssp/v2"
)

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 := []SSP.Data{
    {Db: "name", Dt: 0, Formatter: nil},
    {Db: "role", Dt: 1, Formatter: nil},
    {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 an example of data formatting.

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/v2")

func (c *User) Pagination() {
    columns := []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
    var whereJoin = make(map[string]string, 0)
    whereAll = append(whereAll, "deleted_at IS NULL")

    c.Data["json"], _ = SSP.Complex(c, model.ORM, "events", columns, whereResult, whereAll, whereJoin)
    c.ServeJSON()
}

-Tables with spaces in the name or styart whit numbers must be between ".

SSP.Data{Db: "\"1My special table\"", Dt: 3, Formatter: nil}

-Can implement functions in search, example:accent insensitive search

SELECT * FROM users WHERE f_unaccent(name) LIKE '%maria%';.

can be implemented as:

SSP.Data{Db: "name", Dt: 2, Formatter: nil, Sf: "f_unaccent(name)"}

-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 ✒️

Thanks 🎁

Readme.md based in https://gist.github.com/Villanuevand/6386899f70346d4580c723232524d35a

About

Datatable Server Side pagination for golang whit gorm fork from jinzhu and any beego

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages