Grails Scaffolding Plugin
  1. Grails Scaffolding Plugin
  2. GPSCAFFOLD-29

Support for Sorting (Comparable) in default scaffolding

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      None
    • Patch Submitted:
      Yes

      Description

      Currently if you have a 1-1 or 1-many association, the default scaffolding renders a <select> with the appropriate domain objects in it. But they are always sorted by id, and that is often not what you want. I would suggest that, for example, if there is a toString() representation you would probably want to see them sorted alphabetically by toString() value.

      Obvious candidate is to use Comparable. If the domain implements Comparable, you can easily sort it before rendering.

      i.e. change renderEditor.template.renderManyToOne from

      private renderManyToOne(domainClass,property) {
      if(property.association) {
      return "<g:select optionKey=\"id\" from=\"\${$

      {property.type.name}.list()}\" name=\"${property.name}.id\" value=\"\${${domainClass.propertyName}?.${property.name}?.id}\" ${renderNoSelection(property)}></g:select>"
      }
      }

      to

      private renderManyToOne(domainClass,property) {
      if(property.association) {
      if(Comparable.isAssignableFrom(property.type))
      {
      return "<g:select optionKey=\"id\" from=\"\${${property.type.name}

      .list().sort{a,b->(a<b ? -1 : 1)}}\" name=\"$

      {property.name}.id\" value=\"\${${domainClass.propertyName}?.${property.name}

      ?.id}\" $

      {renderNoSelection(property)}></g:select>"
      }
      else
      {
      return "<g:select optionKey=\"id\" from=\"\${${property.type.name}.list()}\" name=\"${property.name}.id\" value=\"\${${domainClass.propertyName}?.${property.name}?.id}\" ${renderNoSelection(property)}

      ></g:select>"
      }
      }
      }

      i.e. add the .sort() if domain class is Comparable

        Activity

        Hide
        Jason Morris added a comment -

        Ahem...that should of course be more like

        .sort

        {a,b->a.comparesTo(b)}

        i.e. actually use the Comparable implementation that the user supplies directly.

        D'oh!

        Show
        Jason Morris added a comment - Ahem...that should of course be more like .sort {a,b->a.comparesTo(b)} i.e. actually use the Comparable implementation that the user supplies directly. D'oh!
        Hide
        Jason Morris added a comment -

        Actually with hindsight it would probably be more efficient to pass sort/order params to the .list() method but that is harder (and less intuitive) for the user to control.

        Show
        Jason Morris added a comment - Actually with hindsight it would probably be more efficient to pass sort/order params to the .list() method but that is harder (and less intuitive) for the user to control.
        Hide
        Graeme Rocher added a comment -

        Moving non-critical issues that aren't going to make it into 1.1 to 1.2

        Show
        Graeme Rocher added a comment - Moving non-critical issues that aren't going to make it into 1.1 to 1.2
        Hide
        Graeme Rocher added a comment -

        There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3

        for 1.2 final only issues considered blocking will now be fixed

        Show
        Graeme Rocher added a comment - There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3 for 1.2 final only issues considered blocking will now be fixed

          People

          • Assignee:
            Unassigned
            Reporter:
            Jason Morris
          • Votes:
            3 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: