Yii2-如何使用kartik日期时间选择器来搜索gridview记录 [英] Yii2- How to search gridview records using kartik date time picker
问题描述
我在 yii2
框架中工作。我的索引中有两个
索引控制器
public function actionIndex()
{
$ value = isset($ _ GET ['名称']); ($ value =='search')
{
}
else else
{
$ searchModel = new MeterpingSearch();
$ dataProvider = $ searchModel->搜索(Yii :: $ app-> request-> queryParams);
return $ this-> render('index',[
'searchModel'=> $ searchModel,
'dataProvider'=> $ dataProvider,
]);
}
}
现在我要搜索记录当用户选择开始和结束日期时间并点击搜索按钮时。即我必须传递日期时间值,然后搜索记录。以下是我的搜索模式。
公共函数规则()
{
return [
[['id', 'meter_id'],'integer'],
[['meter_msn','sub_div_code','meter_type','sub_div_name'],'safe'],
];
}
公共函数场景()
{
//在父类中绕过场景()实现
return Model :: scenarios();
public function search($ params)
{
$ query = MeterPing :: find();
//添加应该始终应用于此的条件
$ dataProvider = new ActiveDataProvider([
'query'=> $ query,
] );
$ this->载入($ params);
if(!$ this-> validate()){
//如果在验证失败时不想返回任何记录,请取消注释以下行
// $查询 - 化合物其中( '0 = 1');
返回$ dataProvider;
}
//网格过滤条件
$ query-> andFilterWhere([
'id'=> $ this-> id,
'meter_id'=> $ this-> meter_id,
'meter_ping_date_time'=> $ this-> meter_ping_date_time,
'start_date_time'=> $ this-> start_date_time,
'end_date_time'=> $ this-> end_date_time,
'meter_type'=> $ this-> meter_type,
]);
$ query-> andFilterWhere(['like','meter_msn',$ this-> meter_msn])
- > andFilterWhere(['like','sub_div_code', $ this-> sub_div_code])
- >和FilterWhere(['like','sub_div_name',$ this-> sub_div_name]);
返回$ dataProvider;
}
更新1
按照给出的答案,我试图改变我的代码
< section class =content >
< div class =box>
< div class =box-body>
< p>
<?= Html :: a('Update Record',['create'],['class'=>'btn btn-success'])?>>
< / p>
<?php Pjax :: begin(); ?>
$ b $<?php $ form = ActiveForm :: begin([
'action'=&';'''index'],
'method'=>'get' ,
'options'=> [
'data-pjax'=> 1
],
]); ?>
< div class =div1style =float:left; text-align:right; width:25%;>
< span style =padding-bottom:10px; border-bottom:1px solid black>
<?php
echo DateTimePicker :: widget([
'model'=> $ searchModel,
'attribute'=>'start_date_time',
'options'=> ['placeholder'=>'Select Start Time ...'],
'convertFormat'=> true,
'pluginOptions'=> [
'format'=>'yyyy-dd-MM HH:i:ss',
//'startDate'=&'; 01 - Mar-2014 12:00 AM',
'todayHighlight'=> true
]
]);
?>
< / span>
< / div>
< div class =div2style =float:left; text-align:right; width:25%;>
< span style =padding-bottom:10px; border-bottom:1px solid black>
<?php
echo DateTimePicker :: widget([
'model'=> $ searchModel,
'attribute'=>'end_date_time',
'options'=> ['placeholder'=>'Select End Time ...'],
'convertFormat'=> true,
'pluginOptions'=> [
'format'=>'yyyy-dd-MM HH:i:ss',
//'startDate'=&'; 01 - Mar-2014 12:00 AM',
'todayHighlight'=> true
]
]);
?>
< / span>
< / div>
< p>
<?= Html :: submitButton('Search',['class'=>'btn btn-primary'])?>
< / p>
<?= GridView :: widget([
'dataProvider'=> $ dataProvider,
'filterModel'=> $ searchModel,
' '=> [
['class'=>'yii\grid\SerialColumn'],
//'id',
//'meter_id' ,
'meter_msn',
[
'label'=>'Meter Type',
'value'=> function($ d){
if (is_object($ d))
return $ d-> meter_type;
return' - ';
},
'filter'=> Html :: activeDropDownList($ searchModel,'meter_type',\app\models\Meters :: getMeterTypeValues(),['prompt'=>Meter Type,'class'=>'form-control']),
],
'sub_div_code',
[
'label'=>'Sub Division Name',
'value'=> functio n($ d){
if(is_object($ d-> subdiv))
return $ d-> subdiv-> name;
返回' - ';
},
'filter'=> Html :: activeDropDownList($ searchModel,'sub_div_code',\common\models\SurveyHescoSubdivision :: toArrayList(),['prompt'=>Sub-Div,'class'=>'form-control ']),
],
'meter_ping_date_time',
'start_date_time',
'end_date_time',
['class'=> 'yii\grid\ActionColumn'],
],
]); ?>
<?php ActiveForm :: end(); ?>
<?php Pjax :: end(); ?>
< / div>
< / div>
< / section>
动作索引
$ searchModel = new MeterpingSearch();
$ dataProvider = $ searchModel->搜索(Yii :: $ app-> request-> queryParams);
return $ this-> render('index',[
'searchModel'=> $ searchModel,
'dataProvider'=> $ dataProvider,
]);
点击按钮后,它没有给我想要的结果。
更新2
我的更新搜寻模式低于
/ **
* @inheritdoc
* /
公共函数规则()
{
return [
[['id','meter_id'],'integer'],
[['meter_msn','sub_div_code','meter_type','sub_div_name','start_date_time','end_date_time'],安全'],
];
$ b / **
* @inheritdoc
* /
公共功能场景()
{
//绕过在父类中的scenario()实现
return Model :: scenarios();
}
/ **
*使用搜索查询创建数据提供程序实例
*
* @param array $ params
*
* @return ActiveDataProvider
* /
公共函数搜索($ params){
$ query = MeterPing :: find();
//添加应该始终应用于此的条件
$ dataProvider = new ActiveDataProvider([
'query'=> $ query,
] );
$ this->载入($ params);
if(!$ this-> validate()){
//如果在验证失败时不想返回任何记录,请取消注释以下行
// $查询 - 化合物其中( '0 = 1');
返回$ dataProvider;
}
//网格过滤条件
$ query-> andFilterWhere([
'id'=> $ this-> id,
'meter_id'=> $ this-> meter_id,
'meter_ping_date_time'=> $ this-> meter_ping_date_time,
]);
if($ this-> start_date_time!==''){
$ query-> andFilterWhere(['> =','DATE_FORMAT(start_date_time,%Y- ('Ymd h:i:s',strtotime($ this-> start_date_time))]);%m-%d%h:%i:%s
}
if($ this-> end_date_time!==''){
$ query-> andFilterWhere(['< =','DATE_FORMAT(end_date_time,%Y - %m-%d%h:%i:%s)',date('Ymd h:i:s',strtotime($ this-> end_date_time))]);
$ query-> andFilterWhere(['like','meter_type',$ this-> meter_type]);
返回$ dataProvider;
}
日期时间选取器
< div class =div1style =float:left; text-align:right; width:25%;>
< span style =padding-bottom:10px; border-bottom:1px solid black>
<?php
echo DateTimePicker :: widget([
'model'=> $ searchModel,
'attribute'=>'start_date_time',
'options'=> ['placeholder'=>'选择开始日期时间...'],
'convertFormat'=> true,
'pluginOptions'=> [
'format'=>'yyyy-MM-dd hh:i:ss',
//'startDate'=&'; 01 -Mar-2014 12:00 AM',
'todayHighlight'=> true
]
]);
?>
< / span>
< / div>
< div class =div2style =float:left; text-align:right; width:25%;>
< span style =padding-bottom:10px; border-bottom:1px solid black>
<?php
echo DateTimePicker :: widget([
'model'=> $ searchModel,
'attribute'=>'end_date_time',
'options'=> ['placeholder'=>'选择结束日期时间...'],
'convertFormat'=> true,
'pluginOptions'=> [
'format'=>'yyyy-MM-dd hh:i:ss',
//'startDate'=&'; 01 -Mar-2014 12:00 AM',
'todayHighlight'=> true
]
]);
?>
< / span>
< / div>
我也研究过这个问题,但它没有帮助我。
任何帮助都将得到高度评价。 您应该使用用于创建活动字段的searchModel对象,并使用表单标记包装字段,因为日期选择器也位于索引操作视图上。将html更改为以下内容,我假设您的表单位于 pjax
容器<?php Pjax :: begin(); ?>
<?php $ form = ActiveForm :: begin([
'action'=> ['index'],
'method'=>'get',
'options'=> [
'data-pjax'=> 1
],
]); ?>
< div class =div1style =float:left; text-align:right; width:25%;>
< span style =padding-bottom:10px; border-bottom:1px solid black>
<?php
echo DateTimePicker :: widget([
'model'=> $ searchModel,
'attribute'=>'start_date_time',
'options'=> ['placeholder'=>'Select Start Time ...'],
'convertFormat'=> true,
'pluginOptions'=> [
'format'=>'yyyy-dd-MM HH:i:ss',
//'startDate'=&'; 01 - Mar-2014 12:00 AM',
'todayHighlight'=> true
]
]);
?>
< / span>
< / div>
< div class =div2style =float:left; text-align:right; width:25%;>
< span style =padding-bottom:10px; border-bottom:1px solid black>
<?php
echo DateTimePicker :: widget([
'model'=> $ searchModel,
'attribute'=>'end_date_time',
'options'=> ['placeholder'=>'Select End Time ...'],
'convertFormat'=> true,
'pluginOptions'=> [
'format'=>'yyyy-dd-MM HH:i:ss',
//'startDate'=&'; 01 - Mar-2014 12:00 AM',
'todayHighlight'=> true
]
]);
?>
< / span>
< / div>
< p>
<?= Html :: submitButton('Search',['class'=>'btn btn-primary'])?>
< / p>
<?php ActiveForm :: end(); ?>
然后将您的操作更改为以下内容: 以上将搜索 删除 并更改搜索功能如下 将您的
<$公共职能actionIndex()
{
$ searchModel = new MeterpingSearch(); p $ p>
$ dataProvider = $ searchModel->搜索(Yii :: $ app-> request-> queryParams);
return $ this-> render('index',[
'searchModel'=> $ searchModel,
'dataProvider'=> $ dataProvider,
]);
$ / code> start_date_time
和 end_date_time
与确切输入的日期&如果你想匹配日期部分,只需要在 $ query->和FilterWhere([
)条件中设置列和匹配值的格式搜索模式。
'DATE_FORMAT('%Y-%d-%m',start_date_time)'=> date( Ydm,strtotime($ this-> start_date_time)),
'DATE_FORMAT('%Y-%d-%m',end_date_time)'=> date(Ydm,strtotime($ this- > end_date_time)),
编辑$ c $您希望输入的开始时间和结束时间获取所有开始和结束时间在此范围之间的记录,如果我假设这是正确的话
FIDDLE
正在做,如果这是正确的
'DATE_FORMAT('%Y-%d-%m',start_date_time)'=>日期(Ydm,strtotime($ this-> start_date_time)),
'DAT E_FORMAT('%Y-%d-%m',end_date_time)'=> date(Ydm,strtotime($ this-> end_date_time)),
//网格过滤条件
$ query-> andFilterWhere([
'id '=> $ this-> id,
'meter_id'=> $ this-> meter_id,
'meter_ping_date_time'=> $ this-> meter_ping_date_time,
' meter_type'=> $ this-> meter_type,
]);
$ query-> andFilterWhere(['< =','DATE_FORMAT(start_date_time,%Y-%m-%d)',date('Ym-d',strtotime($ this-> ; START_DATE_TIME))]);
$ query-> andFilterWhere(['> =','DATE_FORMAT(end_date_time,%Y-%m-%d)'),日期('Ym-d',strtotime($ this-> ; END_DATE_TIME))]);
$ query-> andFilterWhere(['like','meter_msn',$ this-> meter_msn])
- > andFilterWhere(['like','sub_div_code',$ this-> ; sub_div_code])
- > andFilterWhere(['like','sub_div_name',$ this-> sub_div_name]);
返回$ dataProvider;
EDIT2
DateTimePicker
格式更新为'format'=> 'yyyy-MM-dd hh:i:ss',
用于结束和开始日期
公共函数搜索($ params){
$ query = MeterPing :: find();
//添加应该始终应用于此的条件
$ dataProvider = new ActiveDataProvider([
'query'=> $ query,
] );
$ this->载入($ params);
if(!$ this-> validate()){
//如果在验证失败时不想返回任何记录,请取消注释以下行
// $查询 - 化合物其中( '0 = 1');
返回$ dataProvider;
}
//网格过滤条件
$ query-> andFilterWhere([
'id'=> $ this-> id,
'meter_id'=> $ this-> meter_id,
'meter_ping_date_time'=> $ this-> meter_ping_date_time,
]);
$ b $ if if($ this-> start_date_time!==''&&!is_null($ this-> start_date_time)){
$ query-> andFilterWhere(['' > =','DATE_FORMAT(start_date_time,%Y-%m-%d%H:%i:%s)',日期('Ymd H:i:s',strtotime($ this-> start_date_time ))]);
if($ this-> end_date_time!==''&&!is_null($ this-> end_date_time)){
$ query-> andFilterWhere([ '< =','DATE_FORMAT(end_date_time,%Y-%m-%d%H:%i:%s)',日期('Ymd H:i:s',strtotime($ this-> end_date_time))]);
$ query-> andFilterWhere(['like','meter_type',$ this-> meter_type]);
返回$ dataProvider;
}
I am working in yii2
framework. I have two kartik date time pickers in my index
view. I want to use them to search the records.
<section class="content">
<div class="box">
<div class="box-body">
<p>
<?= Html::a('Update Record', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<div class="div1" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'name' => 'startTime',
'options' => ['placeholder' => 'Select Start Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-dd-MM HH:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<div class="div2" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'name' => 'endTime',
'options' => ['placeholder' => 'Select End Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-dd-MM HH:i:ss',
// 'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<p>
<?= Html::a('Search Record', ['index','id','name'=>'search'], ['class' => 'btn btn-info']) ?>
</p>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'meter_msn',
[
'label' => 'Meter Type',
'value' => function ($d) {
if(is_object($d))
return $d->meter_type;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'meter_type', \app\models\Meters::getMeterTypeValues(), ['prompt' => "Meter Type", 'class' => 'form-control']),
],
'sub_div_code',
[
'label' => 'Sub Division Name',
'value' => function ($d) {
if(is_object($d->subdiv))
return $d->subdiv->name;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'sub_div_code', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),
],
'meter_ping_date_time',
'start_date_time',
'end_date_time',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>
</div>
GUI
Index Controller
public function actionIndex()
{
$value = isset($_GET['name']);
if($value =='search')
{
}
else
{
$searchModel = new MeterpingSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
Now I want to search the records when a user selects the start and end date time and click on search button. i.e. I have to pass the date time values and then search the records. Below is my search model.
public function rules()
{
return [
[['id', 'meter_id'], 'integer'],
[['meter_msn', 'sub_div_code','meter_type', 'sub_div_name'], 'safe'],
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = MeterPing::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'meter_id' => $this->meter_id,
'meter_ping_date_time' => $this->meter_ping_date_time,
'start_date_time' => $this->start_date_time,
'end_date_time' => $this->end_date_time,
'meter_type' => $this->meter_type,
]);
$query->andFilterWhere(['like', 'meter_msn', $this->meter_msn])
->andFilterWhere(['like', 'sub_div_code', $this->sub_div_code])
->andFilterWhere(['like', 'sub_div_name', $this->sub_div_name]);
return $dataProvider;
}
Update 1
As per answer given I have tried to change my code
<section class="content">
<div class="box">
<div class="box-body">
<p>
<?= Html::a('Update Record', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<div class="div1" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'model' => $searchModel,
'attribute'=>'start_date_time',
'options' => ['placeholder' => 'Select Start Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-dd-MM HH:i:ss',
// 'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<div class="div2" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'model' => $searchModel,
'attribute'=>'end_date_time',
'options' => ['placeholder' => 'Select End Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-dd-MM HH:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<p>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
//'meter_id',
'meter_msn',
[
'label' => 'Meter Type',
'value' => function ($d) {
if(is_object($d))
return $d->meter_type;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'meter_type', \app\models\Meters::getMeterTypeValues(), ['prompt' => "Meter Type", 'class' => 'form-control']),
],
'sub_div_code',
[
'label' => 'Sub Division Name',
'value' => function ($d) {
if(is_object($d->subdiv))
return $d->subdiv->name;
return ' - ';
},
'filter' => Html::activeDropDownList($searchModel, 'sub_div_code', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),
],
'meter_ping_date_time',
'start_date_time',
'end_date_time',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
</div>
</section>
Action Index
$searchModel = new MeterpingSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
After clicking on button it didn't gives me the desired result.
Update 2
My updated search model is below
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'meter_id'], 'integer'],
[['meter_msn', 'sub_div_code','meter_type', 'sub_div_name','start_date_time','end_date_time'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search( $params ) {
$query = MeterPing::find ();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider ( [
'query' => $query ,
] );
$this->load ( $params );
if ( !$this->validate () ) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere ( [
'id' => $this->id ,
'meter_id' => $this->meter_id ,
'meter_ping_date_time' => $this->meter_ping_date_time ,
] );
if ( $this->start_date_time !== '' ) {
$query->andFilterWhere ( [ '>=' , 'DATE_FORMAT(start_date_time,"%Y-%m-%d %h:%i:%s")' , date ( 'Y-m-d h:i:s' , strtotime ( $this->start_date_time ) ) ] );
}
if ( $this->end_date_time !== '' ) {
$query->andFilterWhere ( [ '<=' , 'DATE_FORMAT(end_date_time,"%Y-%m-%d %h:%i:%s")' , date ( 'Y-m-d h:i:s' , strtotime ( $this->end_date_time ) ) ] );
}
$query->andFilterWhere ( [ 'like' , 'meter_type' , $this->meter_type ] );
return $dataProvider;
}
Date Time Pickers
<div class="div1" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'model' => $searchModel,
'attribute'=>'start_date_time',
'options' => ['placeholder' => 'Select Start Date Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-MM-dd hh:i:ss',
// 'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<div class="div2" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'model' => $searchModel,
'attribute'=>'end_date_time',
'options' => ['placeholder' => 'Select End Date Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-MM-dd hh:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
I have also looked into this question, but it didn't help me out.
Any help would be highly appreciated.
You should use the searchModel object to create the active fields, and wrap your fields with form tag, as the datepicker is also on the index action's view. Change the html to the following, i am assuming that your form is inside the pjax
container <?php Pjax::begin (); ?>
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<div class="div1" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'model' => $searchModel,
'attribute'=>'start_date_time',
'options' => ['placeholder' => 'Select Start Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-dd-MM HH:i:ss',
// 'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<div class="div2" style="float: left; text-align: right; width: 25%;">
<span style="padding-bottom: 10px; border-bottom: 1px solid black">
<?php
echo DateTimePicker::widget([
'model' => $searchModel,
'attribute'=>'end_date_time',
'options' => ['placeholder' => 'Select End Time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'yyyy-dd-MM HH:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
</span>
</div>
<p>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</p>
<?php ActiveForm::end(); ?>
Then change your action to the following
public function actionIndex()
{
$searchModel = new MeterpingSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
The above will search the records where the start_date_time
and end_date_time
matches the exact entered date & time in the form fields, if you want to match the date part only you might have to format the column and matching value in your $query->andFilterWhere([
condition inside the search model.
'DATE_FORMAT('%Y-%d-%m',start_date_time)' => date("Y-d-m",strtotime($this->start_date_time)),
'DATE_FORMAT('%Y-%d-%m',end_date_time)' => date("Y-d-m",strtotime($this->end_date_time)),
EDIT
You want the entered start and end time to fetch all the records that have the start and end time between this range if i assumed correctly like this FIDDLE
is doing, if that is correct
remove
'DATE_FORMAT('%Y-%d-%m',start_date_time)' => date("Y-d-m",strtotime($this->start_date_time)),
'DATE_FORMAT('%Y-%d-%m',end_date_time)' => date("Y-d-m",strtotime($this->end_date_time)),
and change the search function like below
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'meter_id' => $this->meter_id,
'meter_ping_date_time' => $this->meter_ping_date_time,
'meter_type' => $this->meter_type,
]);
$query->andFilterWhere(['<=','DATE_FORMAT(start_date_time,"%Y-%m-%d")',date('Y-m-d',strtotime($this->start_date_time))]);
$query->andFilterWhere(['>=','DATE_FORMAT(end_date_time,"%Y-%m-%d")',date('Y-m-d',strtotime($this->end_date_time))]);
$query->andFilterWhere(['like', 'meter_msn', $this->meter_msn])
->andFilterWhere(['like', 'sub_div_code', $this->sub_div_code])
->andFilterWhere(['like', 'sub_div_name', $this->sub_div_name]);
return $dataProvider;
EDIT2
update your DateTimePicker
format to 'format' => 'yyyy-MM-dd hh:i:ss' ,
for both end and start date
then update your search function to the following
public function search( $params ) {
$query = MeterPing::find ();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider ( [
'query' => $query ,
] );
$this->load ( $params );
if ( !$this->validate () ) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere ( [
'id' => $this->id ,
'meter_id' => $this->meter_id ,
'meter_ping_date_time' => $this->meter_ping_date_time ,
] );
if ( $this->start_date_time !== '' && !is_null($this->start_date_time) ) {
$query->andFilterWhere ( [ '>=' , 'DATE_FORMAT(start_date_time,"%Y-%m-%d %H:%i:%s")' , date ( 'Y-m-d H:i:s' , strtotime ( $this->start_date_time ) ) ] );
}
if ( $this->end_date_time !== '' && !is_null($this->end_date_time) ) {
$query->andFilterWhere ( [ '<=' , 'DATE_FORMAT(end_date_time,"%Y-%m-%d %H:%i:%s")' , date ( 'Y-m-d H:i:s' , strtotime ( $this->end_date_time ) ) ] );
}
$query->andFilterWhere ( [ 'like' , 'meter_type' , $this->meter_type ] );
return $dataProvider;
}
这篇关于Yii2-如何使用kartik日期时间选择器来搜索gridview记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!