0

我目前正在 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

/控制器/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,
    ));
}
4

1 回答 1

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 -> 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',
        ),
    ),
)); ?>
于 2013-05-16T15:10:01.710 回答