我目前正在Yii中編寫一個應用程序,在這個應用程序中我想在標題(無論是在導航菜單中還是在其上方)中創建一個搜索框。該搜索框應該能夠從網站的每個部分訪問,並且它應該能夠搜索不同表格的不同列。Yii在導航菜單中搜索
我不知道如何做到這一點,幾乎所有關於它的帖子都涉及到使用網格視圖或擴展(如果可能的話,我想創建沒有擴展名的代碼)。
您是否對搜索代碼的外觀(我應該放在哪個控制器等)有個想法?
- 編輯 -
我仍然不知道如何做到這一點,但我會告訴你我此刻有反正。它不是很多,它很明顯我缺少一些代碼。
/view/layout/main.php:
<?php echo CHtml::form(Yii::app()->createUrl('product/search'), 'get') ?>
<?php echo CHtml::textField('search_key','',array('placeholder' => 'Search')); ?>
<?php echo CHtml::submitButton('Go'); ?>
<?php echo CHtml::endForm() ?>
/view/product/search.php:
//Not sure by any means what to write here, but I'll like a list view populated with the search results
/controllers/productController.php
/**
* Search through model.
*/
public function actionSearch()
{
if(isset($_GET['search_key'])){
$search = $_GET['search_key'];
$model->name = $search;
}
$this -> render('search', array(
'model' => $model,
));
}
/models/Product.php
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
我沒有做到這一點,但我想你將需要:輸入字段,提交按鈕。方法在你的控制器中搜索需要的參數,或默認爲'標題'。然後這個方法創建一個CActiveDataProvider並呈現一個頁面,顯示一個CGridView,該頁面具有TITLE = title找到的POST。 – JorgeeFG