Przeglądaj źródła

增加 Grid\Column::orderable 方法

jqh 5 lat temu
rodzic
commit
593414909a

+ 23 - 0
src/Form.php

@@ -669,6 +669,25 @@ class Form implements Renderable
         return $this;
     }
 
+    /**
+     * Handle orderable update.
+     *
+     * @param int   $id
+     * @param array $input
+     *
+     * @return bool
+     */
+    protected function handleOrderable(array $input = [])
+    {
+        if (array_key_exists('_orderable', $input)) {
+            $input['_orderable'] == 1 ? $this->repository->moveOrderUp() : $this->repository->moveOrderDown();
+
+            return $this->ajaxResponse(__('admin.update_succeeded'));
+        }
+
+    }
+
+
     /**
      * Handle update.
      *
@@ -705,6 +724,10 @@ class Form implements Renderable
         $this->setModel(new Fluent($this->repository->getDataWhenUpdating($this)));
         $this->setFieldOriginalValue();
 
+        if ($response = $this->handleOrderable($data)) {
+            return $response;
+        }
+
         // Handle validation errors.
         if ($validationMessages = $this->validationMessages($data)) {
             if (!$isEditable && !$this->isAjaxRequest()) {

+ 2 - 0
src/Grid/Column.php

@@ -34,6 +34,7 @@ use Illuminate\Support\Str;
  * @method $this qrcode($formatter = null, $width = 150, $height = 150)
  * @method $this downloadable($server = '', $disk = null)
  * @method $this copyable()
+ * @method $this orderable()
  *
  * @method $this limit($limit = 100, $end = '...')
  * @method $this ascii()
@@ -83,6 +84,7 @@ class Column
         'qrcode'       => Displayers\QRCode::class,
         'downloadable' => Displayers\Downloadable::class,
         'copyable'     => Displayers\Copyable::class,
+        'orderable'    => Displayers\Orderable::class,
     ];
 
     /**

+ 56 - 0
src/Grid/Displayers/Orderable.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace Dcat\Admin\Grid\Displayers;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Grid;
+use Dcat\Admin\Grid\Column;
+
+class Orderable extends AbstractDisplayer
+{
+    public function __construct($value, Grid $grid, Column $column, $row)
+    {
+        parent::__construct($value, $grid, $column, $row);
+
+        if (! trait_exists('\Spatie\EloquentSortable\SortableTrait')) {
+            throw new \Exception('To use orderable grid, please install package [spatie/eloquent-sortable] first.');
+        }
+    }
+
+    public function display()
+    {
+        Admin::script($this->script());
+
+        return <<<EOT
+
+<div class="">
+    <a href="javascript:void(0)" class=" font-14 {$this->grid->getGridRowName()}-orderable" data-id="{$this->getKey()}" data-direction="1">
+        <i class="fa fa-hand-o-up fa-fw"></i>
+    </a>
+    <a href="javascript:void(0)" class=" font-14 {$this->grid->getGridRowName()}-orderable" data-id="{$this->getKey()}" data-direction="0">
+        <i class="fa fa-hand-o-down fa-fw"></i>
+    </a>
+</div>
+
+EOT;
+    }
+
+    protected function script()
+    {
+        return <<<JS
+
+$('.{$this->grid->getGridRowName()}-orderable').on('click', function() {
+
+    var key = $(this).data('id');
+    var direction = $(this).data('direction');
+
+    $.post('{$this->getResource()}/' + key, {_method:'PUT', _token:LA.token, _orderable:direction}, function(data){
+        if (data.status) {
+            LA.reload();
+            LA.success(data.message);
+        }
+    });
+});
+JS;
+    }
+}

+ 36 - 0
src/Repositories/EloquentRepository.php

@@ -12,6 +12,7 @@ use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Database\Eloquent\Relations;
 use Illuminate\Support\Str;
+use Spatie\EloquentSortable\Sortable;
 
 abstract class EloquentRepository extends Repository
 {
@@ -240,6 +241,41 @@ abstract class EloquentRepository extends Repository
         return $result;
     }
 
+    /**
+     * Swaps the order of this model with the model 'above' this model.
+     *
+     * @return bool
+     */
+    public function moveOrderUp()
+    {
+        $model = $this->eloquent();
+        if ($model instanceof Sortable) {
+            $model->moveOrderUp();
+
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Swaps the order of this model with the model 'below' this model.
+     *
+     * @return bool
+     */
+    public function moveOrderDown()
+    {
+        $model = $this->eloquent();
+
+        if ($model instanceof Sortable) {
+            $model->moveOrderDown();
+
+            return true;
+        }
+
+        return false;
+    }
+
     /**
      * Destroy data.
      *