Skip to content

Instantly share code, notes, and snippets.

@jamband
Last active October 10, 2016 11:21
Show Gist options
  • Save jamband/1433813 to your computer and use it in GitHub Desktop.
Save jamband/1433813 to your computer and use it in GitHub Desktop.
Yii Framework: CListView and searching
<?php
class Hoge extends CActiveRecord
{
...
public function getAll($q)
{
$c = new CDbCriteria();
$c->addSearchCondition('t.fuga', $q);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $c,
'sort' => array(
'defaultOrder' => 't.id DESC',
),
'pagination' => array(
'params' => $q ? compact('q') : null,
),
));
}
<?php
class HogeController extends Controller
{
/**
* Lists all models.
*/
public function actionIndex()
{
$q = trim(mb_convert_kana(Yii::app()->request->getParam('q'), 's', 'UTF-8');
$dataProvider = Hoge::model()->getAll($q);
$this->render('index', compact('q', 'dataProvider'));
}
<div class="form">
<?php echo CHtml::form($this->createUrl($this->route)); ?>
<div class="row">
キーワード検索<br />
( 入力後「Enterキー」を押してください )<br />
<?php echo CHtml::textField('q', $q); ?>
</div><!-- /.row -->
<?php echo CHtml::endForm(); ?>
</div><!-- /.form -->
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view',
)); ?>
@hero2201
Copy link

Thanks for sharing! this is helpfull!
Can you help me add submit button when type finnished!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment