dcat-admin 統計修改時間選擇器 日期範圍查詢

huangfsd發表於2022-08-01

PHP Dcat-admin 統計卡片 指定日期查詢 日期選擇器

背景,統計卡片只能是下拉框如圖1,需求想要制定日期範圍查詢,查了一圈都沒有,只能自己改個元件

開搞!

1、先要修改掉檢視檔案,原檢視檔案在vendor/dcat/laravel-admin/resources/views/widgets/metrics/card.blade.php;
新建了一個本地檢視resources/views/widgets/metrics/card.blade.php;增加了.datepicker的內容

<div {!! $attributes !!}>
    <div class="card-header d-flex justify-content-between align-items-start pb-0">
        <div>
            @if($icon)
            <div class="avatar bg-rgba-{{ $style }} p-50 m-0">
                <div class="avatar-content">
                    <i class="{{ $icon }} text-{{ $style }} font-medium-5"></i>
                </div>
            </div>
            @endif

            @if($title)
                <h4 class="card-title mb-1">{!! $title !!}</h4>
            @endif

            <div class="metric-header">{!! $header !!}</div>
        </div>

        @if (! empty($subTitle))
            <span class="btn btn-sm bg-light shadow-0 p-0">
                {{ $subTitle }}
            </span>
        @endif

        @if(! empty($dropdown))
        <div class="dropdown chart-dropdown">
            <button class="btn btn-sm btn-light shadow-0 dropdown-toggle p-0 waves-effect" data-toggle="dropdown">
                {{ current($dropdown) }}
            </button>
            <div class="dropdown-menu dropdown-menu-right">
                @foreach($dropdown as $key => $value)
                <li class="dropdown-item"><a href="javascript:void(0)" class="select-option" data-option="{{ $key }}">{{ $value }}</a></li>
                @endforeach
            </div>
        </div>
        @endif

        @if(! empty($datepicker))
        <div class="col-md-7 datepicker">
            <div class="row">
                <div class="col-lg-5 pl-0">
                    <div class="input-group">
                         <span class="input-group-prepend">
                            <span class="input-group-text bg-white"><i class="feather icon-calendar"></i></span>
                        </span>
                        <input autocomplete="off" type="text" name="started" value="{{ $datepicker['start'] }}" class="form-control field_started" required="1" id="asdsss">
                    </div>
                </div>
                <div class="col-lg-5 pl-0">
                    <div class="input-group">
                         <span class="input-group-prepend">
                            <span class="input-group-text bg-white"><i class="feather icon-calendar"></i></span>
                        </span>
                        <input autocomplete="off" type="text" name="ended" value="{{ $datepicker['end'] }}" class="form-control field_ended" required="1" id="_35455a066a22a942">
                    </div>
                </div>
                <div class="col-lg-2 pl-0 pr-0">
                    <button class="btn btn-primary btn-outline-info" data-started="{{ $datepicker['start'] }}" data-ended="{{ $datepicker['end'] }}">
                        <span class="d-none d-sm-inline">查詢</span>
                    </button>
                </div>
            </div>
        </div>
        @endif

    </div>

    <div class="metric-content">{!! $content !!}</div>
</div>

2、修改統計卡片預設呼叫的檢視,如圖,統計卡片類裡增加

protected $view = "widgets.metrics.card";

3、寫個Trait,引入靜態檔案和日期選擇事件,DatepickerTrait.php

trait DatepickerTrait
{

    protected $id = '';

    /**
     * 引入
     * @param $formID
     * @return $this
     */
    public function datepicker($formID)
    {
        $this->id = $formID;

        $this->file();
        $this->script();
        return $this;
    }

    /**
     * 所需要的css js
     */
    protected function file()
    {
        admin_css('/vendor/dcat-admin/dcat/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.css');
        admin_js('/vendor/dcat-admin/dcat/plugins/moment/moment-with-locales.min.js');
        admin_js('/vendor/dcat-admin/dcat/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js');
    }

    /**
     * 繫結時間選擇器點選事件
     */
    protected function script()
    {
        admin_script( <<<JS
Dcat.init('#{$this->id} .datepicker .field_started', function (self, id) { var options = {"format":"YYYY-MM-DD","locale":"zh_CN"};
    var last = $('#{$this->id} .datepicker .field_ended');

    self.datetimepicker(options);
    last.datetimepicker($.extend(options, {useCurrent: false}));
    self.on("dp.change", function (e) {
        last.data("DateTimePicker").minDate(e.date);
        $("#{$this->id} .datepicker .btn-primary").data('started', $(this).val())
    });
    last.on("dp.change", function (e) {
        self.data("DateTimePicker").maxDate(e.date);
        $("#{$this->id} .datepicker .btn-primary").data('ended', $(this).val())
    });
 });
JS);
    }
}

4、引用trait

5、在統計初始化的時候呼叫方法

/**
     * 初始化卡片內容
     */
    protected function init()
    {
        parent::init();

        $this->title('完成情況');
        $this->height(300);
//        $this->height(400);#放棄了腳部
        $this->chartHeight(240);
        $this->chartLabels('比對成功率');
        ......

        #日期選擇開始
        $id = $this->id();
        $this->datepicker($id)
            ->click("#{$id} .datepicker .btn-primary")
            ->addVariables([
            'datepicker' => [
                'start' => date('Y-m-d', strtotime('-7 days')),
                'end' => date('Y-m-d', time()),
            ]
        ]);
        #日期選擇結束
    }

6、效果

列印結果

/**
     * 處理請求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        dd($request->input());
        var_dump($request->get('started'));die;

        switch ($request->get('option')) {
            case '365':
            case '30':
            case '28':
            case '7':
            default:
                // 卡片內容
                $this->withContent(162);
                // 卡片底部
//                $this->withFooter(29, 63, '1d');
                // 圖表資料
                $this->withChart(83);
        }

array:4 [
  "_key" => "App\Admin\Metrics\Home\Completion"
  "_token" => "YFEn3gJde4YWy6frX57JyoFbci7WSY6SIaTb2MW9"
  "ended" => "2022-08-01"
  "started" => "2022-07-25"
]
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章