bharathi
bharathi

Reputation: 6271

How to use g:select within taglib in grails

Can anyone help me how to use the grails tags like g:select,g:label while creating a taglib functionality.

Following is my code.

I have create a taglib functionality

 def dynamicAdvancedSearchTagLib={attrs, body ->
    String beanName = attrs.remove("beanName")
    def bean = request.getAttribute(beanName)
    out << createAdvancedSearchUI(bean)

}
def createAdvancedSearchUI(bean){
    StringBuilder sb = new StringBuilder();

    sb<<"""<fieldset class="search-advanced collapsed"style="border: 1px solid #CCC;padding:0px 21px 0px 21px;border-radius:8px 8px 8px 8px;min-height:24px;">
    <legend>
        <span id="advancedsearch"> <label for="advancedsearch">Advanced Search
        </label>
        </span>
    </legend>
    <g:form method="post">
    <div id="searchcontent">
        <div class="advanced-content-search">
            <label for="Criteria Name">Criteria Name <span
                class="required-indicator">*</span>
            </label>
            <select id="searchCriteria" name="searchCriteria" required="">
            *****<option value="0" selected="selected">Choose...</option>
            <option value="1">Something</option>
            <option value="2">Something else</option>
            <option value="3">Another choice</option>**
            </select>***
        </div>
        <div style="margin-left: 338px;">
            <label for="Enter Your Search Here"> Enter Your Search Here <span
                class="required-indicator">*</span>
            </label>
            <input type="text" name="advancedSearchText"
                value="" required="" />
        </div>

    </div>
    </g:form>
</fieldset>"""
}

Here my requirement instead of using the select html tag i need to use the g:select.

I have a static gsp page i have used g:select as

    <g:select id="searchCriteria" name="searchCriteria"
                        from="${SearchCriteria?.values()}"  value="${searchInstance.searchCriteria}"
                        noSelection="${['':'---Select any One---']}" required="" />

The same functionality should has to be used while creating the taglib functionality..

If anyone knows how to use the grails tags while createing taglib, please help me.

Upvotes: 3

Views: 2774

Answers (1)

Ankur
Ankur

Reputation: 12774

You need to use something like this

def attrs=["id":"searchCriteria","name":"searchCriteria"]//fill other values
def formTagLib = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.FormTagLib')
formTagLib.select.call(attrs)

Here is a tutorial for the above that I found in Overwrite and reuse section

As an alternative you can also use

out << g.select(attrs) 

Upvotes: 3

Related Questions