Quantcast

How to Populate g:select With a Query

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to Populate g:select With a Query

rhutchins
This post was updated on .
I have a g:select tag that I am currently populating with all of the entries from a table like so:

<g:select name="prefix" from="${com.tcr.Vehicle_Make.list()}" optionKey="id" optionValue="make"  noSelection="['':'-Choose make-']"/>

And that works just fine.

However, I would like to, instead, populate this same g:select tag with only a subset of the items from the Vehicle_Make (as shown in the above code snippet) table. Think of it this way...if the Vehicle_Make table stores and the drop-down shows all vehicle makes in the world, but the user only wants to see "domestic" makes, then, instead of showing all of the makes in the table, the drop-down would only show makes such as Chevrolet, Ford, Chrysler, etc.

I'm assuming I'd pull this off with some form of query, but don't know where to begin piecing it together. Is it possible within the from="" property of the g:select tag or should it be done as a query in the controller and passed to the g:select tag?

Hope that makes sense. Any assistance is appreciated.

Thanks in advance.
Rich
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to Populate g:select With a Query

rhutchins
This post has NOT been accepted by the mailing list yet.
Just to close this loop for anybody stumbling on this thread...

I figured out how to pull this off. In my case, I created a Service that has the database query that fetches the items I want in my drop-down list. For example:

   def getSelectListItems(String selectListName, Boolean selectListItemActiveStatus, String selectListSort) {
       
                def qryOrder
                               
                if ( selectListSort.toUpperCase()=="ASC" ) {
                                qryOrder = " order by explicitRelativeOrder ASC"
                        } else if ( selectListSort.toUpperCase()=="DESC" ) {
                                qryOrder = " order by explicitRelativeOrder DESC"
                        } else {
                                qryOrder = ""
                        }
               
                def itemList = SelectListItems.findAll("from SelectListItems as a where a.parentListName = ? and a.isActive = ?" + qryOrder, [selectListName, selectListItemActiveStatus])
               
                return itemList

    }

And I inject that Service into a Controller thusly:
        //injects the ManageSelectListItemsService into this controller
        def manageSelectListItemsService

Inside the Controller, I access my Service's query functionality like this (there are three different select lists I'm managing with the Controller, thus the three calls):
   def create = {
        def accountInstance = new Account()
        accountInstance.properties = params
               
                //replaced by the code under the "revised to use service" comment
                //return [accountInstance: accountInstance]
               
                //revised to use service
                def listItemsSecurityQuestions = manageSelectListItemsService.getSelectListItems("SecurityQuestions", true, "desc")
                def listItemsAccountStatus = manageSelectListItemsService.getSelectListItems("AccountStatus", true, "none")
                def listItemsAccountCloseReason = manageSelectListItemsService.getSelectListItems("AccountCloseReason", true, "none")
                return [accountInstance: accountInstance, listItemsSecurityQuestions: listItemsSecurityQuestions, listItemsAccountStatus: listItemsAccountStatus, listItemsAccountCloseReason: listItemsAccountCloseReason]
               
    }

As you can see above, I'm passing the variables populated with their respective lists to the View with the Return statement.

And in the View, I am able to access the list items like this:
                                    <g:select name="securityQuestion" from="${listItemsSecurityQuestions}" optionKey="id" optionValue="longDesc"  noSelection="['':'-Choose Security Question-']"/>

You can see that the from="${listItemsSecurityQuestions}" matches up with one of the named items in the Return statement from my controller.

I hope this helps out anybody who gets stuck on this like I did. I'm not saying this is the best, most efficient way, but it does work.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to Populate g:select With a Query

rhutchins
Just to close this loop for anybody stumbling on this thread...

I figured out how to pull this off. In my case, I created a Service that has the database query that fetches the items I want in my drop-down list. For example:

   def getSelectListItems(String selectListName, Boolean selectListItemActiveStatus, String selectListSort) {
       
                def qryOrder
                               
                if ( selectListSort.toUpperCase()=="ASC" ) {
                                qryOrder = " order by explicitRelativeOrder ASC"
                        } else if ( selectListSort.toUpperCase()=="DESC" ) {
                                qryOrder = " order by explicitRelativeOrder DESC"
                        } else {
                                qryOrder = ""
                        }
               
                def itemList = SelectListItems.findAll("from SelectListItems as a where a.parentListName = ? and a.isActive = ?" + qryOrder, [selectListName, selectListItemActiveStatus])
               
                return itemList

    }

And I inject that Service into a Controller thusly:
        //injects the ManageSelectListItemsService into this controller
        def manageSelectListItemsService

Inside the Controller, I access my Service's query functionality like this (there are three different select lists I'm managing with the Controller, thus the three calls):
   def create = {
        def accountInstance = new Account()
        accountInstance.properties = params
               
                //replaced by the code under the "revised to use service" comment
                //return [accountInstance: accountInstance]
               
                //revised to use service
                def listItemsSecurityQuestions = manageSelectListItemsService.getSelectListItems("SecurityQuestions", true, "desc")
                def listItemsAccountStatus = manageSelectListItemsService.getSelectListItems("AccountStatus", true, "none")
                def listItemsAccountCloseReason = manageSelectListItemsService.getSelectListItems("AccountCloseReason", true, "none")
                return [accountInstance: accountInstance, listItemsSecurityQuestions: listItemsSecurityQuestions, listItemsAccountStatus: listItemsAccountStatus, listItemsAccountCloseReason: listItemsAccountCloseReason]
               
    }

As you can see above, I'm passing the variables populated with their respective lists to the View with the Return statement.

And in the View, I am able to access the list items like this:
                                    <g:select name="securityQuestion" from="${listItemsSecurityQuestions}" optionKey="id" optionValue="longDesc"  noSelection="['':'-Choose Security Question-']"/> 

You can see that the from="${listItemsSecurityQuestions}" matches up with one of the named items in the Return statement from my controller.

I hope this helps out anybody who gets stuck on this like I did. I'm not saying this is the best, most efficient way, but it does work.
Loading...