Documentation
¶
Overview ¶
Package datatablessrv handles the server side processing of an AJAX request for DataTables For details on the parameters and the results, read the datatables documentation at https://datatables.net/manual/server-side
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Dbx sqlx.DB
Database Instance
var ErrNotDataTablesReq = errors.New("Not a DataTables request")
ErrNotDataTablesReq indicates that this is not being requested by Datatables
Functions ¶
Types ¶
type ColData ¶
type ColData struct {
// columns[i][name] Column's name, as defined by columns.name.
Name string
// columns[i][data] Column's data source, as defined by columns.data.
// It is poss
Data string
// columns[i][searchable] boolean Flag to indicate if this column is searchable (true) or not (false).
// This is controlled by columns.searchable.
Searchable bool
// columns[i][orderable] Flag to indicate if this column is orderable (true) or not (false).
// This is controlled by columns.orderable.
Orderable bool
// columns[i][search][value] Search value to apply to this specific column.
Searchval string
// columns[i][search][regex]
// Flag to indicate if the search term for this column should be treated as regular expression (true) or not (false).
// As with global search, normally server-side processing scripts will not perform regular expression searching
// for performance reasons on large data sets, but it is technically possible and at the discretion of your script.
UseRegex bool
}
ColData tracks all of the columns requested by DataTables
type DataTablesInfo ¶
type DataTablesInfo struct {
// HasFilter Indicates there is a filter on the data to apply. It is used to optimize generating
// the query filters
HasFilter bool
// Draw counter. This is used by DataTables to ensure that the Ajax returns
// from server-side processing requests are drawn in sequence by DataTables
// (Ajax requests are asynchronous and thus can return out of sequence).
// This is used as part of the draw return parameter (see below).
Draw int
// Start is the paging first record indicator.
// This is the start point in the current data set (0 index based - i.e. 0 is the first record).
Start int
// Length is the number of records that the table can display in the current draw.
// It is expected that the number of records returned will be equal to this number, unless the server has fewer records to return.
// Note that this can be -1 to indicate that all records should be returned (although that negates any benefits of server-side processing!)
Length int
// Searchval holds the global search value. To be applied to all columns which have searchable as true.
Searchval string
// UseRegex is true if the global filter should be treated as a regular expression for advanced searching.
// Note that normally server-side processing scripts will not perform regular expression
// searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.
UseRegex bool
// Order provides information about what columns are to be ordered in the results and which direction
Order []OrderInfo
// Columns provides a mapping of what fields are to be searched
Columns []ColData
}
DataTablesInfo represents all of the information that was requested by DataTables
func ParseDatatablesRequest ¶
func ParseDatatablesRequest(r *http.Request) (res *DataTablesInfo, err error)
ParseDatatablesRequest checks the HTTP request to see if it corresponds to a datatables AJAX data request and parses the request data into the DataTablesInfo structure.
This structure can be used by MySQLFilter and MySQLOrderby to generate a MySQL query to run against a database.
For example assuming you are going to fill in a response structure to DataTables such as:
type QueryResponse struct {
DateAdded time.Time
Status string
Email struct {
Name string
Email string
}
}
var emailQueueFields = map[string]string{
"DateAdded": "t1.dateadded",
"Status": "t1.status",
"Email.Name": "t2.Name",
"Email.Email": "t2.Email",
}
const baseQuery = `
SELECT t1.dateadded
,t1.status
,t2.Name
,t2.Email
FROM infotable t1
LEFT JOIN usertable t2
ON t1.key = t2.key`
// See if we have a where clause to add to the base query
query := baseQuery
sqlPart, err := di.MySQLFilter(sqlFields)
// If we did have a where filter, append it. Note that it doesn't put the " WHERE "
// in front because we might be doing a boolean operation.
if sqlPart != "" {
query += " WHERE " + sqlPart
}
sqlPart, err = di.MySQLOrderby(sqlFields)
query += sqlPart
At that point you have a query that you can send straight to mySQL
func (*DataTablesInfo) SetDbX ¶
func (di *DataTablesInfo) SetDbX(db *sqlx.DB)
type DataTablesResponse ¶
type DataTablesResponse struct {
// Draw counter. This is used by DataTables to ensure that the Ajax returns
// from server-side processing requests are drawn in sequence by DataTables
// (Ajax requests are asynchronous and thus can return out of sequence).
// This is used as part of the draw return parameter (see below).
Draw int `json:"draw"`
// Total records, before filtering (i.e. the total number of records in the database)
RecordsTotal int `json:"recordsTotal"`
// Total records, after filtering
// (i.e. the total number of records after filtering has been applied - not just the number of records being returned for this page of data).
RecordsFiltered int `json:"recordsFiltered"`
// The data to be displayed in the table. This is an array of data source objects, one for each row, which will be used by DataTables.
// Note that this parameter's name can be changed using the ajax option's dataSrc property.
Data []map[string]string `json:"data"`
}
type OrderInfo ¶
type OrderInfo struct {
// ColNum indicates Which column to apply sorting to (zero based index to the Columns data)
ColNum int
// Direction tells us which way to sort
Direction SortDir
}
OrderInfo tracks the list of columns to sort by and in which direction to sort them.
type Route ¶
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type UiView ¶
type UiView struct {
Title string
AjaxEndpoint string
// using a slice, will preserve the column order when ranging through it
TableHeaders []tableColumnMap
// this is only used to generate poller list
Pollers []common.Poller
// version info
Version string
CommitSHA string
}
Helper struct that defines the UI view
Source Files
¶
- datatables.go
- handler_clusters.go
- handler_datacenter.go
- handler_datastore.go
- handler_dt.go
- handler_esxi.go
- handler_folders.go
- handler_poller.go
- handler_portgroup.go
- handler_resourcepools.go
- handler_ui.go
- handler_vcenter.go
- handler_vdisks.go
- handler_virtualmachine.go
- handler_vnics.go
- handler_vswitch.go
- routing.go
- server.go
- uiview.go