This class adds sorting capability to some container.

Add data-uf-grid-sorting to a container element to add sorting of children support. The class assumes the children are organized in some sort of grid structure.

A container using sorting has two groups of children:

  • control elements, that can be clicked upon to determine which element to sort on.
  • sortable group of elements that will be reordered within their parent container.

Add data-uf-sort-ascending and data-uf-sort-descending attributes to the container to specify one or more css classes to add to the control element that has been selected to sort the data on. When one of the attributes is missing, no css classes will be set.

Add data-uf-storage-id to the container element to store the selected controller choice in the local storage and use it when the page with the container is shown again. The value of this attribute is used as key to store the data with.

Add data-uf-sort-control to child elements that are the control elements. The values of data-uf-sort-ascending and data-uf-sort-descending will be added to or removed from css classes of this element (even if there is a separate clickable child element). The value of this attribute can be one of the following:

  • none - to not sort the children
  • text - to sort the children as text
  • number - to sort the children as numbers
  • date - to sort the children as dates

Add data-uf-sort-button to a child element of a control element to specify the element that can be clicked upon to sort the children. This attribute is optional, when missing the header element itself will be used as clickable element.

Add data-uf-sort-key to specify an id for a control element. This attribute is optional, when missing the relative sibling index of the control element will be used. With relative indexes, the first control element has index 0, the second control element 1, etc. The key is used to link the data elements to the correct control element.

The control elements can have sibling elements in between that do not use any of the attributes. These will be ignored, they are also not used when determining the relative sibling index.

The class supports two different ways of sorting the children:

  • related sortable elements are placed in containers. The containers are reordered in the parent depending on the selected control element. For example a table row with table data entries.
  • related sortable elements are siblings. All elements are placed in the same parent. The class will reorder the elements in the parent keeping the siblings together.

To use containers, add data-uf-item-container to each container. The containers should not have other elements in between them. The class will reorder the containers in the parent.

To use siblings, either set data-uf-group-size with the container element or add data-uf-item-group to the sibling elements. If both attributes are missing, the number of siblings per group will be based on the number of controls.

With data-uf-group-size the children of the grid element (that are not using data-uf-grid-control, data-uf-item-container and data-uf-item-group) are split into groups using the value of data-uf-group-size. It is also possible to place the child elements in a separate container (that is a child element of the grid). Add data-uf-grid-body to the container element that contains the children.

With data-uf-item-group the value of the attribute determines which group the siblings belong to. Each group should use a unique value. When using data-uf-item-group make sure the sibling elements do not have any other elements in between them. When reordering only the elements using data-uf-item-group are reordered.

Adddata-uf-sort-key to a sortable element to link it to one of the controls. When missing the relative sibling index of the element will be used. With data-uf-item-group the index is relative to the first element with a certain group value.

By default, the class uses the innerText from the element to determine the value for. Add data-uf-sort-value to provide an alternative value to use when sorting.

When the elements are resorted because of a click on one of the controls, the class will dispatch an event "sorted" (SortedEvent) at the container element.

When elements are resorted (either the container elements or the grouped elements), the elements are reinserted at the first element inside the parent. Elements can have different parents; the elements with the same parent will be reordered within that parent starting at the position of the first element. This allows for data to be grouped and be sorted within their group.

Hierarchy (View Summary)

Constructors

Properties

SortedEvent: string = "sorted"

The event that is dispatched when the grid is resorted because the user clicked on a sort control.

Methods

  • Adds elements that have a specific attribute which value is a selector pointing to one or several elements; the target element(s). The elements are grouped per target element pointed to.

    A separate target list is used, so that multiple aTargetToSourceMap instances can be used that use the same target but point to different type of sources.

    The allows for a single event handler. The event handlers should handle all the different types of sources pointing to the same target.

    Type Parameters

    • TTarget extends HTMLElement
    • TSource extends HTMLElement

    Parameters

    • aSelectorAttribute: string

      The attribute that contains the selector. The sources are the elements containing this attribute. The targets are the elements pointed to by the selector value of the attribute.

    • aTargetList: TTarget[]

      Target elements are added to this list. If the target is already in the list, it will not be added again.

    • aTargetToSourceMap: UFMapOfSet<TTarget, TSource>

      A map that contains the source elements grouped per target element.

    • anEvent: null | string = null

      Optional event to listen for at the target element(s). It will be set only once at each target.

    • anHandler: null | (target: TTarget) => void = null

      A handler that is called with the target element that triggered the event.

    • aGroupName: string = ''

      Event group to use. If empty, the event listener just gets added to the target.

    Returns void

  • Gets the target element(s).

    Parameters

    • element: HTMLElement

      Element to get the target element(s) from.

    • target: string

      Either one of the predefined values or a selector.

    Returns HTMLElement[]

    list of elements (can be empty)

  • Resorts a grid using current selected control and sort direction. If the grid is not managed by this helper; nothing happens. This call will not fire a SortedEvent event.

    Parameters

    • grid: HTMLElement

      Grid to resort.

    Returns void