New plug-in: listview
| From: | David Perry | Date: | Wed, 12 Mar 2003 22:02:04 +0000 |
| Subject: | New plug-in: listview | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-8600@lists.php.net to get a copy of this message | ||
Hey there! This is my first time in this newsgroup, but I've been using
Smarty for a while, and love it.
At work, we wanted to provide a consistent interface for tabular lists
of data (of different types in different places), and we wanted the user
to be able to sort these tabular lists by clicking on an icon in the
column header. I mucked around a bit and came up with the attached
plugin. I post it here in the hopes that someone else will find it as
useful.
I documented its use in the header, but I'll try to give a shortened
version here. Basically, you define a sorting function in PHP for each
column of your data that you want to be sortable. Then, pass that data
and the sorting functions into {listview}, and it'll put the sorted data
into $listviewOutput for you to use in your template. The user just
sees pretty headers (if you're good with CSS (: ) and small sort buttons
(which you'll have to provide) that they can click to change the sort
order of the data.
Here's an example:
>>> addresses.php:
function sortName( $a, $b )
{
return ($a["name"]<$b["name"] ? -1 : 1);
}
$Addresses = array(
array("name"=>"David Perry", "address"=>"21 Jump
St."),
...
);
$Columns = array(
array("name"=>"fullname", "title"=>"Name",
"sortfunc"=>"sortName"),
array("name"=>"street", "title"=>"Address")
);
$smarty->assign( "Addresses", $Addresses );
$smarty->assign( "Columns", $Columns );
$smarty->display( "addresses.tpl" );
>>> addresses.tpl:
{listview url="addresses.php" outputdata=$Addresses columndata=$Columns}
{section name="user" loop=$listviewOutput}{* <-- important *}
<tr class="{cycle values="odd,even"}">
<td>{$listviewOutput[user].name}</td>
<td>{$listviewOutput[user].address}</td>
</tr>
{/section}
{/listview}
This has already come in handy at work. I hope it makes your life
easier, too.
Keep well,
David