Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offset param not cleared when different sort field is selected #185

Open
nickdos opened this issue Apr 19, 2021 · 1 comment
Open

offset param not cleared when different sort field is selected #185

nickdos opened this issue Apr 19, 2021 · 1 comment
Labels

Comments

@nickdos
Copy link
Contributor

nickdos commented Apr 19, 2021

Related to #180 but slightly different. When the user clicks on a column heading to change the sort field, the results should be from page 1 (again). Currently the offset value is persisted between requests when sort field is changed.

E.g. view https://lists.ala.org.au/public/speciesLists, then click through to page 2 or 3. Now click a column heading to change the sort order to be for Date submitted. Resting page shows page 3 of the results for that sort field, which is inconsistent with most pagination implementations (both in the ALA and the web in general). It should always revert to the first page of results when the sort field or order is changed.

@nickdos nickdos added the bug label Apr 19, 2021
@Rita-C
Copy link
Contributor

Rita-C commented Apr 19, 2021

Sorting (heading) field is implemented by g:sortableColumn org.grails.plugins.web.taglib.UrlMappingTagLib

sortableColumn directly access request.params, ignoring passed in params, seems to be a defect in the plugin:

Closure sortableColumn = { Map 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 namespace = attrs.namespace ? attrs.remove("namespace") : ""

        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
        Map linkParams = [:]
        if (params.id) linkParams.put("id", params.id)
        def paramsAttr = attrs.remove("params")
        if (paramsAttr instanceof Map) linkParams.putAll(paramsAttr)
        linkParams.sort = property

        // propagate "max" and "offset" standard params
        if (params.max) linkParams.max = params.max
        if (params.offset) linkParams.offset = params.offset

        // 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'] as String) + " sorted " + order
            if (order == "asc") {
                linkParams.order = "desc"
            }
            else {
                linkParams.order = "asc"
            }
        }
        else {
            linkParams.order = defaultOrder
        }

        // determine column title
        String title = attrs.remove("title") as String
        String titleKey = attrs.remove("titleKey") as String
        Object mapping = attrs.remove('mapping')
        if (titleKey) {
            if (!title) title = titleKey
            def messageSource = grailsAttributes.messageSource
            def locale = RequestContextUtils.getLocale(request)
            title = messageSource.getMessage(titleKey, null, title, locale)
        }

        writer << "<th "
        // process remaining attributes
        Encoder htmlEncoder = codecLookup.lookupEncoder('HTML')
        attrs.each { k, v ->
            writer << k
            writer << "=\""
            writer << htmlEncoder.encode(v)
            writer << "\" "
        }
        writer << '>'
        Map linkAttrs = [:]
        linkAttrs.params = linkParams
        if (mapping) {
            linkAttrs.mapping = mapping
        }

        linkAttrs.action = action
        linkAttrs.namespace = namespace

        writer << callLink((Map)linkAttrs) {
            title
        }
        writer << '</th>'
    }

The following code does NOT work:

<g:set var="paramters" value="${params.findAll {it.key != 'offset'}} " />

<g:sortableColumn property="listName" params="${paramters}"
                  title="${message(code: 'speciesList.listName.label', default: 'List Name')}"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants