Search code examples
grails

Is there a way to filter the result of the static-scaffold's list method?


Stuck for too long trying to filter out some committee names in a select tag of a gsp file in a procurement system.

I'm trying to provide my clients with a feature that filters Committee(Domain) instances that are named 'MANAGEMENT'.

I've already tried:

${Committee.list().findAll{it.name != 'MANAGEMENT'}}

and also tried filtering the list in the controller, but i'm calling it like that: from="${Committee.list()}" in a select tag and i don't know how to override the default list method

<li>
    <g:select class="btn bg-info" dir="rtl" id="commDDLid" name="committeeDDL" action="filterByCommittee"
              controller="management" from="${Committee.list()}" optionKey="id" optionValue="${name}"
              value="${committees}" noSelection="${['null':'..']}"
              onchange="goToCommittee(this.value)"/>
</li>

CommitteeController is left untouched after static scaffolding. Committee Domain Class:

package attainrvtwo

class Committee {

    CommitteeOf name

    static hasMany = [summaries: Summary, users: User]
    static belongsTo = [department: Department]

    static constraints = {
        name()
        department()
        summaries(nullable: true)
    }

    @Override
    String toString() {
        return getName()
    }
}

CommitteeOf.groovy:

package attainrvtwo

enum CommitteeOf {
    EARLY_CHILDHOOD_EDUCATION,
    SOLDIERS,
    SPORT,
    MAINTENANCE,
    CULTURE,
    TRADITION,
    ENVIRONMENT_AND_COMMUNITY,
    STURDINESS,
    SPIRIT_AND_COMMUNITY,
    FIFTY_FIVE_PLUS,
    REGIONAL_PARTNERSHIP,
    DOGS,
    YOUTH,
    REVIEW,
    SECURITY,
    TZACHI,
    ARCHIVE,
    MANAGEMENT
}

I'm expecting the Committee.list() to return only committees which aren't named MANAGEMENT


Solution

  • Do you have to use list?

    How about findAllBy e.g.

    Committee.findAllByNameNotEqual( 'MANAGEMENT' )