如何在 Yii 中添加搜索和过滤条件

use*_*056 1 php mysql search filter yii

我正在使用 Yii 框架开发数据库应用程序。我正在从 MySQL 数据库中读取表并将它们显示给用户。我需要用户能够过滤表中的字段或搜索某个值。

例如,我有一个名为“supermarkets”的表:

CREATE TABLE IF NOT EXISTS `supermarkets` (
  `Name` varchar(71) NOT NULL,
  `Location` varchar(191) DEFAULT NULL,
  `Telephone` varchar(68) DEFAULT NULL,
  `Fax` varchar(29) DEFAULT NULL,
  `Website` varchar(24) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

.../型号/超市:

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Supermarkets extends ActiveRecord
{



}
Run Code Online (Sandbox Code Playgroud)

.../views/supermarkets/index.php:

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
$array = (array) $supermarkets;

function build_table($array){

    // start table

    $html = '<table class="altrowstable" id="alternatecolor">';

    // header row

    $html .= '<tr>';

    foreach($array[0] as $key=>$value){

            $html .= '<th>' . $key . '</th>';

        }

    $html .= '</tr>';

    // data rows

    foreach( $array as $key=>$value){

        $html .= '<tr>';

        foreach($value as $key2=>$value2){

            $html .= '<td>' . $value2 . '</td>';

        }

        $html .= '</tr>';

    }

    // finish table and return it

    $html .= '</table>';

    return $html;

}



echo build_table($array);

?>
Run Code Online (Sandbox Code Playgroud)

....控制器/超市控制器:

<?php

namespace app\controllers;

use yii\web\Controller;

use yii\data\Pagination;
use app\models\Supermarkets;
class SupermarketsController extends Controller
{
    public function actionIndex()
    {
        $query = supermarkets::find();

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $supermarkets = $query->orderBy('Name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return $this->render('index', [
            'supermarkets' => $supermarkets,
            'pagination' => $pagination,
        ]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要用户能够通过一个或多个属性过滤表格或搜索其字段。我怎样才能做到这一点?

cha*_*era 5

您可以简单地使用 Yii 提供的CGridView 小部件,而不是试图重新发明轮子。它具有排序和过滤功能。查看文档,您会发现有很多配置可以玩。以下代码片段使用最低配置。

.../views/supermarkets/index.php:

    <?php
    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'supermarkets-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'name',
            'location',
            'telephone',
            'fax',
            'website'
        ),
    ));
    ?>
Run Code Online (Sandbox Code Playgroud)

在 Supermarkets 模型中实现 search() 函数。

public function search()
{

    $criteria=new CDbCriteria;

    $criteria->compare('name',$this->name,true);
    $criteria->compare('location',$this->location,true);
    $criteria->compare('telephone',$this->telephone,true);
    $criteria->compare('fax',$this->fax,true);
    $criteria->compare('website',$this->website,true);

    return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'name ASC',
        ),
        'pagination'=>array(
            'pageSize'=>20
        ),
    ));
}
Run Code Online (Sandbox Code Playgroud)

控制器/超市控制器:.

public function actionIndex() {
    $model =new Supermarkets('search');
    if(isset($_GET['Supermarkets']))
        $model->attributes =$_GET['Supermarkets'];

    return  $this->render('index', array('model'=>$model));
}
Run Code Online (Sandbox Code Playgroud)