Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
The Grid – Basics of Paging (0 Comments)
Admin: Posted Date: April 4, 2010

A paging grid must have a server side element to perform the breaking up of data into pages.

For this example we are using php as the server side language, and a MySQL database with some randomly generated data.

The Grid – Basics of Paging

Grid Data

A paging grid must have a server side element to perform the breaking up of data into pages.

For this example we are using php as the server side language, and a MySQL database with some randomly generated data. The PHP script below is used to retrieve our data and break it up into pages using the limit and start variables that are passed from the paging toolbar.

 

  1. $link = mysql_pconnect("test-db.vinylfox.com""test""testuser")   
  2.     or die("Could not connect");  
  3. mysql_select_db("test"or die("Could not select database");  
  4.   
  5. $sql_count = "SELECT id, name, title, hire_date, active FROM random_employee_data";  
  6. $sql = $sql_count . " LIMIT ".$_GET['start'].", ".$_GET['limit'];  
  7.   
  8. $rs_count = mysql_query($sql_count);  
  9.   
  10. $rows = mysql_num_rows($rs_count);  
  11.   
  12. $rs = mysql_query($sql);  
  13.   
  14. while($obj = mysql_fetch_object($rs))  
  15. {  
  16.     $arr[] = $obj;  
  17. }  
  18.   
  19. Echo $_GET['callback'].'({"total":"'.$rows.'","results":'.json_encode($arr).'})';   
$link = mysql_pconnect("test-db.vinylfox.com", "test", "testuser") or die("Could not connect"); mysql_select_db("test") or die("Could not select database"); $sql_count = "SELECT id, name, title, hire_date, active FROM random_employee_data"; $sql = $sql_count . " LIMIT ".$_GET['start'].", ".$_GET['limit']; $rs_count = mysql_query($sql_count); $rows = mysql_num_rows($rs_count); $rs = mysql_query($sql); while($obj = mysql_fetch_object($rs)) { $arr[] = $obj; } Echo $_GET['callback'].'({"total":"'.$rows.'","results":'.json_encode($arr).'})'; 

Were not going to dig much into the server side code, since it can vary quite a bit for each developers needs and environment.

                 

Whats Needed to Make a Paged Grid?

NOTE: The example uses ScriptTagProxy only because the data resides on a different server from the example code. In most cases you will be pulling data from the same server the code resides on and using HttpProxy.

The only difference in the data Store is the addition of a totalProperty declaration. In our example, we use 'total' which is coming from our server side script with the value for the total number of rows we had before breaking them up into pages.

 

  1. var ds = new Ext.data.Store({  
  2.   
  3.     proxy: new Ext.data.ScriptTagProxy({  
  4.         url: 'http://www.vinylfox.com/yui-ext/examples/grid-paging/grid-paging-data.php'  
  5.     }),  
  6.   
  7.     reader: new Ext.data.JsonReader({  
  8.         root: 'results',  
  9.         totalProperty: 'total',  
  10.         id: 'id'  
  11.     }, [  
  12.         {name: 'employee_name', mapping: 'name'},  
  13.         {name: 'job_title', mapping: 'title'},  
  14.         {name: 'hire_date', mapping: 'hire_date', type: 'date', dateFormat: 'm-d-Y'},  
  15.         {name: 'is_active', mapping: 'active'}  
  16.     ])  
  17.   
  18. });  
var ds = new Ext.data.Store({ proxy: new Ext.data.ScriptTagProxy({ url: 'http://www.vinylfox.com/yui-ext/examples/grid-paging/grid-paging-data.php' }), reader: new Ext.data.JsonReader({ root: 'results', totalProperty: 'total', id: 'id' }, [ {name: 'employee_name', mapping: 'name'}, {name: 'job_title', mapping: 'title'}, {name: 'hire_date', mapping: 'hire_date', type: 'date', dateFormat: 'm-d-Y'}, {name: 'is_active', mapping: 'active'} ]) });

The Paging Bar

Adding a paging toolbar to the bottom of the grid pane, and your almost done.

 

  1. var gridFoot = grid.getView().getFooterPanel(true);  
  2.   
  3. var paging = new Ext.PagingToolbar(gridFoot, ds, {  
  4.     pageSize: 25,  
  5.     displayInfo: true,  
  6.     displayMsg: 'Displaying results {0} - {1} of {2}',  
  7.     emptyMsg: "No results to display"  
  8. });  
var gridFoot = grid.getView().getFooterPanel(true); var paging = new Ext.PagingToolbar(gridFoot, ds, { pageSize: 25, displayInfo: true, displayMsg: 'Displaying results {0} - {1} of {2}', emptyMsg: "No results to display" });

The last step is to pass the initial start and limit parameters to your data load.

 

  1. ds.load({params:{start:0, limit:25}});  
ds.load({params:{start:0, limit:25}});

A large portion of the time spent setting up paging is going to be your server side environment that handles passing data back and forth with the grid, once you have that in place the task of adding paging to a grid can be very simple.

The Final Product

                 

grid-paging.js

 

  1. Ext.onReady(function(){  
  2.   
  3.     var ds = new Ext.data.Store({  
  4.         // HttpProxy should be used here  
  5.         proxy: new Ext.data.ScriptTagProxy({  
  6.             url: 'http://www.vinylfox.com/yui-ext/examples/grid-paging/grid-paging-data.php'  
  7.         }),  
  8.           
  9.         reader: new Ext.data.JsonReader({  
  10.             root: 'results',  
  11.             totalProperty: 'total',  
  12.             id: 'id'  
  13.         }, [  
  14.             {name: 'employee_name', mapping: 'name'},  
  15.             {name: 'job_title', mapping: 'title'},  
  16.             {name: 'hire_date', mapping: 'hire_date', type: 'date', dateFormat: 'm-d-Y'},  
  17.             {name: 'is_active', mapping: 'active'}  
  18.         ])  
  19.           
  20.     });  
  21.   
  22.     var cm = new Ext.grid.ColumnModel([{  
  23.            id: 'name',  
  24.            header: "Name",  
  25.            dataIndex: 'employee_name',  
  26.            width: 100  
  27.         },{  
  28.            header: "Title",  
  29.            dataIndex: 'job_title',  
  30.            width: 170  
  31.         },{  
  32.            header: "Hire Date",  
  33.            dataIndex: 'hire_date',  
  34.            width: 70,  
  35.            renderer: Ext.util.Format.dateRenderer('n/j/Y')  
  36.         },{  
  37.            header: "Active",  
  38.            dataIndex: 'is_active',  
  39.            width: 50  
  40.         }]);  
  41.   
  42.     var grid = new Ext.grid.Grid('grid-paging', {  
  43.         ds: ds,  
  44.         cm: cm,  
  45.         autoExpandColumn: 'name'  
  46.     });  
  47.   
  48.     grid.render();  
  49.   
  50.     var gridFoot = grid.getView().getFooterPanel(true);  
  51.   
  52.     var paging = new Ext.PagingToolbar(gridFoot, ds, {  
  53.         pageSize: 25,  
  54.         displayInfo: true,  
  55.         displayMsg: 'Displaying results {0} - {1} of {2}',  
  56.         emptyMsg: "No results to display"  
  57.     });  
  58.   
  59.     ds.load({params:{start:0, limit:25}});  
  60.   
  61. });  
Ext.onReady(function(){ var ds = new Ext.data.Store({ // HttpProxy should be used here proxy: new Ext.data.ScriptTagProxy({ url: 'http://www.vinylfox.com/yui-ext/examples/grid-paging/grid-paging-data.php' }), reader: new Ext.data.JsonReader({ root: 'results', totalProperty: 'total', id: 'id' }, [ {name: 'employee_name', mapping: 'name'}, {name: 'job_title', mapping: 'title'}, {name: 'hire_date', mapping: 'hire_date', type: 'date', dateFormat: 'm-d-Y'}, {name: 'is_active', mapping: 'active'} ]) }); var cm = new Ext.grid.ColumnModel([{ id: 'name', header: "Name", dataIndex: 'employee_name', width: 100 },{ header: "Title", dataIndex: 'job_title', width: 170 },{ header: "Hire Date", dataIndex: 'hire_date', width: 70, renderer: Ext.util.Format.dateRenderer('n/j/Y') },{ header: "Active", dataIndex: 'is_active', width: 50 }]); var grid = new Ext.grid.Grid('grid-paging', { ds: ds, cm: cm, autoExpandColumn: 'name' }); grid.render(); var gridFoot = grid.getView().getFooterPanel(true); var paging = new Ext.PagingToolbar(gridFoot, ds, { pageSize: 25, displayInfo: true, displayMsg: 'Displaying results {0} - {1} of {2}', emptyMsg: "No results to display" }); ds.load({params:{start:0, limit:25}}); });

grid-paging-working.php

 

  1. <html>  
  2.     <head>  
  3.         <title>Paging Grid Example</title>  
  4.   
  5.         <link rel="stylesheet" type="text/css" href="/lib/resources/css/ext-all.css" />  
  6.         <script type="text/javascript" src="/lib/yui-utilities.js"></script>  
  7.         <script type="text/javascript" src="/lib/ext-yui-adapter.js"></script>  
  8.         <script type="text/javascript" src="/lib/ext-all-debug.js"></script>  
  9.           
  10.         <script type="text/javascript" src="grid-paging.js"></script>  
  11.   
  12.     </head>  
  13.     <body>  
  14.       
  15.         <div id="grid-paging" style="border:1px solid #99bbe8;overflow: hidden; width: 665px; height: 300px;"></div>  
  16.       
  17.     </body>  
  18. </html> 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: