Horizon includes a componentized API for programmatically creating tables in the UI. Why would you want this? It means that every table renders correctly and consistently, table- and row-level actions all have a consistent API and appearance, and generally you don’t have to reinvent the wheel or copy-and-paste every time you need a new table!
The core class which defines the high-level structure of the table being represented. Example:
class MyTable(DataTable):
name = Column('name')
email = Column('email')
class Meta:
name = "my_table"
table_actions = (MyAction, MyOtherAction)
row_actions - (MyAction)
A full reference is included below:
alias of get_row_actions
The following options can be defined in a Meta class inside a DataTable class. Example:
class MyTable(DataTable):
class Meta:
name = "my_table"
verbose_name = "My Table"
Contains options for DataTable objects.
A short name or slug for the table.
A more verbose name for the table meant for display purposes.
A list of column objects or column names. Controls ordering/display of the columns in the table.
A list of action classes derived from the Action class. These actions will handle tasks such as bulk deletion, etc. for multiple objects at once.
A list similar to table_actions except tailored to appear for each row. These actions act on a single object at a time.
Boolean value to control rendering of an additional column containing the various actions for each row. Defaults to True if any actions are specified in the row_actions option.
Boolean value to control rendering of an extra column with checkboxes for selecting multiple objects in the table. Defaults to True if any actions are specified in the table_actions option.
Boolean value to control the display of the “filter” search box in the table actions. By default it checks whether or not an instance of FilterAction is in table_actions.
String containing the template which should be used to render the table. Defaults to "horizon/common/_data_table.html".
The name of the context variable which will contain the table when it is rendered. Defaults to "table".
A list or tuple of column names which represents the “state” of the data object being represented.
If status_columns is set, when the rows are rendered the value of this column will be used to add an extra class to the row in the form of "status_up" or "status_down" for that row’s data.
The row status is used by other Horizon components to trigger tasks such as dynamic AJAX updating.
A class which represents a single column in a DataTable.
A string or callable. If transform is a string, it should be the name of the attribute on the underlying data class which should be displayed in this column. If it is a callable, it will be passed the current row’s data at render-time and should return the contents of the cell. Required.
The name for this column which should be used for display purposes. Defaults to the value of transform with the first letter of each word capitalized.
Boolean to determine whether this column should be sortable or not. Defaults to False.
Boolean to determine whether or not this column should be displayed when rendering the table. Default: False.
A string or callable which returns a URL which will be wrapped around this column’s text as a link.
Boolean designating whether or not this column represents a status (i.e. “enabled/disabled”, “up/down”, “active/inactive”). Default: False.
A tuple of tuples representing the possible data values for the status column and their associated boolean equivalent. Positive states should equate to True, negative states should equate to False, and indeterminate states should be None.
Values are compared in a case-insensitive manner.
Example (these are also the default values):
status_choices = (
('enabled', True),
('true', True)
('up', True),
('active', True),
('on', True),
('none', None),
('unknown', None),
('', None),
('disabled', False),
('down', False),
('false', False),
('inactive', False),
('off', False),
)
A string to be used for cells which have no data. Defaults to an empty string.
A list of functions (often template filters) to be applied to the value of the data for this column prior to output. This is effectively a shortcut for writing a custom transform function in simple cases.
An iterable of CSS classes which should be added to this column. Example: classes=('foo', 'bar').
A dict of HTML attribute strings which should be added to this column. Example: attrs={"data-foo": "bar"}.
Returns the appropriate data for this column from the given input.
The return value will be either the attribute specified for this column or the return value of the attr:~horizon.tables.Column.transform method for this column.
Returns the final value for the column’s link property.
If link is a callable, it will be passed the current data object and should return a URL. Otherwise get_link_url will attempt to call reverse on link with the object’s id as a parameter. Failing that, it will simply return the value of link.
Represents a row in the table.
When iterated, the Row instance will yield each of its cells.
Rows are capable of AJAX updating, with a little added work:
The ajax property needs to be set to True, and subclasses need to define a get_data method which returns a data object appropriate for consumption by the table (effectively the “get” lookup versus the table’s “list” lookup).
The automatic update interval is configurable by setting the key ajax_poll_interval in the settings.HORIZON_CONFIG dictionary. Default: 2500 (measured in milliseconds).
The table which this row belongs to.
The data object which this row represents.
A string uniquely representing this row composed of the table name and the row data object’s identifier.
The cells belonging to this row stored in a SortedDict object. This attribute is populated during instantiation.
Boolean value representing the status of this row calculated from the values of the table’s status_columns if they are set.
Returns a css class for the status of the row based on status.
Boolean value to determine whether ajax updating for this row is enabled.
String that is used for the query parameter key to request AJAX updates. Generally you won’t need to change this value. Default: "row_update".
Returns the bound cells for this row in order.
Fetches the updated data for the row based on the object id passed in. Must be implemented by a subclass to allow AJAX updating.
Load the row’s data (either provided at initialization or as an argument to this function), initiailize all the cells contained by this row, and set the appropriate row properties which require the row’s data to be determined.
This function is called automatically by __init__() if the datum argument is provided. However, by not providing the data during initialization this function allows for the possibility of a two-step loading pattern when you need a row instance but don’t yet have the data available.
Represents an action which can be taken on this table’s data.
Required. The short name or “slug” representing this action. This name should not be changed at runtime.
A descriptive name used for display purposes. Defaults to the value of name with the first letter of each word capitalized.
Used like verbose_name in cases where handles_multiple is True. Defaults to verbose_name with the letter “s” appended.
The HTTP method for this action. Defaults to POST. Other methods may or may not succeed currently.
Boolean value indicating whether or not this action can be taken without any additional input (e.g. an object id). Defaults to True.
Boolean value indicating whether this action should be evaluated in the period after the table is instantiated but before the data has been loaded.
This can allow actions which don’t need access to the full table data to bypass any API calls and processing which would otherwise be required to load the table.
At least one of the following methods must be defined:
Handler for a single-object action.
Handler for multi-object actions.
If a single function can work for both single-object and multi-object cases then simply providing a handle function will internally route both single and multiple requests to handle with the calls from single being transformed into a list containing only the single object id.
Returns the full POST parameter name for this action.
Defaults to {{ table.name }}__{{ action.name }}.
A table action which is simply a link rather than a form POST.
Required. The short name or “slug” representing this action. This name should not be changed at runtime.
A string which will be rendered as the link text. (Required)
A string or a callable which resolves to a url to be used as the link target. You must either define the url attribute or a override the get_link_url method on the class.
Returns the final URL based on the value of url.
If url is callable it will call the function. If not, it will then try to call reverse on url. Failing that, it will simply return the value of url as-is.
When called for a row action, the current row data object will be passed as the first parameter.
A base class representing a filter action for a table.
The short name or “slug” representing this action. Defaults to "filter".
A descriptive name used for display purposes. Defaults to the value of name with the first letter of each word capitalized.
A string representing the name of the request parameter used for the search term. Default: "q".
Provides the actual filtering logic.
This method must be overridden by subclasses and return the filtered data.
Returns the full query parameter name for this action.
Defaults to {{ table.name }}__{{ action.name }}__{{ action.param_name }}.
An internal name for this action.
String or tuple/list. The display forms of the name. Should be a transitive verb, capitalized and translated. (“Delete”, “Rotate”, etc.) If tuple or list - then setting self.current_present_action = n will set the current active item from the list(action_present[n])
String or tuple/list. The past tense of action_present. (“Deleted”, “Rotated”, etc.) If tuple or list - then setting self.current_past_action = n will set the current active item from the list(action_past[n])
A display name for the type of data that receives the action. (“Keypair”, “Floating IP”, etc.)
Optional plural word for the type of data being acted on. Defaults to appending ‘s’. Relying on the default is bad for translations and should not be done.
Optional location to redirect after completion of the delete action. Defaults to the current page.
Required. Accepts a single object id and performs the specific action.
Return values are discarded, errors raised are caught and logged.
Returns the URL to redirect to after a successful action.
Switches the action verbose name, if needed
Several class-based views are provided to make working with DataTables easier in your UI.
A class-based generic view to handle basic DataTable processing.
Three steps are required to use this view: set the table_class attribute with the desired DataTable class; define a get_data method which returns a set of data for the table; and specify a template for the template_name attribute.
Optionally, you can override the has_more_data method to trigger pagination handling for APIs that support it.
A class-based generic view to handle the display and processing of multiple DataTable classes in a single view.
Three steps are required to use this view: set the table_classes attribute with a tuple of the desired DataTable classes; define a get_{{ table_name }}_data method for each table class which returns a set of data for that table; and specify a template for the template_name attribute.