gii生成后,index页面,添加按钮旁边会有搜索按钮,如果需要的话可以根据业务需求修改.
现在的业务需求是如图
现在修改视图中的index,部分代码如下
<div class="view-professor-index">
<p style="width: 100%;height: 22px;margin-bottom: 20px">
<?= Html::a('添加学生', ['create'], ['class' => 'btn btn-success pull-right', 'style' => 'padding:6px 26px']) ?>
<?php Html::a('搜索', '#', ['class' => 'btn btn-info search-button']) ?>
</p>
<div class="professor-index">
<div class="search-form">
<?= $this->render('_search', ['model' => $searchModel]); ?>
</div>
<?php ........
视图中_search中主要代码为
div class="form-student-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<div class="input-group col-md-12">
<?= $form->field($model, 'searchAll', ['addon' => ['prepend' => ['content' => '搜索']]])
->textInput(['maxlength' => true, 'placeholder' => ' 可搜索内容id,标题,作者'])->label(false); ?>
<span class="input-group-btn" style="vertical-align: top">
<?= Html::submitButton('搜索', ['class' => 'btn btn-primary', 'style' => 'padding:6px 40px']) ?>
</span>
</div>
......
searchModel里操作如下
lass StudentSearch extends ViewStudent
{
public $searchAll;//定义变量接收要搜索的字段
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id'], 'integer'],
[['role', 'organization_ids', 'nickname', 'mobile'], 'safe'],
['searchAll', 'safe'],//验证
];
}
......
......
//接收查询内容
$query->orFilterWhere([
'id' => $this->searchAll
]);
$query->orFilterWhere([
'like', 'nickname', $this->searchAll
]);
$query->orFilterWhere([
'like', 'mobile', $this->searchAll
]);
效果如图