Skip to content
Tim Curzon edited this page Feb 28, 2022 · 2 revisions

Ztal Table Column

Ztal\Table\Column\Abstract is an abstract class representing a single column in a table. Ztal\Table\Column\Array and Ztal\Table\Column\Object are concrete subclasses that manage array-based columns and object-based columns respectively. The documentation here is for the Abstract class because neither of the subclasses provide additional functionality.

Instances of the column classes are usually created while building a table and within the Ztal\Table\Abstract's _init method. However, they may be created and appended to a table instance at any time.

If the data that needs to be tabulated is not in the array or object formats expected, subclassing one of the supplied classes is a recommended option. The majority of the functionality within the Abstract class is very generic so subclassing should only require overriding 2 methods, as described below and as demonstrated in the concrete classed for arrays and objects.


Constants

  • DIRECTION_ASCENDING - defines a sort in the ascending direction (1 -> 10)
  • DIRECTION_DESCENDING - defines a sort in the descending direction (10 -> 1)

__construct (string, string{null}, array{array()}) : void

The constructor accepts one required and two optional arguments:

  • The first argument is the keyName of the column and is how the column is referred to during use.
  • The second is the name of the data source for the column. For the Array subclass it is the array key, for the Object subclass it is the name of the getter method to call.
  • The third is an array of configuration options.

The Options available to be passed in the options array are:

  • sortField
  • defaultSortDirection
  • formatter

sortField and defaultSortDirection work together to specify that the column is sortable and which direction the table should be sorted in unless the user specifies differently. For arrays, the sortField value is the key to a field in the array (and it need not be the same as the field providing the data to the column) while for objects it is the name of a sort method.

The formatter option can be used to pass a method or closure that will be used to process the output from getColumnDataForSource before it is then rendered in the table. This is useful if, for instance, you wish to process the output from Zend_Date rather than relying on its __string method or if you wish to format a float to a specific number of decimal places.


_sortDataSource (&mixed, string, int) : void : throws Exception

A protected method that actually performs the sort on a data source.

This method should be overridden to manage sorting on custom data sources. The first parameter is the data source to sort, the second the sort field and the third the sort direction.

The Abstract class throws an Exception if this method is called.


_dataForKey (mixed, string) : mixed : throws Exception

A protected method that returns the value for a specified key in the supplied data source.

This method should be overridden to manage supplying values from a custom data source. The first parameter is the data source and the second is the key for which a value is requested. Any formatter specified for a column is applied to the result of a call to this method.

The Abstract class throws an Exception if this method is called.


getColumnDataForSource (mixed) : string

Given a data source, this method returns a string (or a value that can be cast to a string) for rendering in the table output. If a formatter has been specified in the constructor options then formatting is applied before the value is returned.


getColumnKey () : string

Returns the column's nameKey


isSortable () : boolean

Returns whether the column is sortable (i.e. a sortField option was specified in the constructor options).


getSortField () : string|null

If the column is sortable this method returns the name of the field / method used in sorting.


getSortDirection() : int

Returns the current sort direction for the column.


setSortDirection (int) : void

Sets the sort direction for the column. The DIRECTION_ constants should be used for clarity of code.


sort (&mixed) : void

Sort the supplied data source using the previously configured sort field and sort direction. Note that this method WILL modify the supplied data source (by sorting it).