Matt Westlake
Matt Westlake

Reputation: 3651

Grails radio for Types

I have a Grails app that I'm trying to do a search based on different fields on an object. I want to implement a search field with radio buttons to determine which fields get searched.

Example: Input box has "ABC" if box 1 is selected (search title) if box 2 is selected (search author) if box 1 & 2 are selected (search both title and author and return the item if either match)

so it would look like:

if box 1 (entries = entry.findAllByTitleLike("ABC")) if box 2 (entries = entry.findAllByAuthorLike("ABC"))

once I get entry I'm ok outputting it, just need to understand how to do multi-selects of radio buttons.

Upvotes: 0

Views: 115

Answers (2)

Bryan Cox
Bryan Cox

Reputation: 288

if (box1 && box2) {
   entries = entry.findAllByTitleLikeOrAuthorLike("ABC", "ABC")
}
else if (box1) {
   entries = entry.findAllByTitleLike("ABC")
}
else if (box2) {
   entries = entry.findAllByAuthorLike("ABC")
}
else {
   entries = []
}

Upvotes: 0

Victor Sergienko
Victor Sergienko

Reputation: 13495

def entries = entryClass.withCriteria {
  if (box1) {
    like 'title', "ABC"
  }
  if (box2) {
    like 'author', "CDE"
  }
}

Upvotes: 1

Related Questions