views:

471

answers:

2

I use jqGrid to display data which is retrieved using NHibernate. jqGrid does paging for me, I just tell NHibernate to get "count" rows starting from "n".

Also, I would like to highlight specific record. For example, in list of employees I'd like a specific employee (id) to be shown and pre-selected in table.

The problem is that this employee may be on non-current page. E.g. I display 20 rows from 0, but "highlighted" employee is #25 and is on second page.

It is possible to pass initial page to jqGrid, so, if I somehow use NHibernate to find what page the "highlighted" employee is on, it will just navigate to that page and then I'll use .setSelection(id) method of jqGrid.

So, the problem is narrowed down to this one: given specific search query like the one below, how do I tell NHibernate to calculate the page where the "highlighted" employee is?

A sample query (simplified):

var query = Session.CreateCriteria<T>();
foreach (var sr in request.SearchFields)
   query = query.Add(Expression.Like(sr.Key, "%" + sr.Value + "%"));
query.SetFirstResult((request.Page - 1) * request.Rows)
query.SetMaxResults(request.Rows)

Here, I need to alter (calculate) request.Page so that it points to the page where request.SelectedId is.

Also, one interesting thing is, if sort order is not defined, will I get the same results when I run the search query twice? I'd say that SQL Server may optimize query because order is not defined... in which case I'll only get predictable result if I pull ALL query data once, and then will programmatically in C# slice the specified portion of query results - so that no second query occur. But it will be much slower, of course.

Or, is there another way?

A: 

Pretty sure you'd have to figure out the page with another query. This would surely require you to define the column to order by. You'll need to get the order by and restriction working together to count the rows before that particular id. Once you have the number of rows before your id, you can figure what page you need to select and perform the usual paging query.

dotjoe
Not sure how do I get number of orders before the id? If query is sorted by id that easy, but it is usually sorted by name or like that... Currently I'm going to get just list of IDs from server (while still sorting and filtering) and do IndexOf(id) in there.
queen3
if it's sorted by name then you need to look up the name for the id and restrict to rows less than or greater than (depending on sort direction) that name and count those rows.
dotjoe
And if there're several same names? The is the road to hell ;-)
queen3
Then you'd use the id column as the second sort column for tiebreakers.
dotjoe
A: 

OK, so currently I do this:

    var iquery = GetPagedCriteria<T>(request, true)
                    .SetProjection(Projections.Property("Id"));
    var ids = iquery.List<Guid>();
    var index = ids.IndexOf(new Guid(request.SelectedId));
    if (index >= 0)
       request.Page = index / request.Rows + 1;

and in jqGrid setup options

       url: "${Url.Href<MyController>(c => c.JsonIndex(null))}?_SelectedId=${Id}",
       // remove _SelectedId from url once loaded because we only need to find its page once
       gridComplete: function() { 
          $("#grid").setGridParam({url: "${Url.Href<MyController>(c => c.JsonIndex(null))}"}); 
       },
       loadComplete: function() {
          $("#grid").setSelection("${Id}");
       }

That is, in request I lookup for index of id and set page if found (jqGrid even understands to display the appropriate page number in the pager because I return the page number to in in json data). In grid setup, I setup url to include the lookup id first, but after grid is loaded I remove it from url so that prev/next buttons work. However I always try to highlight the selected id in the grid.

And of course I always use sorting or the method won't work.

One problem still exists is that I pull all ids from db which is a bit of performance hit. If someone can tell how to find index of the id in the filtered/sorted query I'd accept the answer (since that's the real problem); if no then I'll accept my own answer ;-)

UPDATE: hm, if I sort by id initially I'll be able to use the technique like "SELECT COUNT(*) ... WHERE id < selectedid". This will eliminate the "pull ids" problem... but I'd like to sort by name initially, anyway.

UPDATE: after implemented, I've found a neat side-effect of this technique... when sorting, the active/selected item is preserved ;-) This works if _SelectedId is reset only when page is changed, not when grid is loaded.

UPDATE: here's sources that include the above technique: http://sprokhorenko.blogspot.com/2010/01/jqgrid-mvc-new-version-sources.html

queen3