donguy76
donguy76

Reputation: 640

Grails : Updating Index.gsp from another controller

My Grails app is using version 2.3.6

In this app there's a Controller class:

class UserOrderController {

def index(Integer max) {
        params.max = Math.min(max ?: 20, 100)       
        respond UserOrder.list(params), model:[userOrderInstanceCount: WorkOrder.count()]
    }
}

Now in another Controller i am accessing the UserOrder objects and filtering based on product ID. Product ID is a string property in UserOrder domain class.

The other controller is:

class UserOrderFilterController {

def getUOBasedOnID () {

  // Here i get a new list for **UserOrder**
  // Now i want to draw the UserOrderController page with the new list
}
}

I am pretty new to Grails and not sure how to do this.

Should i be creating a new INDEX function in UserOrderController class and pass the new list?

Like shown below:

class UserOrderController {

def index(Integer max) {
        params.max = Math.min(max ?: 20, 100)       
        respond UserOrder.list(params), model:[userOrderInstanceCount: userOrder.count()]
    }

 def index(List UserOrder) {

   // create page???
}

}

UPDATE:

The UserOrderFilterController has it's own index.gsp file. What am doing is: Accessing all the objects for UserOrder domain class and filter them based on a property.

Now in the index.gsp of UserOrderFilterController i will show the total number of objects/orders found. This number will be shown with a hyperlink using href and when the user clicks on it, it will go to index.gsp page of UserOrderController with only the filtered UserOder's displayed.

So what am expecting is:

<a href='${createLink(controller:'UserOrder', action:'index')}'>%s</a>

A href like shown above with a params field that will have the filtered list of UserOrder's.

I have no idea how to add the params/list to href. Is this possible?

Upvotes: 0

Views: 759

Answers (2)

christopher
christopher

Reputation: 27346

Each action in Grails has a matching view. Duplicating actions is asking for trouble. If you have some new functionality that deserves a page of its own, then you should create a new action and a new view.

If it belongs on the home page, then put it through the same index method.

Note that in grails, you simply pass the values to the .gsp page and the .gsp page is what handles the formatting of the data. Your action should have absolutely 0 knowledge of the structure of the view. This is a core concept in MVC.

In this case, you can redirect to a controller, as per the docs. The one that should interest you the most is this:

redirect(controller: "yourController", action:"index", params=[filteredList: filteredList]);

This will redirect to the existing index action in your UserOrderController, and pass in the filtered list. From there, you can have something like..

if(params.filteredList) {
    // You know this came from the filtered controller, so display this and don't
    // make a DB call.
}

Point to Note

  • The structure of your grails application worries me. Why have you got an entirely separate controller for simply filtering data?

Edit

Taking the createLink approach can be improved a bit. Instead of..

<a href='${createLink(controller:'UserOrder', action:'index')}'>%s</a>

You can use the g:link functionality:

<g:link controller="UserOrder" action="index" params=['filteredList':filteredList]>%s</g:link>

Upvotes: 1

Ramsharan
Ramsharan

Reputation: 2064

If you are trying to create new controller for filter UserOrderFilterController, you can do

class UserOrderFilterController {

def getUOBasedOnID () {

  //get filtered list
    respond filteredList, [view:'userOrder/index',model:[userOrderInstanceCount: filteredListCount]]
  }
}

You can see more about respond here.

Upvotes: 1

Related Questions