瀏覽代碼

Log in as list - column sorting

Vijayakrishnan 4 年之前
父節點
當前提交
24f5325bf7
共有 3 個文件被更改,包括 42 次插入7 次删除
  1. 30 0
      app/Helpers/helpers.php
  2. 8 3
      app/Http/Controllers/HomeController.php
  3. 4 4
      resources/views/app/log-in-as.blade.php

+ 30 - 0
app/Helpers/helpers.php

@@ -10,6 +10,36 @@ use App\Models\AppSession;
 //require_once './class.Diff.php';
 use Soundasleep\Html2Text as Html2Text;
 
+if(!function_exists('queryLineExcept')) {
+    function queryLineExcept($except = []) {
+        $params = request()->all();
+        $final = [];
+        foreach ($params as $k => $v) {
+            if(in_array($k, $except) === FALSE) {
+                $final[] = "$k=" . urlencode($v);
+            }
+        }
+        return implode('&', $final);
+    }
+}
+
+if(!function_exists('sortColumnHead')) {
+    function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
+        $currentSortKey = request()->input('sort');
+        $currentDir = request()->input('dir');
+        $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
+        echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
+        if($currentSortKey === $sortKey) {
+            if($currentDir === 'ASC') {
+                echo "&nbsp;&nbsp;↑";
+            }
+            elseif($currentDir === 'DESC') {
+                echo "&nbsp;&nbsp;↓";
+            }
+        }
+    }
+}
+
 if(!function_exists('html2Text')) {
     function html2Text($old, $new){
 

+ 8 - 3
app/Http/Controllers/HomeController.php

@@ -656,9 +656,14 @@ WHERE measurement.label NOT IN ('SBP', 'DBP')
             });
         }
 
-        $pros = $pros->orderBy('name_last', 'asc')
-            ->orderBy('name_first', 'asc')
-            ->paginate(20);
+        if($request->input('sort') && $request->input('dir')) {
+            $pros = $pros->orderBy($request->input('sort'), $request->input('dir'));
+        }
+        else {
+            $pros = $pros->orderBy('name_last', 'asc');
+        }
+            
+        $pros = $pros->paginate(20);
 
         return view('app/log-in-as', ['logInAsPros' => $pros]);
     }

+ 4 - 4
resources/views/app/log-in-as.blade.php

@@ -31,10 +31,10 @@
                     <table class="table table-sm table-striped mb-0">
                         <thead>
                         <tr>
-                            <th class="border-0 pl-2">Name</th>
-                            <th class="border-0">Cell Number</th>
-                            <th class="border-0">Email</th>
-                            <th class="border-0"></th>
+                            <th class="font-weight-normal border-0 pl-2">{{sortColumnHead('/log_in_as', 'Name', 'name_last')}}</th>
+                            <th class="font-weight-normal border-0">{{sortColumnHead('/log_in_as', 'Cell Number', 'cell_number')}}</th>
+                            <th class="font-weight-normal border-0">{{sortColumnHead('/log_in_as', 'Email', 'email_address')}}</th>
+                            <th class="font-weight-normal border-0"></th>
                         </tr>
                         </thead>
                         <tbody>