ActiveForm 搜索表单提交后 搜索项就消失了 [ 2.0 版本 ]
如上图 选择搜索的选项 提交后原本的搜索选项就消失了 如下图
视图代码如下:
<?php $form = ActiveForm::begin([
'action' => ['goods-analyze'],
'method' => 'get',
'options' => ['class' => 'form-search'],
'fieldConfig' => [
'options' => ['class' => 'form-group col-xs-4'],
'template' => "{label}\n<div class=\"control-div col-sm-8\">{input}</div>",
'labelOptions' => ['class' => 'control-label col-sm-4'],
],
]); ?>
<?= $form->field($model, 'start_date')->widget(DatePicker::classname(), [
'options' => ['placeholder' => '选择日期'],
'readonly' => true,
'pluginOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd',
]
])->label('开始时间') ?>
<?= $form->field($model, 'end_date')->widget(DatePicker::classname(), [
'options' => ['placeholder' => '选择日期'],
'readonly' => true,
'pluginOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd',
]
])->label('结束时间') ?>
<?= $form->field($model, 'goods_name')->textInput(['prompt'=>'输入商品名称'])->label('品名'); ?>
<?= $form->field($model, 'shop_name')->dropDownList(\app\models\GoodsAnalyze::getShopNameList(),['prompt'=>'选择店铺名称'])->label('店铺'); ?>
<?= $form->field($model,'quarter')->radioList(['1'=>'本季度','2'=>'上季度','3'=>'上周','4'=>'本周'])->label('快捷选项')?>
<div class="form-group left-margin">
<?= Html::submitButton('搜索', ['class' => 'btn btn-primary left']) ?>
<?= Html::resetButton('重置', ['class' => 'btn btn-primary left']) ?>
</div>
<?php ActiveForm::end(); ?>
EizoPeter 补充于 2019-05-15 11:21
start_date,end_date,quarter 并不是数据表中的字段 为了根据不同时间维度 对create_date 进行搜索的
EizoPeter 补充于 2019-05-15 14:12
@花椒
var_dump(Yii::$app->request->queryParams);这个打印出来是
直接改成了$dataProvider = $model->search(Yii::$app->request->queryParams);搜索不起作用了
最佳答案
-
楼上说的对,需要在search model里增加rules规则,例:
public function rules() { return [ [['start_date', 'end_date', 'goods_name','shop_name','quarter'], 'safe'], ]; }
共 5 条回复@花椒 这个是控制器里面的代码
public function actionGoodsAnalyze(){$model = new GoodsAnalyze(); $data = Yii::$app->request->queryParams; if($data){ $dataProvider = $model->search($data['GoodsAnalyze']); }else{ $dataProvider = $model->search($data); } return $this->render('goods-analyze',[ 'dataProvider' => $dataProvider, 'model' => $model, ]); }
@EizoPeter
问题就出在你
if($data){$dataProvider = $model->search($data['GoodsAnalyze']);//$data['GoodsAnalyze']这是什么鬼。。。
直接
$model = new GoodsAnalyze();
$dataProvider = $model->search(Yii::$app->request->queryParams);
return $this->render('goods-analyze',['dataProvider' => $dataProvider, 'model' => $model, ]);
另外,你GoodsAnalyze里得有
$this->load($params);这句你应该知道吧?@花椒
model里面有$this->load($params);的Yii::$app->request->queryParams 打印出来是
array (size=1)
'GoodsAnalyze' =>array (size=5) 'start_date' => string '2019-05-01' (length=10) 'end_date' => string '2019-05-15' (length=10) 'goods_name' => string '' (length=0) 'shop_name' => string '' (length=0) 'quarter' => string '' (length=0)'
我改成了 $dataProvider = $model->search(Yii::$app->request->queryParams); 这个写法 搜索用不了
其他 1 个回答
EizoPeter
注册时间:2018-08-30
最后登录:2020-06-18
在线时长:34小时12分
最后登录:2020-06-18
在线时长:34小时12分
- 粉丝6
- 金钱2775
- 威望0
- 积分3115