# sortableColumn

## Purpose

Renders a sortable column to support sorting in tables.

## Examples

    <g:sortableColumn property="title" title="Title" />
    <g:sortableColumn property="title" title="Title" style="width: 200px" />
    <g:sortableColumn property="title" titleKey="book.title" />
    <g:sortableColumn property="releaseDate" defaultOrder="desc" title="Release Date" />
    <g:sortableColumn property="releaseDate" defaultOrder="desc" title="Release Date" titleKey="book.releaseDate" />

## Description

Attribute `title` or `titleKey` is required. When both attributes are specified then `titleKey` takes precedence, resulting in the title caption to be resolved against the message source. In case when the message could not be resolved, the title will be used as title caption.

Attributes

- `property` - name of the property relating to the field
- `defaultOrder` (optional) - default order for the property; choose between asc (default if not provided) and desc
- `title` (optional) - title caption for the column
- `titleKey` (optional) - title key to use for the column, resolved against the message source
- `params` (optional) - a map containing request parameters
- `action` (optional) - the name of the action to use in the link, if not specified the list action will be linked

## Source

Show Source

    def sortableColumn = { attrs ->
            def writer = out
            if (!attrs.property) {
                throwTagError("Tag [sortableColumn] is missing required attribute [property]")
            }        if (!attrs.title && !attrs.titleKey) {
                throwTagError("Tag [sortableColumn] is missing required attribute [title] or [titleKey]")
            }        def property = attrs.remove("property")
            def action = attrs.action ? attrs.remove("action") : (actionName ?: "list")        def defaultOrder = attrs.remove("defaultOrder")
            if (defaultOrder != "desc") defaultOrder = "asc"        // current sorting property and order
            def sort = params.sort
            def order = params.order        // add sorting property and params to link params
            def linkParams = [:]
            if (params.id) linkParams.put("id",params.id)
            if (attrs.params) linkParams.putAll(attrs.remove("params"))
            linkParams.sort = property        // determine and add sorting order for this column to link params
            attrs.class = (attrs.class ? "${attrs.class} sortable" : "sortable")
            if (property == sort) {
                attrs.class = attrs.class + " sorted " + order
                if (order == "asc") {
                    linkParams.order = "desc"
                }
                else {
                    linkParams.order = "asc"
                }
            }
            else {
                linkParams.order = defaultOrder
            }        // determine column title
            def title = attrs.remove("title")
            def titleKey = attrs.remove("titleKey")
            if (titleKey) {
                if (!title) title = titleKey
                def messageSource = grailsAttributes.messageSource
                def locale = RCU.getLocale(request)
                title = messageSource.getMessage(titleKey, null, title, locale)
            }        writer << "<th "
            // process remaining attributes
            attrs.each { k, v ->
                writer << "${k}=\"${v.encodeAsHTML()}\" "
            }
            writer << ">${link(action:action, params:linkParams) { title }}</th>"
        }