Pārlūkot izejas kodu

fix: 🐛 admin/store sorting

IceToast 2 gadi atpakaļ
vecāks
revīzija
d905171fcb

+ 21 - 25
app/Http/Controllers/Admin/ShopProductController.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers\Admin;
 
 use App\Models\ShopProduct;
+use App\Traits\DatatablesSortable;
 use Illuminate\Contracts\Foundation\Application;
 use Illuminate\Contracts\View\Factory;
 use Illuminate\Contracts\View\View;
@@ -14,6 +15,8 @@ use Illuminate\Validation\Rule;
 
 class ShopProductController extends Controller
 {
+    use DatatablesSortable;
+
     /**
      * Display a listing of the resource.
      *
@@ -66,23 +69,12 @@ class ShopProductController extends Controller
             'display' => 'required|string|max:60',
         ]);
 
-        $disabled = ! is_null($request->input('disabled'));
+        $disabled = !is_null($request->input('disabled'));
         ShopProduct::create(array_merge($request->all(), ['disabled' => $disabled]));
 
         return redirect()->route('admin.store.index')->with('success', __('Store item has been created!'));
     }
 
-    /**
-     * Display the specified resource.
-     *
-     * @param  ShopProduct  $shopProduct
-     * @return Response
-     */
-    public function show(ShopProduct $shopProduct)
-    {
-        //
-    }
-
     /**
      * Show the form for editing the specified resource.
      *
@@ -116,7 +108,7 @@ class ShopProductController extends Controller
             'display' => 'required|string|max:60',
         ]);
 
-        $disabled = ! is_null($request->input('disabled'));
+        $disabled = !is_null($request->input('disabled'));
         $shopProduct->update(array_merge($request->all(), ['disabled' => $disabled]));
 
         return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!'));
@@ -129,7 +121,7 @@ class ShopProductController extends Controller
      */
     public function disable(Request $request, ShopProduct $shopProduct)
     {
-        $shopProduct->update(['disabled' => ! $shopProduct->disabled]);
+        $shopProduct->update(['disabled' => !$shopProduct->disabled]);
 
         return redirect()->route('admin.store.index')->with('success', __('Product has been updated!'));
     }
@@ -147,19 +139,23 @@ class ShopProductController extends Controller
         return redirect()->back()->with('success', __('Store item has been removed!'));
     }
 
-    public function dataTable()
+    public function dataTable(Request $request)
     {
         $query = ShopProduct::query();
 
+        if ($request->has('order')) {
+            $query = $this->sortByColumn($request->input('order'), $request->input('columns'), $query);
+        }
+
         return datatables($query)
             ->addColumn('actions', function (ShopProduct $shopProduct) {
                 return '
-                            <a data-content="'.__('Edit').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.store.edit', $shopProduct->id).'" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
+                            <a data-content="' . __('Edit') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.store.edit', $shopProduct->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
 
-                           <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.store.destroy', $shopProduct->id).'">
-                            '.csrf_field().'
-                            '.method_field('DELETE').'
-                           <button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
+                           <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.destroy', $shopProduct->id) . '">
+                            ' . csrf_field() . '
+                            ' . method_field('DELETE') . '
+                           <button data-content="' . __('Delete') . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
                        </form>
                 ';
             })
@@ -167,12 +163,12 @@ class ShopProductController extends Controller
                 $checked = $shopProduct->disabled == false ? 'checked' : '';
 
                 return '
-                                <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.store.disable', $shopProduct->id).'">
-                            '.csrf_field().'
-                            '.method_field('PATCH').'
+                                <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $shopProduct->id) . '">
+                            ' . csrf_field() . '
+                            ' . method_field('PATCH') . '
                             <div class="custom-control custom-switch">
-                            <input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$shopProduct->id.'">
-                            <label class="custom-control-label" for="switch'.$shopProduct->id.'"></label>
+                            <input ' . $checked . ' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch' . $shopProduct->id . '">
+                            <label class="custom-control-label" for="switch' . $shopProduct->id . '"></label>
                           </div>
                        </form>
                 ';

+ 4 - 5
themes/default/views/admin/store/index.blade.php

@@ -102,10 +102,12 @@
                         data: 'price'
                     },
                     {
-                        data: 'display'
+                        data: 'display',
+                        sortable: false
                     },
                     {
-                        data: 'description'
+                        data: 'description',
+                        sortable: false
                     },
                     {
                         data: 'created_at'
@@ -121,7 +123,4 @@
             });
         });
     </script>
-
-
-
 @endsection