Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Sort

Sort represents information relevant to sorting.

When data needs to be sorted according to one or several attributes, we can use Sort to represent the sorting information and generate appropriate hyperlinks that can lead to sort actions.

A typical usage example is as follows,

async sampleRouteHandler(request, response, next)
{
let sort = new Sort({
attributes': {
age',
name': {
asc': { first_name: 'asc', last_name: 'asc' },
desc': { first_name: 'desc', last_name: 'desc' },
default': 'desc',
label': 'Name',
},
},
});

let models = Post::findAll({ where: { status: 1 }, order: sort.getOrders()})

return response.render('/index.ejs', { models, sort, sorter });
}

View:

// display links leading to sort actions
<%- sort.link('name') + ' | ' + sort.link('age') %>

fore(let model of models) {
// display model here
}

In the above, we declare two attributes that support sorting: name and age. We pass the sort information to the Post query so that the query results are sorted by the orders specified by the Sort object. In the view, we show two hyperlinks that can lead to pages with the data sorted by the corresponding attributes.

For more details and usage information on Sort, see the guide article on sorting.

property

attributeOrders Sort directions indexed by attribute names. Sort direction can be either SORT_ASC for ascending order or SORT_DESC for descending order. Note that the type of this property differs in getter and setter. See getAttributeOrders and [[setAttributeOrders]] for details.

property-read

orders The columns (keys) and their corresponding sort directions (values). This can be passed to the 'order by' clause of the DB query. This property is read-only.

author

Mahesh S Warrier https://github.com/codespede

Hierarchy

Index

Constructors

  • new Sort(config: {}): Sort

Properties

_attributeOrders: any[]
attributes: any = {}
defaultOrder: any[] = []
enableMultiSort: boolean = false
initialization: Promise<void>

A Promise which resolves when this Component has been successfully initialized.

params: {}

Type declaration

  • [key: string]: any
separator: string = ','
sortFlags: string = 'SORT_REGULAR'
sortParam: string = 'sort'

Methods

  • createSortParam(attribute: string): string
  • createUrl(attribute: string, absolute?: boolean): URL
  • Parameters

    • attribute: string
    • absolute: boolean = false

    Returns URL

  • getAttributeOrder(attribute: string): string
  • getAttributeOrders(recalculate?: boolean): any[]
  • getOrders(recalculate?: boolean): any[]
  • Parameters

    • recalculate: boolean = false

    Returns any[]

  • hasAttribute(name: string): boolean
  • init(): Promise<void>
  • Initializes the object. This method is invoked at the end of the constructor after the object is initialized with the given configuration.

    Returns Promise<void>

  • link(attribute: string, options: {}): string
  • Parameters

    • attribute: string
    • options: {}
      • [key: string]: any

    Returns string

  • parseSortParam(param: string): string[]

Generated using TypeDoc