Dojo's Industrial-Strength Grid Widget
So far, the examples demonstrated here are using ItemFileReadStore or ItemFileWriteStore, which necessarily implies that your data set is small enough that it's practical to load it into the client. In other words, we've been dodging the issue of having such a large data set (say, millions of records) that it can't all be loaded into the client. Let's put together a final example that demonstrates the grid at work using a server-backed store, such as the dojox.data.QueryReadStore. The markup for defining the DataGrid should look familiar enough. Note that because the QueryReadStore implements only the Read and Identity APIs, trying to make cells editable would have no effect. It's totally possible, however, to extend the QueryReadStore with Write and Notification support or attach a store, such as the dojox.data.JsonRestStore, that implements all four dojo.data APIs to produce an editable interface:
<body class="tundra">
<!--Fetch data from a store as usual.
This time, it just happens to be a QueryReadStore -->
<span dojoType="dojox.data.QueryReadStore"
jsId="gridStore"
url="/data">
</span>
<!-- Define the grid directly in markup and allow the parser
to take care of the rest -->
<table id="gridNode"
dojoType="dojox.grid.DataGrid" store="gridStore">
<thead>
<tr>
<th width="50%" field="id">ID</th>
<th width="50%" field="label">Label</th>
</tr>
</thead>
</table>
</body>
To try out the example, however, you need a basic server implementation that returns pages of data whenever the QueryReadStore requests them. A minimalistic server written in CherryPy is shown in Listing 4.

Figure 5. Given a server-backed store, the DataGrid can render arbitrary numbers of rows—all without pagination!
Listing 4. An ultra-simple Web server that provides slices of a very large (mock) data source for a dojox.grid.Grid client that uses a dojox.data.QueryReadStore to page the data on demand.
import cherrypy #do an "easy_install cherrypy" to get it
from cherrypy.lib.static import serve_file
import demjson #do an "easy_install demjson" to get it
import os
from random import randint #for building up mock data
json = demjson.JSON(compactly=False)
jsonify = json.encode
NUM_ITEMS = 1000000
class Content:
def __init__(self):
"""
Maybe you would call out to a db with some sql to get some
data based on the query string that comes into /data. For
now, we'll build up some static data to use.
"""
self.items = []
possible_item_labels = ["foo", "bar", "baz", "qux"]
id=0
for i in xrange(NUM_ITEMS):
self.items.append({
'id' : id,
'label' : possible_item_labels[randint(0,3)]
})
id +=1
#keep track of sort order b/c sorting is expensive...
self.current_sort_order = ""
@cherrypy.expose
def data(self, **kw):
"""
Serve up the data via http://localhost:8000/data
kw will contain whatever is in your store's query.
By default the query string will come across as
something like:
?name=*&start=0&count=20 to populate the table
Note: you may get into trouble if you have multiple users
trying to access this url and changing the sort order of
items all at the same time (but relax, this is just
a little demo.)
"""
#sorting the items by values for a given dictionary key...
if kw.get('sort') and self.current_sort_order != kw.get('sort'):
if kw['sort'][0] == '-': #descending order, slice off the -
self.items.sort(lambda m,n:cmp(m.get(kw['sort'][1:]), \
n.get(kw['sort'][1:])),reverse=True)
else: #ascending order
self.items.sort(lambda m,n:cmp(m.get(kw['sort']), \
n.get(kw['sort'])))
self.current_sort_order = kw['sort']
#slicing the data...
start = int(kw['start'])
end = start + int(kw['count'])
#serving up the slice of interest as well as the total size
return jsonify({
'numRows':NUM_ITEMS,
'items':self.items[start:end],
'identifier' : 'id'
})
@cherrypy.expose
def index(self, **kw):
"""
Serve up the web page through http://localhost:8000/
"""
return serve_file(os.path.join(os.getcwd(), 'page.html'))
#the page containing the grid
cherrypy.server.socket_port = 8000
cherrypy.quickstart(Content(),'/')
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Web Development News
Developer Poll
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- seo services in india
2 hours 10 min ago - For KDE install kio-mtp
2 hours 11 min ago - Evernote is much more...
4 hours 11 min ago - Reply to comment | Linux Journal
12 hours 56 min ago - Dynamic DNS
13 hours 30 min ago - Reply to comment | Linux Journal
14 hours 29 min ago - Reply to comment | Linux Journal
15 hours 19 min ago - Not free anymore
19 hours 21 min ago - Great
23 hours 8 min ago - Reply to comment | Linux Journal
23 hours 16 min ago








Comments
Excellent article
Excellent article .