DateColumn.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-03-21 19:32
  7. */
  8. namespace backend\grid;
  9. use Yii;
  10. use yii\base\Model;
  11. use yii\helpers\Html;
  12. use yii\web\View;
  13. /**
  14. * @inheritdoc
  15. */
  16. class DateColumn extends DataColumn
  17. {
  18. public $headerOptions = ['width' => '120px'];
  19. public $filter = null;
  20. public $format = ['datetime', 'php:Y-m-d H:i'];
  21. public function init()
  22. {
  23. parent::init();
  24. !isset($this->filterInputOptions['elem']) && $this->filterInputOptions['elem'] = 'this';
  25. !isset($this->filterInputOptions['type']) && $this->filterInputOptions['type'] = 'datetime';
  26. !isset($this->filterInputOptions['range']) && $this->filterInputOptions['range'] = '~';
  27. !isset($this->filterInputOptions['format']) && $this->filterInputOptions['format'] = 'yyyy-MM-dd HH:mm:ss';
  28. !isset($this->filterInputOptions['isInitValue']) && $this->filterInputOptions['isInitValue'] = 'false';
  29. !isset($this->filterInputOptions['min']) && $this->filterInputOptions['min'] = '1900-1-1';
  30. !isset($this->filterInputOptions['max']) && $this->filterInputOptions['max'] = '2099-12-31';
  31. !isset($this->filterInputOptions['trigger']) && $this->filterInputOptions['trigger'] = 'focus';
  32. !isset($this->filterInputOptions['show']) && $this->filterInputOptions['show'] = 'false';
  33. !isset($this->filterInputOptions['position']) && $this->filterInputOptions['position'] = 'absolute';
  34. !isset($this->filterInputOptions['zIndex']) && $this->filterInputOptions['zIndex'] = '66666666';
  35. !isset($this->filterInputOptions['showBottom']) && $this->filterInputOptions['showBottom'] = 'true';
  36. !isset($this->filterInputOptions['btns']) && $this->filterInputOptions['btns'] = "['clear', 'now', 'confirm']";
  37. !isset($this->filterInputOptions['lang']) && $this->filterInputOptions['lang'] = ( strpos( Yii::$app->language, 'en' ) === 0 ? 'en' : 'cn' );
  38. !isset($this->filterInputOptions['theme']) && $this->filterInputOptions['theme'] = 'molv';
  39. !isset($this->filterInputOptions['calendar']) && $this->filterInputOptions['calendar'] = 'true';
  40. !isset($this->filterInputOptions['mark']) && $this->filterInputOptions['mark'] = '{}';//json对象
  41. !isset($this->filterInputOptions['ready']) && $this->filterInputOptions['ready'] = 'function(date){}';//匿名函数
  42. !isset($this->filterInputOptions['change']) && $this->filterInputOptions['change'] = 'function(value, date, endDate){}';//匿名函数
  43. !isset($this->filterInputOptions['done']) && $this->filterInputOptions['done'] = 'function(value, date, endDate){}';//匿名函数
  44. $this->filterInputOptions['dateType'] = $this->filterInputOptions['type'];
  45. unset($this->filterInputOptions['type']);
  46. if (!isset($this->filterInputOptions['class'])) {
  47. $this->filterInputOptions['class'] = 'form-control date-time';
  48. }else{
  49. $this->filterInputOptions['class'] .= ' form-control date-time';
  50. }
  51. }
  52. protected function renderFilterCellContent()
  53. {
  54. if (is_string($this->filter)) {
  55. return $this->filter;
  56. }
  57. $model = $this->grid->filterModel;
  58. if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) {
  59. if ($this->grid->filterModel->hasErrors($this->attribute)) {
  60. Html::addCssClass($this->filterOptions, 'has-error');
  61. $error = ' ' . Html::error($this->grid->filterModel, $this->attribute, $this->grid->filterErrorOptions);
  62. } else {
  63. $error = '';
  64. }
  65. return Html::activeTextInput($this->grid->filterModel, $this->attribute, $this->filterInputOptions) . $error;
  66. }
  67. return parent::renderFilterCellContent();
  68. }
  69. }