Fred A
Fred A

Reputation: 1764

Few questions about Grails' createCriteria

I read about createCriteria, and kind of interested on how these works, and its usability in providing values for dropdown box.

So say, i have a table in the database, Resource table, where i have defined the table in the domain class called Resource.groovy. Resource table has a total of 10 columns, where 5 of it are

So using the createCriteria, and i can use just like a query to return the items that i want to

def resList = Resource.createCriteria().list { 
        and { 
            eq('resource', resourceInstance)
            ne('materialId', '-')
        }
    }

Where in the above, i want to get the data that matches the resource = resourceInstance, and none of the materialId is equal to '-'.

I want to use the returned data from createCriteria above on my form, where i want to use some of the column on my select dropdown. Below is the code i used for my select dropdown.

<g:select id="resourceId" name="resourceId" 
                    from="${resList}"
                    disabled="${actionName != 'show' ? false : true}"  />

Thanks

Upvotes: 0

Views: 121

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

First of all refer to the document for using select in Grails. To answer all questions:

  • Yes, the list to select from in the dropdown can be customized. In this case it should be something like from="${resList*.productCode}"

  • Yes, this can be customized as well with something like

    from="${resList.collect { \"${it.resourceId} - ${it.resourceDesc}\" } }"

  • It depends. If there are associations involved in a domain then using Criteria will lead to eager fetches which might not be required. But with HQL one gets the flexibility of tailoring the query as needed. With latest version of Grails those boundries are minimized a lot. Usage of DetachedCriteria, where queries etc are recommended whereever possible. So it is kind of mixing and matching to the scenario under consideration.

Upvotes: 0

Related Questions