This document is based on questions asked on PEAR general mailing list and other mailing lists and forums.
The setRequestPrefix() method is the solution for this problem. Each DataGrid for the page needs such a prefix that is internally used before the GET parameters for sorting and paging. An example of the usage:
require_once 'Structures/DataGrid.php'; $datagrid1 = new Structures_DataGrid(); $datagrid2 = new Structures_DataGrid(); $datagrid1->bind('SELECT * FROM trade', array('dsn' => DSN)); $datagrid2->bind('SELECT * FROM stock', array('dsn' => DSN)); $datagrid1->setRequestPrefix('trade_'); $datagrid2->setRequestPrefix('stock_'); $datagrid1->render(); $datagrid2->render(); |
Note: You need to call setRequestPrefix() before calling bind().
Currently there are four DataSource drivers that are recommended in the sense of efficiency:
DB_DataObject
DB_Table
DBQuery
MDB2
These four drivers will only fetch the needed records from the database. For example, if you have a row limit of 15 records per page, they will only fetch (up to) 15 records.
All other DataSource drivers can, of course, also be used. But there is no logic implemented (better said: implementable) to avoid fetching (or keeping in memory) unneeded records.
You need a formatter for the new column that should hold the row number. The first parameter that is passed to such a formatter function contains a currRow value with the row number per page. For calculating the row number relative to the whole table, you need to take also the getCurrentRecordNumberStart() method into account.
The following code snippet shows you how to define the formatter function and how to add the column (with # as the column label and right aligned values):
function formatRowNumber($params, $recordNumberStart) { return $params['currRow'] + $recordNumberStart; } $datagrid->addColumn( new Structures_DataGrid_Column( '#', null, null, array('style' => 'text-align: right;'), null, 'formatRowNumber', $datagrid->getCurrentRecordNumberStart() )); |