Structures_DataGridたのすぃー。
前にServices_KakakuComにページャを付けようとして、ついでにStructures_DataGridを使用したのだが、なんかとても楽しげだったのでもう少し使ってみた。RendererにSmartyも使うことができるようだったので、早速使用。データの中身はPEARのダウンロードベスト10を使用した。つーか、bindするものさえ変えれば、速攻使えるのがいいね。今回は、面倒なのでArrayを使用したけども、CSV, DB_DataObject, DB, DBQuery(使ったことない。)DBTable(これも使ったことない),Excel, MDB2, RSS, XMLでいけるみたい。Excelは日本語大丈夫なのかな。そのままオブジェクトを渡してあげればいいみたいなので、楽チン。
あ。ちなみにこれはPHP4な書き方ね。
<?php
require_once 'Smarty/Smarty.class.php';
require_once 'Structures/DataGrid.php';
$pearPackages = array(
array('id' => 1, 'package' => 'DB', '# of downloas' => 1880131),
array('id' => 2, 'package' => 'PEAR', '# of downloas' => 1404515),
array('id' => 3, 'package' => 'Net_Socket', '# of downloas' => 1327259),
array('id' => 4, 'package' => 'Mail', '# of downloas' => 1063157),
array('id' => 5, 'package' => 'PHPUnit', '# of downloas' => 1060862),
array('id' => 6, 'package' => 'Net_SMTP', '# of downloas' => 952771),
array('id' => 7, 'package' => 'Archirve_Tar', '# of downloas' => 942372),
array('id' => 8, 'package' => 'Console_Getopt', '# of downloas' => 941070),
array('id' => 9, 'package' => 'Log', '# of downloas' => 785971),
array('id' => 10, 'package' => 'Mail_Mime', '# of downloas' => 531212),
);
$dg =& new Structures_DataGrid(4);
$smarty =& new Smarty;
$result = $dg->bind($pearPackages);
PEAR::isError($result) or $result = $dg->fill(
$smarty);
if (!PEAR::isError($result)) {
// pear.tpl is same as the sample in http://pear.php.net/manual/en/package.structures.structures-datagrid.structures-datagrid-renderer.smarty.php
$smarty->display('pear.tpl');
} else {
$smarty->assign('error', 'DataGrid return PEAR Error');
$smarty->dispay('error.tpl');
}
?>
で、pear.tplの中身は、
<!-- Show paging links using the custom getPaging function -->
{getPaging prevImg="<<" nextImg=">>" separator=" | " delta="5"}
<p>Showing records {$firstRecord} to {$lastRecord}
from {$totalRecordsNum}, page {$currentPage} of {$pagesNum}</p>
<table cellspacing="0">
<!-- Build header -->
<tr>
{section name=col loop=$columnSet}
<th {$columnSet[col].attributes}>
<!-- Check if the column is sortable -->
{if $columnSet[col].link != ""}
<a href="{$columnSet[col].link}">{$columnSet[col].label}</a>
{else}
{$columnSet[col].label}
{/if}
</th>
{/section}
</tr>
<!-- Build body -->
{section name=row loop=$recordSet}
<tr {if $smarty.section.row.iteration is even}bgcolor="#EEEEEE"{/if}>
{section name=col loop=$recordSet[row]}
<td {$columnSet[col].attributes}>{$recordSet[row][col]}</td>
{/section}
</tr>
{/section}
</table>
つーか、pear.tplの中身はPEAR::Manual::Structures_DataGrid_Renderer_Smartyと一緒ね。ソート可能なページャがこんなに簡単にできてしまいます。テンプレートをもう少しいじったら、さらにカッコよくできるかも。
つーか、動いているサンプル載せれってか?レンタルサーバへのインストールがめんどくさいのでかんべんしてください。
今日は、親知らずを抜いてくるので、お家で休養。
Shin Ohno 2003-2012