Announcement Announcement Module
Collapse
No announcement yet.
Accessing a list of params in controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing a list of params in controller

    Hello together,

    Im very new to grails (1.3.7) so please be patient :-)

    I have a gsp where I have various checkboxes. A user can click on them and then send his answer to a controller. The controller is receiving this request correctly.

    My problem is, that, for working with what the user chose, I have to check every parameter - to see if this checkbox was really checked. Thats really cumbersome and doesnt work very well, because the page displaying the checkboxes is dynamic - so the checkboxes which can be clicked are dynamic too. In my controller I dont know for which params I have to check then.

    Is there any possibility to receive a list of all checkboxes (or better: all checked checkboxes) in my controller? I researched but didnt find an answer!

    Thanks for answering! :-)

  • #2
    Ive got the answer:

    Code:
    params.name.each{i->
                System.out.println(i);
    }

    really very easy

    Comment


    • #3
      What does your GSP form look like? Detecting a checkbox on the server side is tricky and would make your code heavily dependent on implementation detail. I recommend you either use a naming convention for your checkbox tags and use that to distinguish between them:

      Code:
      for (p in params) {
          // If parameter name begins with 'checkBox', it's a checkbox field.
          if (p.key.startsWith("checkBox")) {
              ...
          }
      }
      Alternatively, add a hidden input field for each checkbox whose value contains the name of the checkbox element.

      Comment


      • #4
        thanks for your answer! why do you think that my solution is not good? I mean, I only get the checkboxes who were checked/clicked from the user, I dont think thats a bad idea? thank you :-)

        Comment


        • #5
          What does your GSP look like?

          Comment


          • #6
            Snippet:

            Code:
              <g:form action="startSimu" controller="lpicSimulator">
            
                <g:if test="${questionsList101 != null && !questionsList101.isEmpty()}">  Bereich 101:<br/>
                  <g:each in="${questionsList101}" var="elem" status="i">
                    <g:checkBox name="eins" value="${questionsList101[i].id}" checked="false"/>${i+1}<br/>
                  </g:each>
                  <br/>
                </g:if>
            
                <g:if test="${questionsList102 != null && !questionsList102.isEmpty()}">
                  Bereich 102:<br/>
                  <g:each in="${questionsList102}" var="elem" status="i">
                    <g:checkBox name="zwei" value="${questionsList102[i].id}" checked="false"/>${i+1}<br/>
                  </g:each>
                  <br/>
                </g:if>
            
                <g:if test="${questionsList103 != null && !questionsList103.isEmpty()}">
                  Bereich 103:<br/>
                  <g:each in="${questionsList103}" var="elem" status="i">
                    <g:checkBox name="drei" value="${questionsList103[i].id}" checked="false"/>${i+1}<br/>
                  </g:each>
                  <br/>
                </g:if>
            ......
            Code:
            params.eins.each{i-> cacheService.questionList.add(i); }
                    params.zwei.each{i-> cacheService.questionList.add(i); }
                    params.drei.each{i-> cacheService.questionList.add(i); }
            I think its very easy, because with the second snippet, I only get the checkboxes which were checked... Of course, Ive got to know the name of the checkbox-list, but with your solution this would be similiar, wouldnt it?

            thank you

            Comment


            • #7
              OK. I understood from your previous messages that you didn't know which input fields were checkboxes, but I see now that isn't the case. Thanks for clarifying.

              So, I only have two other things to mention. First, 'eins', 'zwei' etc. will only be lists if they contain 2 or more values. If only one value is submitted, your code may not work correctly. It's better to use the 'params.list()' method. Second, we now recommend you use for() rather than .each().

              Combining those together, we have:

              Code:
              for (field in ["eins", "zwei", "drei"]) {
                  for (i in params.list(field)) {
                      cacheService.questionList.add(i)
                  }
              }

              Comment


              • #8
                two minutes ago I noticed that my solution has got one error. I dont know why, but sometimes for checkbox "23" it added "2" and "3" instead of "23". But then I found your snippet here. I just changed my code and now everything works perfectly :-) At least I think so :-) Thank you!

                Comment

                Working...
                X