Grails chained drop down

2019-08-13 22:39发布

问题:

I'm trying to implement chained drop down boxes using the tutorial here. My classes are not as straight forward as the ones in the tutorial though.

I want to chain the drop down boxes for the create.gsp view in the Load class. Each load belongs to an account from the Account class, and each account belongs to a user from the User class, and each user has several cargo destinations from the Address class.

My goal is to have the cargo destination field up date based on which account is selected.

I am having trouble understanding the AJAX function in the tutorial (step 3), and how it relates to the Grails function (step 4).

Here is the AJAX code:

     function respondToSelect(event)
 {
       new Ajax.Updater("memberSelect",
          "/chainedSelect/family/updateSelect",
          {method:'get', parameters: {selectedValue : $F("familySelect")} }
         );
 }

Here is the Grails method:

    def updateSelect = {

     def familySelected = Family.find("from Family as family where family.surname=:surname", [surname:params.selectedValue])

render (template:"selectMember", model : ['familySelected' : familySelected])

}

If someone could just explain what the third parameter of the AJAX function is doing I think I can figure the Grails part out.

{method:'get', parameters: {selectedValue : $F("account")}}

回答1:

If someone could just explain what the third parameter of the AJAX function is doing

The third argument is an object of parameters that get passed to the Updater that tell it how to make the HTTP request to the server.

Make the request an HTTP GET request:

method:'get'

Pass the following named query parameters:

{selectedValue: $F("account")}

$F is a prototype shortcut to retrieve the value of an element. In this case, it's getting the selected value of the DOM element with id account.

This ultimately results in something like the following request:

GET /chainedSelect/family/updateSelect?selectedValue=someValue

Where "someValue" is the currently-selected item in the "account" select list.