Thomas Buckley
Thomas Buckley

Reputation: 6046

Pass domain list to view with specific element removed

I'm using grails 1.3.7.

I have the following Domain:

class Category {

    String name;
    String categoryKey;

    Date dateCreated
    Date lastUpdated

    static constraints = {
        name(blank: false, nullable: false, maxSize:30)
        categoryKey(blank: false, nullable: false, maxSize:30)
    }

    String toString()
    {
        return name
    }
}

I display the list of categories in gsp as follows:

 <g:select class="fields" valueMessagePrefix="shared.category.label" name='categoryKey'
              value="${dealInstance?.category?.categoryKey}"
              noSelection="${['': message(code: 'layouts.main.filter.select', default: '(Please select one)')]}"
              from='${categoryList.list()}' optionValue="name"
              optionKey="categoryKey"></g:select>

I need to display the list with one of it's items removed (Where categoryKey property equals OTHER).

def Category categoryList = Category
//categoryList.categoryKey.remove("OTHER") How to remove here maybe?
return [dealInstance: dealDetails, categoryList: categoryList ]

How can I remove this in my controller and pass the new list (Minus OTHER) to the gsp?

Thanks

Upvotes: 0

Views: 397

Answers (2)

rvnrajarao
rvnrajarao

Reputation: 1

You can update toString() in grails domain to do this.

in toString() return name + ", " + categoryKey+ ", " + lastUpdated

I am displaying fields without dateCreated.

You can display fields by using above code as per ur requirement.

Upvotes: 0

micha
micha

Reputation: 49612

You can use the findAll collection function and use the closure to check for the object you want to remove.

From the documentation:

assert [2,4] == [1,2,3,4].findAll { it % 2 == 0 }

So you can do something like:

categoryList.findAll { << return false if 'it' is of type 'OTHER', otherwise true >> }

You can also use this oneliner directly in your view if you like.

Upvotes: 0

Related Questions