Home > Archive > PHP Pear > June 2005 > Structures_DataGrid HTML_Template_Flexy renderer
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Structures_DataGrid HTML_Template_Flexy renderer
|
|
| Dan Rossi 2005-06-07, 3:56 am |
| I have been advised to tweak the renderer and gain acceptance via user
testing. I have created a mix of the table and smarty renderer. It
requires Core.php from CVS for the page results function which the
template gets from the renderer. I had been advised to take this out,
however I cant work out a way to get this function from outside the
renderer to the template ??
It collects flexy options if you are creating a new object from a
static property or sending the options to setTemplate, if you are not
sending the object with setFlexyObj.
I use image navigation, you will see in the paging i had to call the
getlinks and then append first and last to the navigation. I made an
attempt to get first and last added to links in cvs so I could just
call $this->pager->links but was not accepted. You have to set the
firstPagePre and firstPagePost to empty so it wont add brackets around
the image pretty clunky.
www.electroteque.org/pear/DataGrid/
Here is an example of how I use it
function data_grid($do, $template, $limit = 10, $obj = null)
{
/**
* Require the DataGrid and DataSource
*/
require_once 'Structures/DataGrid.php';
require_once 'Structures/DataGrid/DataSource.php';
/*
* Collect the Pager options
*/
$paging_options = & PEAR::getStaticProperty('Pager','options
');
$options = array(
'prevImg'=><img name=\Back$i\ src=\images/nav_back_off.gif\ border=\0\
alt=\Previous Page\ onmouseover=\this.src='images/nav_back_on.gif'\
onmouseout=\this.src='images/nav_back_off.gif'\>,
'nextImg'=><img name=\Forward$i\ src=\images/nav_forward_off.gif\
border=\0\ alt=\Next Page\
onmouseover=\this.src='images/nav_forward_on.gif'\
onmouseout=\this.src='images/nav_forward_off.gif'\>,
'firstPageText'=><img name=\First$i\ src=\images/nav_first_off.gif\
border=\0\ alt=\First Page\
onmouseover=\this.src='images/nav_first_on.gif'\
onmouseout=\this.src='images/nav_first_off.gif'\>,
'lastPageText'=><img name=\Last$i\ src=\images/nav_last_off.gif\
border=\0\ alt=\Last Page\
onmouseover=\this.src='images/nav_last_on.gif'\
onmouseout=\this.src='images/nav_last_off.gif'\>,
'firstPagePre'=>'',
'firstPagePost'=>'',
'lastPagePre'=> '',
'lastPagePost'=> ''
);
if (is_array($this->settings['Pager'])) {
$paging_options = array_merge($this->settings['Pager'],$options);
}
/*
* Setup the DataGrid
*/
$dg =& new Structures_DataGrid($_GET['setPerPage'] ? $_GET['setPerPage']
: $limit,$_GET['page'] ? $_GET['page'] : 1,'Flexy');
$dg->sortRecordSet(isset($_GET['orderBy']) ? $_GET['orderBy'] : '',
isset($_GET['direction']) ? $_GET['direction'] : 'ASC');
if (is_object($do)) {
/*
* Setup the DataObject as the DataSource
*/
$data = Structures_DataGrid_DataSource::create($
do,false,'DataObject');
$dg->bindDataSource($data);
$this->dg = $dg;
/*
* Send the flexy object to the flexy renderer
*/
$dg->renderer->setFlexyObj($this->tpl);
/*
* Send the template to the flexy renderer
*/
$dg->renderer->setTemplate($template);
return $dg->renderer->render($obj);
} elseif (is_array($do) && !empty($do)) {
/*
* Setup an array as the DataSource
*/
$data = Structures_DataGrid_DataSource::create($
do);
$dg->bindDataSource($data);
$this->dg = $dg;
$dg->renderer->setTemplate($template);
return $dg->renderer->render($obj);
}
| |
| Mark Wiesemann 2005-06-07, 8:57 am |
| Dan Rossi wrote:
> I had been advised to take this out, however I cant work out a
> way to get this function from outside the renderer to the
> template ??
My suggestion: Add something like the following to your renderer:
function setFlexy(&$flexy)
{
$this->_flexy = &$flexy;
}
Then construct the flexy object in your script (the script that uses
SDG) and pass this object to SDG like this:
$dg->renderer->setFlexy($flexy);
If you add a variable "statistics" to your template, then you could use
$statistics = 'page ' . $dg->getCurrentPage() . ' from ' .
$dg->getPageCount() . ', record ' . $dg->getCurrentRecordNumberStart()
... ' to ' . $dg->getCurrentRecordNumberEnd() . ' from ' .
$dg->getRecordCount();
for this new template variable.
If you want to use your setTemplate() function in the renderer class,
then add a getFlexyObject() function to it which does only the
following: return $this->_flexy;
Or, even better: Write a getFlexy() function that is used by render().
Both will return the flexy object that you can use then the way I
described above. (Look into the Smarty renderer - it's done there like
this.)
Mark
| |
| Mark Wiesemann 2005-06-07, 4:00 pm |
| Dan Rossi wrote:
> Mark its already there
>
> $dg->renderer->setFlexyObj($this->tpl);
Okay, perfect. I have overlooked it because of the tab/spaces problem.
> It would still need to be in the renderer right ?
No, not in the renderer. Do it better in the function data_grid().
> I cant see why the function couldnt be there, considering its a
> renderer and a datagrid, and most of the datagrids i use require
> paging :)
I _can_ in be the renderer class, no problem. But:
- the statistical functions are in Core.php (=> they can be used with
each renderer, they don't exist more than once)
- not everybody want's to show the page number, the numbers of pages
etc. the same way you do it (altough it's flexbile because of using
templates, one is even more flexible by not having this directly in the
renderer class)
Maybe Andrew told you also something like this? Or how did you mean "I
have been advised"?
> You've lost me here :|
Hmm, describing this more detail would require much typing. What I meant
was, in short: you can do it the way how Smarty and HTMLTable renderer
do it: Having a render() function that calls getSmarty() or toHTML() and
return the results of them. This way render() renders the DataGrid
directly and getSmarty() returns the template object / toHTML returns
the HTML code instead of outputting it.
But the way it is, is also fine, as one can pass the template (flexy)
object to SDG.
Mark
| |
| Mark Wiesemann 2005-06-08, 8:58 am |
| Hi Dan,
your first post didn't get to list but I'll answer it here, too:
>
> ??? Hmm I use eclipse IDE on a mac could that be the problem ? I dont
> see the issues.
Open the .phps of your class in your browser, then you should see it.
And also set the tabs setting in Eclipse to 4 spaces.
> Let me see I doubt the template could get the variable from within the
> datagrid method.
Which variable?
Why not doing it like this? (after binding)
$template->setVariable('statistics', 'found ' . $dg->getRecordCount() .
' records on ' . $dg->getPageCount());
(syntax may be wrong, I only know HTML_Template_Sigma)
> Okies not everyone wants to say found 10 results in 10 pages for
> instance ?
Yes. Some people might also want to display the numbers of the records
on the page (e.g. "record 11 to 20 from 50") or want to have in another
language.
> Ok i have made more tweaks to it, this time it returns the rendered
> flexy object, and then i send it as a variable to an outer shell
> template, in this outer shell template I call my custom getResults
> method and the renderers getPaging method and the pager selectbox
I can't see everything of this as code on your website but it sounds to
be the right way.
Please apply my fix described in bug #4521 to your renderer, otherwise I
have to open a new bug report after your renderer has been accepted. ;-)
The getPaging() function should return the links array from Pager, not a
string with only parts of the array. This is also for more flexibility
and for more compatibility with HTMLTable renderer (which also returns
the array).
One comment to your data_grid function() itself (altough I'm not a
DataObject expert):
You don't need the "if (is_object($do)) {". Just do a $dg->bind($do).
SDG recognizes whether $do is a DataObject instance or an array.
Mark
|
|
|
|
|