Index: trunk/grails-app/controllers/InventoryItemPurchaseDetailedController.groovy
===================================================================
--- trunk/grails-app/controllers/InventoryItemPurchaseDetailedController.groovy	(revision 468)
+++ trunk/grails-app/controllers/InventoryItemPurchaseDetailedController.groovy	(revision 594)
@@ -39,13 +39,62 @@
         params.max = Math.min( params.max ? params.max.toInteger() : 10,  1000 )
         params.offset = params.offset?.toInteger() ?: 0
-        params.sort = params.sort ?: "purchaseOrderNumber"
-        params.order = params.order ?: "desc"
 
         def inventoryItemPurchaseList = []
         def inventoryItemPurchaseTotal
-        def filterParams = [:]
-
-        // Quick Search:
-        if(!FilterUtils.isFilterApplied(params)) {
+        def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params)
+        def isFilterApplied = FilterUtils.isFilterApplied(params)
+
+        // Restore search unless a new search is being requested.
+        if(!params.quickSearch && !filterParams) {
+            if(session.inventoryItemPurchaseQuickSearch)
+                params.quickSearch = session.inventoryItemPurchaseQuickSearch
+            else if(session.inventoryItemPurchaseSearchFilterParams) {
+                session.inventoryItemPurchaseSearchFilterParams.each() { params[it.key] = it.value }
+                params.filter = session.inventoryItemPurchaseSearchFilter
+                isFilterApplied = FilterUtils.isFilterApplied(params)
+            }
+        }
+
+        // Remember sort if supplied, otherwise try to restore.
+        if(params.sort && params.order) {
+            // Reset to defaultSort if requested.
+            if(params.sort == 'defaultSort') {
+                params.sort = null
+                params.order = null
+                session.removeAttribute("inventoryItemPurchaseSearchSort")
+                session.removeAttribute("inventoryItemPurchaseSearchOrder")
+            }
+            else {
+                session.inventoryItemPurchaseSearchSort = params.sort
+                session.inventoryItemPurchaseSearchOrder = params.order
+            }
+        }
+        else if(session.inventoryItemPurchaseSearchSort && session.inventoryItemPurchaseSearchOrder) {
+            params.sort = session.inventoryItemPurchaseSearchSort
+            params.order = session.inventoryItemPurchaseSearchOrder
+        }
+        else {
+            params.sort = "purchaseOrderNumber"
+            params.order = "desc"
+        }
+
+        if(isFilterApplied) {
+            // filterPane:
+            params.sort = params.sort ?: "id"
+            params.order = params.order ?: "desc"
+            //if(params.sort == "attentionFlag") // See ticket #64 in Trac.
+                //params.sort = "id"
+
+            // Call filterService.
+            inventoryItemPurchaseList = filterService.filter( params, InventoryItemPurchase )
+            inventoryItemPurchaseTotal = filterService.count( params, InventoryItemPurchase )
+            filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params)
+            // Remember search.
+            session.inventoryItemPurchaseSearchFilterParams = new LinkedHashMap(filterParams)
+            session.inventoryItemPurchaseSearchFilter = new LinkedHashMap(params.filter)
+            session.removeAttribute("inventoryItemPurchaseQuickSearch")
+        }
+        else {
+            // Quick Search:
 
             if(params.quickSearch == "searchAllOrders") {
@@ -80,10 +129,8 @@
             inventoryItemPurchaseTotal = inventoryItemPurchaseList.size()
             filterParams.quickSearch = params.quickSearch
-        }
-        else {
-            // filterPane:
-            inventoryItemPurchaseList = filterService.filter( params, InventoryItemPurchase )
-            inventoryItemPurchaseTotal = filterService.count( params, InventoryItemPurchase )
-            filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params)
+            // Remember search.
+            session.removeAttribute("inventoryItemPurchaseSearchFilterParams")
+            session.removeAttribute("inventoryItemPurchaseSearchFilter")
+            session.inventoryItemPurchaseQuickSearch = params.quickSearch
         }
 
