Yii在导航菜单中搜索

时间:2013-05-16 13:26:41

标签: search menu yii

我目前正在Yii中编写一个应用程序,我想在标题中创建一个搜索框(在导航菜单中或在其上方)。应该能够从站点的每个部分访问此搜索框,并且它应该能够搜索不同表的不同列。

我不知道如何做到这一点,网上关于它的几乎所有帖子都涉及使用网格视图或扩展(如果可能的话,我想创建没有扩展名的代码)。< / p>

您是否知道搜索代码应该如何显示(我应该在哪个控制器等中放置)?

- 编辑 -

我仍然不知道该怎么做,但无论如何我会告诉你我现在拥有的东西。它并不多,而且很明显我错过了一些代码。

/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,
    ));
}

1 个答案:

答案 0 :(得分: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,
    ));
}

/controllers/productController.php - &gt; 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',
        ),
    ),
)); ?>