2013-05-16 48 views
0

我目前正在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, 
    )); 
} 
+0

我沒有做到這一點,但我想你將需要:輸入字段,提交按鈕。方法在你的控制器中搜索需要的參數,或默認爲'標題'。然後這個方法創建一個CActiveDataProvider並呈現一個頁面,顯示一個CGridView,該頁面具有TITLE = title找到的POST。 – JorgeeFG

回答

4

可以執行這樣的:

查找#mainmeu/protected/views/layout/main.php

<div id="mainmenu"> 
    <div style="width: 80%;float: right"> 
     <?php $this->widget('zii.widgets.CMenu',array(
      'items'=>array(
       array('label'=>'home', 'url'=>array('/site/index')), 
       array('label'=>'about', 'url'=>array('/site/page', 'view'=>'about')), 
       array('label'=>'contact', 'url'=>array('/site/contact')), 
      ), 
     )); ?> 
    </div> 

    <div style='float: left;direction: rtl; color: #ffffff; margin: 5px 0 0 5px; font-size: 13px'> 
     <?php echo CHtml::form(Yii::app()->createUrl('product/search'),'get') ?> 
      <?php echo CHtml::textField('search_key', 'search') ?> 
      <?php echo CHtml::submitButton(); ?> 
     <?php echo CHtml::endForm() ?> 
    </div> 
</div><!-- mainmenu --> 

編輯:

/models/Product.php:

public function search() 
{ 
    $criteria=new CDbCriteria; 
    $criteria->compare('name',$this->name,true); 
    return new CActiveDataProvider($this, array(
     'criteria'=>$criteria, 
    )); 
} 

/控制器/ productControlle r.php - > actionSearch():

public function actionSearch() 
{ 
    $model = new Product('search'); 
    $model->unsetAttributes(); 
    if(isset($_GET['search_key'])) 
     $model->name = $_GET['search_key'];  

    $this -> render('search', array(
     'model' => $model, 
    )); 
} 

/view/product/search.php:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'product-grid', 
    'dataProvider'=>$model->search(), 
    //'filter'=>$model, 
    'columns'=>array(
     'name', 
     array(
      'class'=>'CButtonColumn', 
     ), 
    ), 
)); ?> 
+0

好吧,我知道我想要它的位置(你指出的那個地方),我只是非常懷疑我的搜索代碼應該如何看待? – Deram

+0

只需寫一個'html表單'。有什麼問題嗎? – msoa

+0

我看不到如何寫一個HTML表單會做伎倆?我從來沒有在搜索框之前,所以也許我只是看到它太複雜的方式。你將如何編寫這個html表單來重定向到包含搜索結果列表視圖的頁面? – Deram