php后台搜索功能,Yii中后台搜索功能

后台有时数据多了需要添加搜索功能,我也是初学者,也不知道实现的方式对不对。后台的话我都是用bootstrap生成的,要比Yii本身自动生成的页面好看多了。下面还是说下我是如何简单的实现搜索功能的。

1.视图中添加搜索表单<?php /** @var BootActiveForm $form */

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

'id'=>'searchForm',

'type'=>'search',

)); ?>

<?php echo $form->textFieldRow($customers, 'name', array('class'=>'input-medium', 'prepend'=>'')); ?>

<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'label'=>'搜索','htmlOptions'=>array('id'=>'search_customer'))); ?>

<?php $this->endWidget(); ?>

视图样式如下:

167487dab5c0d4e05e4fcdfceec093d9.png

2.后台数据是以表格形式展示的,用到的是bootstrap里的TbGridView<?php $this->widget('bootstrap.widgets.TbGridView', array(

'id'=>'customers-grid',

'type'=>'bordered ',

'dataProvider'=>$customers->search(),

// 'filter'=>$customers,

'columns'=>array(

'name',

// array('name'=>'customerproduct.subscribe_capital','header'=>'认缴出资额','value'=>'CustomerProduct::showTotal($data->id)'),

array('name'=>'history_buy_amount','value'=>'CustomerProduct::showHistoryMoney($data->id)'),

array('name'=>'amount_on_hand','value'=>'CustomerProduct::showOnhandMoney($data->id)'),

array('name'=>'visit.addtime','header'=>'最近被访问时间','value'=>'Visit::showVisitDate($data->id)'),

array('name'=>'status', 'type'=>'html', 'value'=>'Customer::showStatus($data->status, $data->id)','visible'=> Yii::app()->user->checkAccess('admin')),

array('name'=>'employee_id', 'type'=>'html', 'value'=>'Customer::isDivided($data->employee_id, $data->id)','visible'=> Yii::app()->user->checkAccess('admin')),

array('name'=>'channel.cname', 'header'=>'渠道用户','visible'=> Yii::app()->user->checkAccess('admin')),

那么我们搜索后的数据是如何展示的呢?也就是说怎么查询出来数据,让GridView发生变化呢?

首先看上面代码中的dataProvider,是由model调用search方法获取的,那么我们再追踪到search函数。public function search()

{

// Warning: Please modify the following code to remove attributes that

// should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id', $this->id);

$criteria->compare('username',$this->username,true);

$criteria->compare('name',$this->name,true);

$criteria->compare('gender',$this->gender);

$criteria->compare('age',$this->age);

$criteria->compare('email',$this->email,true);

$criteria->compare('mychat',$this->mychat);

$criteria->compare('telephone',$this->telephone,true);

$criteria->compare('status',$this->status);

$criteria->compare('logintime',$this->logintime,true);

$criteria->compare('channel_id',$this->channel_id,true);

if(isset(Yii::app()->user->name)&&Yii::app()->user->getState('roles')!='admin'){

$this->employee_id= User::model()->find(array('select'=>'id','condition'=>"username='".Yii::app()->user->name."'"))->id;

}

$criteria->compare('employee_id',$this->employee_id,true);

// $criteria->with = array( 'channel');

// $criteria->together = true;

return new CActiveDataProvider($this, array(

'criteria'=>$criteria,

'pagination'=>array(

'pageSize'=>10

),

));

}

注意到上面代码中的compare函数,它是用来生成查询条件的,$criteria->compare('name',$this->name,true);就相当于'where name like "%.$this->name.'%"',好了现在我们应该知道了,只要改变这个查询的条件,就能查询出相应的name为多少的数据。

那么在控制器里我们可以获取填入的查询条件:

//客户列表

public function actionIndex(){

$model=new Customer('search');

$model->unsetAttributes(); // clear any default values

if(isset($_POST['Customer'])){

$model->name=$_POST['Customer']['name'];//这里获取查询的关键字,把它赋给属性name

}

if(isset($_GET['Customer']))

$model->attributes=$_GET['Customer'];

$this->render('index',array(

'customers'=>$model,

));

}

这样的话,最终展示到视图中的就是由这个关键字所查询出的数据,但是结果发现表格中的数据还是没有改变。后来发现还得写一段js代码,在视图(index.php)中:

$(function(){

$('#search_customer').click(function(){

var data=$(this).serialize();

$.fn.yiiGridView.update('customers-grid', {data: data});

});

});

这段js相当于一个ajax请求,当点击搜索后,post给控制器Customer/index一个参数,在控制器里获取并赋给model的属性name,从而改变search函数中查询条件,即'where name like "%'.$this->name.'%"',最终获取的是name为这个关键字的数据,展示到视图就是查询出来的数据了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值