Documentation
¶
Index ¶
- type Column
- type ColumnRequest
- type Config
- type DataTable
- func (dt *DataTable) AddColumn(col Column) *DataTable
- func (dt *DataTable) AddColumns(columns ...Column) *DataTable
- func (dt *DataTable) BlacklistColumn(columns ...string) *DataTable
- func (dt *DataTable) CaseInsensitive() *DataTable
- func (dt *DataTable) DisableOrder() *DataTable
- func (dt *DataTable) DisablePagination() *DataTable
- func (dt *DataTable) DisableSearch() *DataTable
- func (dt *DataTable) EditColumn(name string, editFunc func(any) any) *DataTable
- func (dt *DataTable) Filter(filterFunc func(*gorm.DB) *gorm.DB) *DataTable
- func (dt *DataTable) FinalizeResponseColumns(data []map[string]any) []map[string]any
- func (dt *DataTable) Make() (map[string]any, error)
- func (dt *DataTable) Model(model any) *DataTable
- func (dt *DataTable) Only(columns ...string) *DataTable
- func (dt *DataTable) Raw() (any, error)
- func (dt *DataTable) RemoveColumn(data ...string) *DataTable
- func (dt *DataTable) Req(req Request) *DataTable
- func (dt *DataTable) SetConfig(config Config) *DataTable
- func (dt *DataTable) SetFilteredRecords(count int64) *DataTable
- func (dt *DataTable) SetRowAttributes(idFunc func(map[string]any) string, class string, ...) *DataTable
- func (dt *DataTable) SetTotalRecords(count int64) *DataTable
- func (dt *DataTable) SkipPaging() *DataTable
- func (dt *DataTable) Validate() error
- func (dt *DataTable) WhitelistColumn(columns ...string) *DataTable
- func (dt *DataTable) With(relations ...string) *DataTable
- func (dt *DataTable) WithData(key string, value any) *DataTable
- func (dt *DataTable) WithNumber() *DataTable
- type Order
- type Request
- type Search
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct { Searchable bool Orderable bool Name string Data string RenderFunc func(map[string]any) any }
Column represents a single column in a DataTable.
A Column has the following properties:
Fields:
- Searchable: A boolean indicating whether the column is searchable.
- Orderable: A boolean indicating whether the column is orderable.
- Name: The display name of the column.
- Data: The data property name of the column.
- RenderFunc: An optional function that can be used to render the column value.
type ColumnRequest ¶
type ColumnRequest struct { Searchable bool `form:"searchable"` Orderable bool `form:"orderable"` Data string `form:"data"` Name string `form:"name"` Search Search `form:"search"` }
ColumnRequest represents a request for a DataTable column configuration.
Fields:
- Searchable: Indicates if the column is searchable.
- Orderable: Indicates if the column can be ordered.
- Data: The data property name of the column.
- Name: The display name of the column.
- Search: The search criteria applied to the column.
type Config ¶
type Config struct { Searchable bool Orderable bool Paginate bool Union bool Distinct bool CaseInsensitive bool ResponseFormat string GroupBy []string Having []string DefaultSort map[string]string }
Config holds the configuration options for a DataTable.
The Config struct allows customization of various features such as searchability, ordering, pagination, and more.
Fields:
- Searchable: Enables or disables global searching.
- Orderable: Enables or disables column ordering.
- Paginate: Enables or disables pagination.
- Union: Allows the use of UNION in queries.
- Distinct: Enables DISTINCT selection in queries.
- CaseInsensitive: Enables case-insensitive searches.
- ResponseFormat: Specifies the format of the response.
- GroupBy: Specifies columns for GROUP BY clause.
- Having: Specifies conditions for HAVING clause.
- DefaultSort: Specifies default sorting for columns.
type DataTable ¶
type DataTable struct {
// contains filtered or unexported fields
}
The data is represented by the totalRecords, filteredRecords, and columns fields, which are used to store the data.
func (*DataTable) AddColumn ¶
AddColumn adds a column to the DataTable. If a column with the same Data field exists, it is overwritten. The column is added to the columnsMap with the Data field as the key.
func (*DataTable) AddColumns ¶
AddColumns adds multiple columns to the DataTable. If a column with the same Data field exists, it is overwritten. The columns are added to the columnsMap with the Data field as the key.
func (*DataTable) BlacklistColumn ¶
BlacklistColumn marks one or more columns as blacklisted. Columns that are blacklisted will be excluded from the final response. If no columns are passed, this function does nothing.
func (*DataTable) CaseInsensitive ¶
CaseInsensitive enables case-insensitive search for the DataTable.
This method sets the CaseInsensitive field in the DataTable's configuration to true, allowing the DataTable to perform case-insensitive search operations. After calling this method, any search queries executed by the DataTable will ignore the case of the search terms.
Returns the updated DataTable instance.
func (*DataTable) DisableOrder ¶
DisableOrder disables the ordering functionality for the DataTable.
This method sets the Orderable field in the DataTable's configuration to false, effectively disabling any ordering operations on the DataTable. After calling this method, the DataTable will not perform ordering on the data.
Returns the updated DataTable instance.
func (*DataTable) DisablePagination ¶
DisablePagination disables the pagination functionality for the DataTable.
This method sets the Paginate field in the DataTable's configuration to false, effectively disabling any pagination operations on the DataTable. After calling this method, the DataTable will not perform pagination on the data.
Returns the updated DataTable instance.
func (*DataTable) DisableSearch ¶
DisableSearch disables the search functionality for the DataTable.
This method sets the Searchable field in the DataTable's configuration to false, effectively disabling any search operations on the DataTable. After calling this method, the DataTable will not perform search filtering on the data.
Returns the updated DataTable instance.
func (*DataTable) EditColumn ¶
EditColumn edits the render function of a column with the given name.
If a column with the given name exists, the render function is replaced with a new one that calls the given editFunc with the value of the column from the given row. If the column does not exist, the function does nothing.
The RenderFunc field of the column is replaced with a new one, and the new column is stored in the columnsMap with the Data field as the key.
func (*DataTable) Filter ¶
Filter adds the specified filter function to the DataTable's filters slice.
This function allows the user to specify custom filtering logic that should be applied to the DataTable's query. The function takes a single argument, a function that takes a gorm.DB instance and returns a gorm.DB instance. The function should apply its desired filtering logic to the provided gorm.DB instance and return the updated instance.
Returns the updated DataTable instance.
func (*DataTable) FinalizeResponseColumns ¶
FinalizeResponseColumns removes any columns from the data that are not whitelisted or that are blacklisted.
func (*DataTable) Make ¶
Make processes the query and returns a DataTables compatible response.
It will execute the following steps:
- Validate the DataTable configuration.
- Execute the query and get the total records count, filtered records count and the actual data.
- Run the custom column rendering functions in parallel.
- Apply the row attributes in parallel.
- Apply the custom columns in parallel.
- If selected columns are defined, it will filter the columns for the response.
- Merge the additional data into the response.
- Return the response.
The function returns a DataTables compatible response or an error if it occurs.
func (*DataTable) Model ¶
Model sets the model to be used for the datatables request.
This can either be a struct or a string representing the table name.
If a struct is provided, it should have the same fields as the table being queried. If a string is provided, it should be the table name. The query will be executed on this table.
The model can also be set when creating a new DataTable with the New function.
func (*DataTable) Only ¶
Only sets the selectedColumns field of the DataTable to the specified columns.
This function allows the user to specify which columns should be included in the DataTable's operations. It takes one or more string arguments, representing the Data fields of the columns to be included. The selected columns will be used in subsequent operations, such as filtering and rendering the table. The function returns the updated DataTable instance.
func (*DataTable) Raw ¶
Raw returns the raw data retrieved from the database by executing the DataTable's query.
This function does not apply any custom column rendering functions or row attributes. It returns the raw data as retrieved from the database, along with any error that may have occurred.
func (*DataTable) RemoveColumn ¶
RemoveColumn removes one or more columns from the DataTable. The columns are removed from the selectedColumns and columns fields of the DataTable. If the selectedColumns field is empty, the columns are removed from the columns field. If the selectedColumns field is not empty, the columns are removed from the selectedColumns field and the columns field is updated accordingly.
The function takes one or more string arguments, which are the Data fields of the columns to be removed. If a column with one of the given Data fields exists, it is removed from the DataTable. If a column does not exist, the function does nothing.
func (*DataTable) Req ¶
Req sets the request parameters for the DataTable and adds the specified columns.
The request contains various configurations such as draw counter, pagination info, search terms, and column specifications. Each column specified in the request is added to the DataTable. The columns are set with their respective properties, including name, data, searchable, and orderable attributes.
Returns the updated DataTable instance.
func (*DataTable) SetConfig ¶
SetConfig sets the DataTable's configuration to the specified Config instance.
This method is a convenience method that can be used to set the DataTable's configuration after it has been created. The Config instance should contain the desired settings for the DataTable, such as whether or not to allow searching, ordering, and pagination. The Config instance should also contain the desired settings for the DataTable's request, such as the draw counter, start, and length.
Returns the updated DataTable instance.
func (*DataTable) SetFilteredRecords ¶
SetFilteredRecords sets the total number of records in the table that are visible after filtering.
This is a convenience method, and is used internally by the DataTable to store the total number of records returned by the filtered query.
Returns the updated DataTable instance.
func (*DataTable) SetRowAttributes ¶
func (dt *DataTable) SetRowAttributes(idFunc func(map[string]any) string, class string, dataFunc func(map[string]any) map[string]any) *DataTable
SetRowAttributes sets the row attributes of the DataTable.
This method is a convenience method that can be used to set the row attributes of the DataTable. The row attributes are used by the DataTable to generate the HTML for the table.
The idFunc parameter is a function that takes a row and returns the ID of the row. The class parameter is the class to be applied to the table row. The dataFunc parameter is a function that takes a row and returns a map of data to be added to the table row as data-* attributes.
Returns the updated DataTable instance.
func (*DataTable) SetTotalRecords ¶
SetTotalRecords sets the total number of records in the table.
This is a convenience method, and is used internally by the DataTable to store the total number of records returned by the count query.
Returns the updated DataTable instance.
func (*DataTable) SkipPaging ¶
SkipPaging disables the pagination functionality for the DataTable.
This is a convenience method, and is equivalent to calling DisablePagination. After calling this method, the DataTable will not perform pagination on the data.
Returns the updated DataTable instance.
func (*DataTable) Validate ¶
Validate checks the integrity of the DataTable configuration and request.
It ensures that either a model or a transaction (tx) with a valid gorm statement is provided. If a model is not explicitly set, it attempts to derive it from the gorm statement. The function also validates the request by checking the draw and columns parameters. If a regex search pattern is provided, it verifies that the pattern is valid. Returns an error if any of these validations fail, otherwise returns nil.
func (*DataTable) WhitelistColumn ¶
WhitelistColumn marks one or more columns as whitelisted. Only columns that are whitelisted will be included in the final response. If no columns are passed, this function does nothing.
func (*DataTable) With ¶
With appends the specified relations to the DataTable's relations slice.
This function allows the user to specify related models that should be included in the query. The relations are typically defined as strings representing the names of the related models. These relations will be processed during query execution to preload associated data.
Returns the updated DataTable instance.
func (*DataTable) WithData ¶
WithData adds a key-value pair to the DataTable's additional data map.
This function allows the user to specify arbitrary key-value pairs that should be included in the DataTable's response. The key should be a string representing the key of the value, and the value should be the value itself. The function returns the updated DataTable instance.
func (*DataTable) WithNumber ¶
WithNumber adds a column named "No" to the DataTable, which is non-searchable and non-orderable. The column is then blacklisted, meaning it will not be included in the final response. This function returns the updated DataTable instance.
type Order ¶
Order specifies the ordering criteria for a DataTable column.
Fields:
- Column: The index of the column to be ordered.
- Dir: The direction of ordering, either "asc" for ascending or "desc" for descending.
type Request ¶
type Request struct { Draw int `form:"draw"` Start int `form:"start"` Length int `form:"length"` Search Search `form:"search"` Order []Order `form:"order"` Columns []ColumnRequest `form:"columns"` }
Request represents a DataTables request.
Fields:
- Draw: The draw counter for this request.
- Start: The start position for this request.
- Length: The length of the request.
- Search: The search criteria for this request.
- Order: The ordering criteria for this request.
- Columns: The columns to be processed for this request.
func ParseRequest ¶
ParseRequest parses a DataTables request from the given http request.
It will automatically parse the draw, start, length, search, order, and columns parameters from the request. The request is validated and an error is returned if any part of the request is invalid.
The function returns the parsed request and nil if the request is valid, otherwise it returns nil and an error.