Browse Source

daterangepicker基本完成

wechat_public_accounts
blobt 5 years ago
parent
commit
f1ebccbbe4
  1. 8
      common/config/main.php
  2. 12
      common/models/Category.php
  3. 15
      common/models/CategorySearch.php
  4. 3
      composer.json
  5. 109
      composer.lock
  6. 3
      console/controllers/InitController.php
  7. 1
      kcadmin/config/main.php
  8. 5
      kcadmin/controllers/SiteController.php
  9. 16
      kcadmin/views/category/_search.php
  10. 112
      kcadmin/views/site/test.php

8
common/config/main.php

@ -7,6 +7,8 @@ return [
'@blobt' => '@vendor/blobt'
],
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'timeZone' => 'Asia/Shanghai',
'language' => 'zh-CN',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
@ -14,5 +16,11 @@ return [
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'formatter' => [
'dateFormat' => 'php:Y-m-d',
'decimalSeparator' => ',',
'thousandSeparator' => ' ',
'currencyCode' => 'RMB',
],
],
];

12
common/models/Category.php

@ -34,6 +34,18 @@ class Category extends \yii\db\ActiveRecord {
return 'category';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['cat_name'], 'required'],
[['icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
[['description'], 'string'],
[['cat_name', 'icon'], 'string', 'max' => 64],
];
}
/**
* {@inheritdoc}
*/

15
common/models/CategorySearch.php

