Blog

concrete5のItemListクラスで配列からすぐにページ送りやソートに対応した一覧が作れて便利すぎる件

Posted by admin at 6:55 日時 2013/12/18

メモ程度に。ありがたや〜

concrete5のほんとに良いなと思うところですが、コアクラスの再利用性が高く、管理画面や既存ブロックのコードをちょっと眺めて理解できればだいたいなんとかなります。

// 何か適当な配列があります  $array = array(1,3,5,7,9,11,13);    // ItemListクラスのインスタンスを作成します  Loader::library('item_list');  $itemList = new ItemList;    // ItemListに配列をぶっこみます  $itemList->setItems($array);    // 検索リクエストを取得します  $req = $itemList->getSearchRequest();    // 表示件数とか変更できます  if ($req['numResults'] && Loader::helper('validation/numbers')->integer($req['numResults'])) {  	$itemList->setItemsPerPage($req['numResults']);  }    // DBから取得しているようにページを取得できます  // この時点ですでにページ送りのクエリに対応しています  $pages = $itemList->getPage();    foreach($pages as $cID) {  	$page = Page::getByID($cID); // cIDからページオブジェクトをマッピング  	$title = h($page->getCollectionName());  	echo $title;  }    $form = Loader::helper('form');  $c = Page::getCurrentPage();    // 表示件数を変更するselectを出力してみたり  echo '<form method="get" action="' . h($c->getCollectionPath()) . '">';  echo $form->label('numResults', t('# Per Page'));  ?>  <div class="controls">  	<?php	echo $form->select('numResults', array(  		'10' => '10',  		'25' => '25',  		'50' => '50'  	), $req['numResults'], array('style' => 'width:65px'));?>  </div>  <?php  echo $form->submit('ccm-search-pages', t('Search'), array('style' => 'margin-left: 10px'));  echo '</form>';    // ページネーションもさらっと出力!  $pagination = $itemList->getPagination();  echo $pagination->getPages();

全部ビューに書いてあるようなコードになってますが、実際にはコントローラーに書いてビューには $pages, $req, $pagination を個別に渡してあげましょう。


Share this entry