tabular data visualization with jqGrid and jQuery

In my post Data Scientist Makes Peace with Web Programming, I argued that web programming skills belong in data scientists’ repertoire. Essentially, my thesis went that data consumers primarily use web browsers to interact with data, so data scientists should develop proficiency with the medium. With that in mind, I’ll periodically showcase core web development techniques from a data scientist’s point of view.

Today I’ll demonstrate the presentation of tabular data within a web browser using the jqGrid jQuery plugin. I’ll also show how to set all the jqGrid display features (header names, number of columns, row data, etc.) from the server side. (The jqGrid documentation primarily details client-side configuration of these parameters).

jqGrid in action:

Here the web browser retrieves content from the server in a serialized format (e.g. JSON), and jqGrid organizes it into the nice display pictured above. Refreshing the table updates the data, provided the server sends updated JSON.

jqGrid doesn’t stop there though—if it did we would just use the table HTML tag instead to display our data—it provides automatic scrollbars, cell and row editing, non-scrolling headers, and many more features.

Demonstration Code

To make this work, we first load jQuery and jqGrid, and then create an HTML container for the table:

<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
<html xmlns="" xml:lang="en" lang="en">

    <link type="text/css" href="" rel="Stylesheet" />
    <link type="text/css" href="/site_media/js/jqGrid/css/ui.jqgrid.css" rel="Stylesheet" />
    <script src="/site_media/js/jquery.js"></script>
    <script type="text/javascript" src="/site_media/js/jquery-ui-1.8.9.minilims.min.js"></script>

    <title>jqGrid Demonstration</title>

    <script src="/site_media/js/example/example.js"></script>
    <script src="/site_media/js/jqGrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>


    <div id="example_container">
      <table id="example_container_table"></table>

We then activate the jqGrid table with JavaScript:


    // Get the serialized table data and store it in the variable "data".
    $.getJSON('example/', function(data) {


   // Instruct jqGrid to read the serialized data as JSON
   datatype: "json",

   // Initialize the column model from the JSON-retrieved content.
   colNames: data.colNames,
   colModel: data.colModel,

   // Initialize the caption from the JSON-retrieved content.
   caption: data.caption,

   // Allow an unlimited number of rows on the screen at one time.
   rowNum: -1,

   // Miscellaneous settings
   viewrecords: true,
   width: 367,
   height: 300,
   shrinkToFit: false

//Populate the rows from the JSON-retrieved content.

Notice that we do not need to set the “url” property since the $.getJSON call retrieves the data and the addJSONData() function populates the rows with it.

Finally, on the server side we provide code to deliver the content in JSON format. In Django this looks like:

from django.http import HttpResponse
from django.utils.simplejson import dumps

def example(request):
  data = {'page' : 1, 'total' : 1, 'rows' : []} 
  row_data_list = [

  for index, row in enumerate(row_data_list):
    row_data = {
      'id' : 'row_number_' + str(index),
      'cell' : row

  data['colNames'] = ['Window Size', 'Method', 'Group', 'Classifier Score']
  data['colModel'] = [
    {'name' : 'Window Size', 'index' : 'Window Size', 'width' : 130, 'align' : 'center'},
    {'name' : 'Method', 'index' : 'Method', 'width' : 50, 'align' : 'center'},
    {'name' : 'Group', 'index' : 'Group', 'width' : 50, 'align' : 'center'},

    {'name' : 'Classifier Score', 'index' : 'Classifier Score', 'width' : 120, 
     'align' : 'center', 'formatter' : 'number', 'formatoptions' : {'decimalPlaces' : 2}

  data['caption'] = 'Simulation Results'

  response = HttpResponse(dumps(data), mimetype='application/json')
  response['Cache-Control'] = 'no-cache'
  return response

The file that calls this function contains:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from minilims.example.views import *

urlpatterns = patterns (
  (r'^$', direct_to_template, {'template': 'example/index.html'}),
  (r'example/$', example, {}),

Post Author: badassdatascience

Leave a Reply

Your email address will not be published.