@ -5,19 +5,24 @@ namespace common\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Category;
use yii\helpers\ArrayHelper;
/**
* CategorySearch represents the model behind the search form of `\common\models\Category`.
*/
class CategorySearch extends Category {
public function attributes() {
return ArrayHelper::merge(['created_at_range'], parent::attributes());
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['cat_name','id', 'icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
[['cat_name', 'icon', 'description'], 'safe'],
[['id', 'icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
[['cat_name', 'icon', 'description', 'created_at_range'], 'safe'],
];
}
@ -74,6 +79,12 @@ class CategorySearch extends Category {
->andFilterWhere(['like', 'icon', $this->icon])
->andFilterWhere(['like', 'description', $this->description]);
if ($this->created_at_range) {
$arr = explode(' ~ ', $this->created_at_range);
$start = strtotime($arr[0]);
$end = strtotime($arr[1]) + 3600 * 24;
$query->andFilterWhere(['between', 'created_at', $start, $end]);
}
return $dataProvider;
}

3
composer.json

@ -13,7 +13,8 @@
"require": {
"php": ">=7.1.0",
"yiisoft/yii2": "~2.0.14",
"yiisoft/yii2-bootstrap": "^2.0@dev"
"yiisoft/yii2-bootstrap": "^2.0@dev",
"kartik-v/yii2-date-range": "^1.7@dev"
},
"repositories": {
"asset_packagist": {

109
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f3ddaead9bc7b0924e8f9ccf68c842ff",
"content-hash": "ddd66f4b0e8258bef7d2931651335239",
"packages": [
{
"name": "bower-asset/bootstrap",
@ -322,6 +322,112 @@
],
"time": "2018-02-23T01:58:20+00:00"
},
{
"name": "kartik-v/yii2-date-range",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/kartik-v/yii2-date-range.git",
"reference": "c8ed362fb201d17a1b84d4de3a4c7b0be8e0d7c1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kartik-v/yii2-date-range/zipball/c8ed362fb201d17a1b84d4de3a4c7b0be8e0d7c1",
"reference": "c8ed362fb201d17a1b84d4de3a4c7b0be8e0d7c1",
"shasum": ""
},
"require": {
"kartik-v/yii2-krajee-base": ">=2.0.0"
},
"type": "yii2-extension",
"extra": {
"branch-alias": {
"dev-master": "1.7.x-dev"
}
},
"autoload": {
"psr-4": {
"kartik\\daterange\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Kartik Visweswaran",
"email": "kartikv2@gmail.com",
"homepage": "http://www.krajee.com/"
}
],
"description": "An advanced Yii 2 date range picker input for based on bootstrap-daterangepicker plugin.",
"homepage": "https://github.com/kartik-v/yii2-date-range",
"keywords": [
"bootstrap",
"bootstrap 3",
"date",
"date-range",
"extension",
"range",
"time",
"widget",
"yii2"
],
"time": "2019-05-17T12:35:38+00:00"
},
{
"name": "kartik-v/yii2-krajee-base",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/kartik-v/yii2-krajee-base.git",
"reference": "8aa48bb2e7613db45b3c34f7a9346d7f43b1f4ec"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kartik-v/yii2-krajee-base/zipball/8aa48bb2e7613db45b3c34f7a9346d7f43b1f4ec",
"reference": "8aa48bb2e7613db45b3c34f7a9346d7f43b1f4ec",
"shasum": ""
},
"suggest": {
"yiisoft/yii2-bootstrap": "for Krajee extensions to work with Bootstrap 3.x version",
"yiisoft/yii2-bootstrap4": "for Krajee extensions to work with Bootstrap 4.x version"
},
"type": "yii2-extension",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"kartik\\base\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Kartik Visweswaran",
"email": "kartikv2@gmail.com",
"homepage": "http://www.krajee.com/"
}
],
"description": "Base library and foundation components for all Yii2 Krajee extensions.",
"homepage": "https://github.com/kartik-v/yii2-krajee-base",
"keywords": [
"base",
"extension",
"foundation",
"krajee",
"widget",
"yii2"
],
"time": "2019-05-25T07:25:02+00:00"
},
{
"name": "yiisoft/yii2",
"version": "dev-master",
@ -763,6 +869,7 @@
"minimum-stability": "dev",
"stability-flags": {
"yiisoft/yii2-bootstrap": 20,
"kartik-v/yii2-date-range": 20,
"yiisoft/yii2-debug": 20,
"kint-php/kint": 20,
"yiisoft/yii2-gii": 20

3
console/controllers/InitController.php

@ -26,7 +26,8 @@ class InitController extends Controller {
"cat_name" => "这是一个测试分类{$i}",
"icon" => 'fa',
"icon_type" => 1,
"sort_order" => $i
"sort_order" => $i,
"created_at" => time() - rand(10000, 1000000)
];
$model->save();
}

1
kcadmin/config/main.php

@ -7,7 +7,6 @@ $params = array_merge(
return [
'id' => 'kcadmin',
'basePath' => dirname(__DIR__),
'language' => 'zh-CN',
'controllerNamespace' => 'kcadmin\controllers',
'bootstrap' => ['log'],
'modules' => [],

5
kcadmin/controllers/SiteController.php

@ -7,6 +7,7 @@ use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\CategorySearch;
/**
* Site controller
@ -98,8 +99,10 @@ class SiteController extends Controller {
}
public function actionTest() {
$searchModel = new CategorySearch();
return $this->render('test', [
'name' => 'blobt'
'name' => 'blobt',
'model' => $searchModel
]);
}

16
kcadmin/views/category/_search.php

@ -2,6 +2,7 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use blobt\widgets\DateRangePicker;
/* @var $this yii\web\View */
/* @var $model common\models\CategorySearch */
@ -17,13 +18,13 @@ $form = ActiveForm::begin([
]);
?>
<div class="col-sm-11">
<div class="col-sm-10">
<div class="dataTables_filter">
<?=
$form->field($model, 'id', [
$form->field($model, 'cat_name', [
"template" => "{input}{error}",
"inputOptions" => [
"placeholder" => "检索的id",
"placeholder" => "检索类名",
"class" => "form-control",
],
"errorOptions" => [
@ -33,20 +34,19 @@ $form = ActiveForm::begin([
?>
<?=
$form->field($model, 'cat_name', [
$form->field($model, "created_at_range", [
"template" => "{input}{error}",
"inputOptions" => [
"placeholder" => "检索类名",
"class" => "form-control",
"placeholder" => "创建时间",
],
"errorOptions" => [
"class" => "error-tips"
]
])
])->widget(DateRangePicker::className());
?>
</div>
</div>
<div class="col-sm-1" style="padding: 0;">
<div class="col-sm-2" style="padding: 0;">
<?= Html::submitButton('<i class="fa fa-filter"></i>', ['class' => 'btn btn-default']) ?>
<?= Html::resetButton('<i class="fa fa-eraser"></i>', ['class' => 'btn btn-default']) ?>
</div>

112
kcadmin/views/site/test.php

@ -1,68 +1,68 @@
<?PHP
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use blobt\web\DaterangeBootstrap;
use yii\widgets\ActiveForm;
use blobt\widgets\DateRangePicker;
$this->title = '测试';
$this->params['subtitle'] = '这是一个小小的测试';
$this->params['breadcrumbs'][] = $this->title;
$js = <<<EOT
$('#daterange-btn').daterangepicker(
{
locale: {
'cancelLabel': '清除',
'applyLabel': "确定",
"customRangeLabel": "自定义",
"daysOfWeek": [
"",
"",
"",
"",
"",
"",
""
/* @var $this yii\web\View */
/* @var $model common\models\CategorySearch */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'validateOnType' => true,
]);
?>
<div class="col-sm-11">
<div class="dataTables_filter">
<?=
$form->field($model, 'id', [
"template" => "{input}{error}",
"inputOptions" => [
"placeholder" => "检索的id",
"class" => "form-control"
],
"monthNames": [
"一月",
"二月",
"三月",
"四月",
"五月",
"六月",
"七月",
"八月",
"九月",
"十月",
"十一月",
"十二月"
"errorOptions" => [
"class" => "error-tips"
]
])
?>
<?=
$form->field($model, 'cat_name', [
"template" => "{input}{error}",
"inputOptions" => [
"placeholder" => "检索类名",
"class" => "form-control",
],
},
ranges : {
'今天' : [moment(), moment()],
'昨天' : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'最近7天' : [moment().subtract(6, 'days'), moment()],
'最近30天': [moment().subtract(29, 'days'), moment()],
'本月' : [moment().startOf('month'), moment().endOf('month')],
'上月' : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
},
startDate: moment().subtract(29, 'days'),
endDate : moment()
},
function (start, end) {
$('#daterange-btn span').html(start.format('YYYY-M-D') + ' - ' + end.format('YYYY-M-D'))
}
)
EOT;
"errorOptions" => [
"class" => "error-tips"
]
])
?>
DaterangeBootstrap::register($this);
$this->registerJs($js);
?>
<?=
$form->field($model, "created_at", [
"template" => "{input}{error}",
"errorOptions" => [
"class" => "error-tips"
]
])->widget(DateRangePicker::className());
?>
</div>
</div>
<div class="col-sm-1" style="padding: 0;">
<?= Html::submitButton('<i class="fa fa-filter"></i>', ['class' => 'btn btn-default']) ?>
<?= Html::resetButton('<i class="fa fa-eraser"></i>', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
<button type="button" class="btn btn-default pull-right" id="daterange-btn">
<span>
<i class="fa fa-calendar"></i>日期范围
</span>
<i class="fa fa-caret-down"></i>
</button>
Loading…
Cancel
Save