laravel-admin自定義列表按鈕,並帶swal彈窗效果

use Encore\Admin\Facades\Admin;
/** * Make a grid builder. * * @return Grid */
    protected function grid() { $grid = new Grid(new Order); $grid->model()->where(function ($query) { $query->where([ ['start_time', '<', date("Y-m-d", time())], ])->orwhere([ ['start_time', '>=', date("Y-m-d", time())], ]); })->whereIn('state', [6, 7]); $grid->model()->orderByDesc('created_at'); $grid->orderid('訂單ID'); $grid->user()->wx_name('用戶名'); $grid->user()->phone('用戶手機號'); $grid->aircraft()->name('地點'); $grid->aircraftType()->name('入口'); $grid->type('服務類別')->display(function ($cert) { $certArr = [ 1 => ['text' => '休息室', 'color' => 'danger'],
                2 => ['text' => '快速安檢', 'color' => 'success'],
                3 => ['text' => '快速登機', 'color' => 'primary'],
                4 => ['text' => '代辦值機', 'color' => 'info'],
                5 => ['text' => '接機', 'color' => 'success'],
                6 => ['text' => '送機', 'color' => 'info'],
                8 => ['text' => 'VIP', 'color' => 'danger'],
                9 => ['text' => 'VVIP', 'color' => 'primary'], ]; return "<span class='label label-" . $certArr[$cert]['color'] . "'>" . $certArr[$cert]['text'] . "</span>"; }); $grid->amount('金額')->sortable(); $grid->updated_at('預定時間')->sortable(); $grid->state('狀態')->display(function () { $state = $this->state; $certArr = [ 6 => ['text' => '待退款', 'color' => 'danger'],
                7 => ['text' => '退款成功', 'color' => 'success'], ]; return "<span class='label label-" . $certArr[$state]['color'] . "'>" . $certArr[$state]['text'] . "</span>"; }); $grid->disableCreateButton(); $this->script = <<<EOT $('.grid-row-pass').unbind('click').click(function() { var id = $(this).data('id'); swal({ title: "確認經過該用戶的退款申請嗎?", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "確認", showLoaderOnConfirm: true, cancelButtonText: "取消", preConfirm: function() { $.ajax({ method: 'get', url: '/admin/refund/' + id, success: function (data) { console.log(data); $.pjax.reload('#pjax-container'); if(data.code){ swal(data.msg, '', 'success'); }else{ swal(data.msg, '', 'error'); } } }); } }); }); EOT; Admin::script($this->script); $grid->filter(function ($filter) { $filter->disableIdFilter(); $filter->equal('orderid', '訂單ID'); $filter->equal('aircraft_id', '地點')->select('data-aircrafts'); $filter->equal('type', '服務類別')->radio([ '' => '所有',
                8 => 'VIP',
                9 => 'VVIP', ]); $filter->between('created_at', '時間')->datetime(); }); $grid->actions(function (Grid\Displayers\Actions $actions) { $actions->disableView(); $actions->disableEdit(); $actions->disableDelete(); if ($actions->row->state == 6) { $actions->append("<div class='mb-5'><a class='btn btn-xs action-btn btn-primary grid-row-pass' data-id='{$actions->getKey()}'><i class='fa fa-check'></i>退款</a></div>"); } }); return $grid; }

ajax中請求的路由裏處理確認後的邏輯,重點代碼適當加粗了,請仔細查看ajax

 

實現效果app

 

相關文章
相關標籤/搜索