0

Using Grails 3.0.9 Making a Clear Form/session button for the filter class. however nothing i do works. The button is just stuck there, nothing happens

Any help will be appreciated

SupplyController

 def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        if (params.name)
            session.name = params.name
        else if (request.method != "POST")
            params.name = session.name
        else
            session.name = null

        def criteria = Supply.createCriteria()
        def query = {
            and{
                if (params.name) {
                        like ("name", '%' + params.name + '%')
                }
            }
        }
        def results = criteria.list(params, query)

        respond results, model:[supplyCount: results.getTotalCount()]
    }

G:field type of code

<div class="filter">
                <h3>Filter:</h3>
                <g:form action="index" method="post" >
                    <label for='name'>Name:</label> 
                      <input type="text" id="name" name="name" value="${session.name}"/><br/>
                      <span class="button">
                      <g:submitButton name="index" class="index" value="Apply Filter" /></span>
                      <g:field type="reset" name="myReset" value="Reset" />
                </g:form>
</div>

HTML tag type

<div class="filter">
                <h3>Filter:</h3>
                <g:form action="index" method="post" >
                    <label for='name'>Name:</label> 
                      <input type="text" id="name" name="name" value="${session.name}"/><br/>
                      <span class="button">
                      <g:submitButton name="index" class="index" value="Apply Filter" /></span>
                    <input type='reset' value='Reset' />
                </g:form>
</div>
Krizia Simon
  • 167
  • 1
  • 2
  • 11

2 Answers2

1

An HTML form reset button for a form rests the form to the initial state. In this case back to the values it had when the form loaded. A HTML form reset button does not CLEAR the form or CLEAR the session.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Did two methods, g:field didn't work either. As per http://sofb.developer-works.com/article/28045027/Grails%3A+How+to+create+a+clear+button%3F what method would do then? or am i missing something? – Krizia Simon Mar 15 '16 at 18:02
0

wondering why you are using session for where typicalls form params should be used? In order to clear HTML session you would need to trigger a call back to the originating controller just like you would to post but when it receives via this call. It does a session.invalidate() and then renders same view again. You should try to stick with params for forms. The fact that you have the session information already really no need to post it via a form. Your controller receiving the form would be aware of that same session value.

You could easily create a jquery functionality that you do with a button that triggers and a resetform. jQuery/Javascript function to clear all the fields of a form

Or if that does not work you could try for example in your case:

<g:form ..>
..
<button onClick="clearName();" name="clickme">
</g:form>

<g:javascript>
function clearName()  {
  $('#name').val();
}
</g:javascript>
Community
  • 1
  • 1
V H
  • 8,382
  • 2
  • 28
  • 48
  • shoot, sorry, i was editing the login logout portion of the code. Filter is made with createCriteria and params. got confused there. updating the question – Krizia Simon Mar 15 '16 at 18:30
  • @KriziaSimon maybe the javascript would be what you need? – V H Mar 15 '16 at 18:48