Bob
Bob

Reputation: 13

Grails Sorting Issue

I have a grails app developed. Herein, I have a page which accepts data and as per the data . goes to the list action, fires an sql, populates the data into reconciliationInstance object and displays it in list.gsp.

In my list.gsp, I have,

<g:sortableColumn property="access"
    title="${message(code: 'reconciliationInstance.access.label', default: 'Access')}" 
    style="width: 10px" defaultOrder="desc"/>

However, when I click on the "Amount" heading, it takes me back to the list action again.

I have around 15 columns in the page and want to have sorting for all of them. Am I missing something here?? To rectify this issue ,I wrote the below code. Redirected to action sort. But theres something wrong here I believe.

def sort = {
  if (!params.sort) params.sort = "title"
  if (!params.order) params.order = "asc"      
    def reconciliationInstanceList = new ArrayList<Reconciliation>()
reconciliationInstanceList=session["reconciliationInstanceList"]
    order(params.sort, params.order)
   [reconciliationInstanceList: reconciliationInstanceList]
 }

I have saved reconciliationInstanceList in a session. Any Advice/Inputs?

My list action code is as below.

def list ={

//Taking parameters entered in the previous page def odcNum=params.odcNum def odcDate=params.odcDate
def date=null

 def reconciliationInstance = new Reconciliation()
 reconciliationInstance.properties=params
 //Validation if all parameters have been entered by the user
     if (reconciliationInstance.validate()) { 

 def results        
 SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MMM-yyyy")    
 if(odcDate instanceof Date) {
 date = sdfSource.format(odcDate);
 }else{     
 date = odcDate
 }

//Query to be fired. I have altered this query a bit. My actual query returns around 15 parameters 
 String odcData="select odc_access from odc_manager where odc_date=to_char('" + date + "') and odc_num like trim('" + odcNum + "')"

 def reconciliationInstanceList = new ArrayList<Reconciliation>()       
 Sql sql = new Sql(dataSource)       
 results = sql.eachRow (odcData)
 {          
 def reconciliation = new Reconciliation()              
 reconciliation.setAccess it.access
 reconciliationInstanceList.add reconciliation
 session["reconciliationInstanceList"]=reconciliationInstanceList
 }

 [reconciliationInstanceList: reconciliationInstanceList]

     }
     else {
         render(view: "search", model: [reconciliationInstance: reconciliationInstance])
     }
 }

BTW I am a novice at grails. Therefore, you would find a bit of java in my code. Somethings missing in my code? Therefore sort doesnt work. Inputs?

Upvotes: 0

Views: 1035

Answers (2)

Bob
Bob

Reputation: 13

Found a work around for this. Passed the sort order (params.order) to the sql query and let the query do the sorting. Displayed the results on the gsp then.

If there's any other way, do let me know.

Upvotes: 1

Aaron Saunders
Aaron Saunders

Reputation: 33335

it should take you back to the list action, but the params passed to the action will let it know how to sort the resulting model.

the behavior is correct, I assume your code in the list action is not coded properly... You might want to include that code if you want additional guidance.

See sample list action

http://www.grails.org/GSP+Tag+-+sortableColumn

Upvotes: 1

Related Questions