views:

95

answers:

1

Ok, I am new at jQuery, but the JQGrid has peaked my interest. While implementing the grid, I have come across two problems that I am not sure how to solve. Both involve loading the grid with results.

  1. How do you load the grid when you have parameters in your route. For instance, http://domain.com/search/results/2010/KY...I am wanting all results matching 2010 in Kentucky. In the javascript section of the grid initialization, I need to supply a URL (such as /search/gridResults/). How does one pass the other route values or at least use them to load the grid.

  2. Same question, but more along the lines of when the page is loaded with posted form values from a search form.

Perhaps the URL is mostly to do with AJAX-y functions. It would be nice to sort and page with AJAX but to load the grid with AJAX is not neccessary.

Sorry for the long post, but I am sure others have faced this problem even though Google tells me otherwise :) PS - I have looked at Phil Haacks (sp?) and Craig something's blogs about using JQGrid, but neither touch upon loading pre-determined search results.

A: 
  1. You can specify that directly with the 'url' key. e.g.: /search/gridResults/2010/KY
  2. I actually use a custom javascript method in the postData jqgrid key for this (which you could use to solve your question 1 depending on the situation). This seemed kind of lame to me that I had to write this method, but I found something on the internet and had to keep hacking on it to make it flexible enough for my uses.

Code for custom method below. It reads params from the url directly. For POST params, you would need to do something else obviously, but to get them to jqgrid, it's the same idea:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
  var vars = [], hash;
  var hashes = window.location.href.slice(
    window.location.href.indexOf('?') + 1
  ).split('&');
  for(var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    if (hash.length == 2) {
      vars.push(hash[0]);
      vars[hash[0]] = decodeURIComponent(hash[1].replace("+", "%20"));
    }
  }
  return vars;
}

Hopefully that helps... If you come up with something better, I'd love to hear it. :)

dpb