如何在Yii中添加搜索和过滤条件 [英] How to add Search and Filter Criteria in Yii

查看:236
本文介绍了如何在Yii中添加搜索和过滤条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 ````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;

... / model / supermarkets:

 <?php 

namespace app \models;

使用yii\db\ActiveRecord;

class超级市场延伸ActiveRecord
{


$ b}

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

 < ;?php 
使用yii \helpers\Html;
使用yii\widgets\LinkPager;
?>
< h1>超级市场< / h1>
< ul>
<?php
$ array =(array)$ supermarkets;

函数build_table($ array){

//启动表

$ html ='< table class =altrowstableid = alternatecolor>';

//标题行

$ html。='< tr>';

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

$ html。='th>'。 $键。 < /第>;

}

$ html。='< / tr>';

//数据行

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

$ html。='< tr> ;';

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

$ html。='< td>'。 $ value2。 < / TD>;

}

$ html。='< / tr>';



//完成表并返回它

$ html。='< / table>';

返回$ html;

}



echo build_table($ array);

?>

.... Controllers / SupermarketsController:

 <?php 

namespace app \controllers;

使用yii\web\Controller;

使用yii\data\Pagination;
使用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- >限制)
- > all();

return $ this-> render('index',[
'supermarkets'=> $ supermarkets,
'pagination'=> $ pagination,
]);




$ b $ p
$ b

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

解决方案

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

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



<$ p $ ('zii.widgets.grid.CGridView',数组(
'id'=>'超级市场网格');< code><?php
$ this-> ,
'dataProvider'=> $ model-> search(),
'filter'=> $ model,
'columns'=> array(
'名称',
'位置',
'电话',
'传真',
'网站'
),
));
?>

在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);

返回新的CActiveDataProvider(get_class($ this),array(
'criteria'=> $ criteria,
'sort'=> array(
' (
'pageSize'=> 20
),
),
'pagination'=>'name ASC',
) );

$ / code $ / $ p

控制器/超级市场控制器:

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

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


I am developing a database application using Yii Framework. I am reading tables from MySQL database and displaying them to the user. I need the user to be able to filter the fields in the table or search for a certain value.

For example, I have a table named "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;

.../model/supermarkets:

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Supermarkets extends ActiveRecord
{



}

.../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);

?>

....Controllers/SupermarketsController:

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

I need the user to be able to filter the table or search its fields by one or more attribute. How can I do this?

解决方案

Instead of trying to reinvent the wheel you can simply use the Yii provided CGridView widget. It has the sorting and filtering functionality. Check the documentation and you will find that there are lot of configurations you can play with. Following code snippet uses minimum configuration.

.../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'
        ),
    ));
    ?>

Implement search() function in Supermarkets model.

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

Controllers/SupermarketsController:.

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

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

这篇关于如何在Yii中添加搜索和过滤条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