yii gridview实现时间段筛选功能

时间:2021-08-03 08:26:25

yii gridview功能强大,但是时间筛选比较麻烦,与数据库的存储格式有关,本文的时间格式是date类型

那么问题来了,yii只提供关于时间的text搜索格式,就是只能查找精确日期比如2017-8-10。万能的客户说这样不行,我要搜索时间段!我只要一个输入框!我要自动提交!

yii gridview实现时间段筛选功能 yii gridview实现时间段筛选功能

注意要点:

1.首先要在gridview中引入相关js,实现双日期,这里选择了jquery.daterangepicker.js,简单大方(缺点:不能选择年份,需要手动点击,我这里不会大幅度跨年份,可用)

2.要在searchmodel里面对数据进行处理,进行时间查询

3.坑:选择日期后,输入框没有光标,需要二次点击,然后回车才能实现数据刷新,与原装gridview体验相差较大

4.梯:在检测到输入日期数据后,使用jq模拟回车提交动作,完美实现了类似gridview的原装体验,丝般顺滑

view中

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
 
//use yii\web\view;
use kartik\grid\gridview;
use yii\bootstrap\html;
use common\helps\arrayhelper;
use yii\helpers\url;
 
//引入时间段js,这里使用了jquery.daterangepicker.js
$this->registercssfile('/plugins/datep/css/daterangepicker.css');
$this->registerjsfile('/plugins/datep/js/moment.min.js');
$this->registerjsfile('/plugins/datep/js/jquery.daterangepicker.js');
$this->registerjsfile('/plugins/datep/js/demo.js');
?>
 
<body class="gray-bg">
  <div class="wrapper wrapper-content animated fadeinright">
    <div class="row">
      <div class="col-sm-12">
        <div class="ibox float-e-margins">
           <?= backend\widgets\titleback::widget(['title'=>'记录管理']) ?>
           
          <div class="ibox-content"
            
          <?php
                   
            echo gridview::widget([
                'dataprovider' => $dataprovider,
                'filtermodel' => $searchmodel,
     
                'columns' => [
                  
                  ['class' => 'yii\grid\serialcolumn'],
                  ['class' => 'yii\grid\checkboxcolumn'],
                  'title',
                  
                  [
                    
                        'label'=>'下发时间',
                        'attribute'=>'issued',
                     'value' => function ($data) {
                      return arrayhelper::get_date_time($data->issued);
                    },                   
                  ],
                ]
             ]);
          
          ?>
            </div>
            </div>
          </div>
        </div>
      </div>
 
    </div>
  </div>
</body>

demo.js放在最后说,先说patentdatabdsearch  对输入框发送过来的数据进行处理,时间段查询数据库

?
1
2
3
4
5
//时间段筛选
    if($this->issued){
      $time= explode('~', $this->issued);
      $query->andfilterwhere(['between', 'patent_data.issued', $time[0],$time[1]]);
    }

demo.js   实现数据检测,模拟回车操作

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
$(function(){
  
  /*
  define a new language named "custom"  插件设置
  */
 
  $.daterangepickerlanguages['custom'] =
  {
    'selected': 'choosed:',
    'days': 'days',
    'apply': 'close',
    'week-1' : 'mon',
    'week-2' : 'tue',
    'week-3' : 'wed',
    'week-4' : 'thu',
    'week-5' : 'fri',
    'week-6' : 'sat',
    'week-7' : 'sun',
    'month-name': ['january','february','march','april','may','june','july','august','september','october','november','december'],
    'shortcuts' : 'shortcuts',
    'past': 'past',
    '7days' : '7days',
    '14days' : '14days',
    '30days' : '30days',
    'previous' : 'previous',
    'prev-week' : 'week',
    'prev-month' : 'month',
    'prev-quarter' : 'quarter',
    'prev-year' : 'year',
    'less-than' : 'date range should longer than %d days',
    'more-than' : 'date range should less than %d days',
    'default-more' : 'please select a date range longer than %d days',
    'default-less' : 'please select a date range less than %d days',
    'default-range' : 'please select a date range between %d and %d days',
    'default-default': 'this is costom language'
  };
  
  
  //下面设置称自己的输入框选择器
  $("input[name='patentdatabdsearch[issued]']").daterangepicker(
  {
     //时间段的类型设置,这里是输入框时间段以~分隔,选择时间后自动消失弹出框
     separator : ' ~ ',
     autoclose: true
  }).bind('datepicker-change',function(e,r)
  {
    try
    {
      console.log(r);
            //重要:如果检测有输入值了,就在输入框显示光标,或者模拟回车事件,自动提交,像gridview原生功能
              //不添加下面的代码,将无法自动提交,
            var issued=$("input[name='patentdatabdsearch[issued]']").val();
            console.log(issued);
            if(issued){
              //输入之后显示光标
              //$("input[name='patentdatabdsearch[issued]']").focus();
                //模拟回车操作,输入后自动提交,刷新数据,一定要设置时间计数器,否则将无法提交
 
              settimeout(function(){
                e = jquery.event("keydown");
                e.keycode = 13; //enter key
                jquery("input[name='patentdatabdsearch[issued]']").trigger(e);
 
              },100);
            }
    }catch(e){}
  });
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/tystudy/p/7365782.html