This article mainly introduces in detail the time period filtering function of yii gridview, an input box, and automatic submission function. It has certain reference value. Interested friends can refer to it
yii Gridview is powerful, but time filtering is troublesome, and it is related to the storage format of the database. The time format in this article is date type
. Then the problem comes, Yii only provides text search format about time, that is, it can only find accurate The date is such as 2017-8-10. Almighty's customer said this won't work, I want to search for a time period! I just want an input box! I want to submit automatically!
Points to note:
1. First, introduce relevant js into the gridview to implement double dates. Here we chose jquery.daterangepicker.js , simple and generous (disadvantages: you cannot select the year, you need to click manually, I will not greatly span the years, it is available)
2. To process the data in the search model, perform time query
3. Pitfall: After selecting the date, the input box has no cursor, and you need to click twice and then press Enter to refresh the data, which is quite different from the original gridview experience.
4. Ladder: When the input date data is detected Finally, use jq to simulate the carriage return submission action, which perfectly realizes the original experience similar to gridview, silky smooth
view中
<?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"> <p class="wrapper wrapper-content animated fadeInRight"> <p class="row"> <p class="col-sm-12"> <p class="ibox float-e-margins"> <?= backend\widgets\TitleBack::widget(['title'=>'记录管理']) ?> <p 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); }, ], ] ]); ?> </p> </p> </p> </p> </p> </p> </p> </body>
demo.js is put at the end, let’s talk about PatentDataBdSearch first. The data sent from the input box is processed, and the database is queried during the time period
//时间段筛选 if($this->issued){ $time= explode('~', $this->issued); $query->andFilterWhere(['between', 'patent_data.issued', $time[0],$time[1]]); }
demo.js Implements data detection and simulates the carriage return operation
$(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){} }); });
Related recommendations:
Yii implements the method of adding default values to model
The above is the detailed content of Yii gridview implements time period filtering function. For more information, please follow other related articles on the PHP Chinese website!