Last active
October 10, 2016 11:21
-
-
Save jamband/1433813 to your computer and use it in GitHub Desktop.
Yii Framework: CListView and searching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
), | |
)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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', | |
)); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! this is helpfull!
Can you help me add submit button when type finnished!