From 5064042929edc9d2ef08ed529b46177bc2b5a321 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Wed, 28 Aug 2024 14:21:00 +0300 Subject: [PATCH] fix filter --- examples/simple.php | 33 ++++++++++++++++----- src/Filter/Filter.php | 6 ++-- src/Filter/InputFilter.php | 13 +-------- src/Filter/SelectFilter.php | 2 +- src/JasonTable.php | 58 ++++++++++++++++++------------------- src/ListJsonTable.php | 34 +++++++++++++++------- 6 files changed, 83 insertions(+), 63 deletions(-) diff --git a/examples/simple.php b/examples/simple.php index 09ec7a3..1785c7e 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -2,6 +2,8 @@ require_once "../vendor/autoload.php"; +use Itguild\Tables\Filter\InputFilter; +use Itguild\Tables\Filter\SelectFilter; use Itguild\Tables\ListJsonTable; $json = file_get_contents('simple.json'); @@ -10,32 +12,49 @@ $table = new ListJsonTable($json); $table->columns([ "created_at" => [ "format" => "date:Y-m-d", - 'filter' => ["input" => "date"] + 'filter' => [ + 'class' => InputFilter::class, + 'param' => 'date' + ] ], 'description' => [ "format" => "html", "style" => ["width" => "300px"], - "filter" => ["input" => ["radio" => ["hello", "bye"]]], + "filter" => [ + 'class' => InputFilter::class, + 'param' => 'text' + ], "value" => function ($cell) { return "$cell"; } ], 'description2' => [ - "filter" => ["input" => "text"], + "format" => "html", + "filter" => [ + 'class' => SelectFilter::class, + 'param' => ['black', 'red', 'green', 'blue', 'yellow'] + ], ], 'status' => [ "format" => "integer", - "filter" => ["select" => getStatusLabel()], + "filter" => [ + 'class' => SelectFilter::class, + 'param' => getStatusLabel() + ], "value" => function ($cell) { return getStatusLabel()[$cell]; }], + 'k33' => [ + "format" => "integer", + 'filter' => [ + 'class' => InputFilter::class, + 'param' => 'range' + ] + ], 'email' => function ($cell) { return "$cell"; }, ]); -//$table->setBeforePrintCell(function ($key, $data) { -// return $key == "email" ? "$data" : $data; -//}); $table->afterPrint(function ($meta) { return "
After Print
"; }); diff --git a/src/Filter/Filter.php b/src/Filter/Filter.php index 67c8ea3..5ee3a5b 100644 --- a/src/Filter/Filter.php +++ b/src/Filter/Filter.php @@ -5,13 +5,11 @@ namespace Itguild\Tables\Filter; abstract class Filter { public string $html = ''; - public string|array $data; + public string|array $param; public string $name; -// public array|string $value; public function __construct(array $source) { - $this->data = $source['data'] ?? ''; -// $this->value = $data['value'] ?? []; + $this->param = $source['param'] ?? ''; $this->name = $source['name']; } diff --git a/src/Filter/InputFilter.php b/src/Filter/InputFilter.php index 0b69f6d..24e9acb 100644 --- a/src/Filter/InputFilter.php +++ b/src/Filter/InputFilter.php @@ -9,17 +9,6 @@ class InputFilter extends Filter public function fetch() { -// if(is_array($this->data)){ -// $this->html = ""; -//// var_dump(key($this->data)); -// $key = key($this->data); -// foreach ($this->data[$key] as $value){ -//// echo "
";
-////                print_r($value);
-//                $this->html .= "";
-//            }
-//            return $this->html . "";
-//        }
-        return "";
+        return "";
     }
 }
\ No newline at end of file
diff --git a/src/Filter/SelectFilter.php b/src/Filter/SelectFilter.php
index 9c9999c..15931fb 100644
--- a/src/Filter/SelectFilter.php
+++ b/src/Filter/SelectFilter.php
@@ -10,7 +10,7 @@ class SelectFilter extends Filter
     public function fetch()
     {
         $this->html = "";
diff --git a/src/JasonTable.php b/src/JasonTable.php
index fcf4e24..f402dfd 100644
--- a/src/JasonTable.php
+++ b/src/JasonTable.php
@@ -91,33 +91,33 @@ class JasonTable
         return $styleStr;
     }
 
-    protected function getTagFromCustomColumn(string $column): string
-    {
-        if (is_array($this->beforePrintCell[$column])) {
-            if (isset($this->beforePrintCell[$column]['filter'])) {
-                if (is_array($this->beforePrintCell[$column]['filter'])) {
-//                    foreach ($this->beforePrintCell[$column]['filter'] as $key => $value) {
-                        return key($this->beforePrintCell[$column]['filter']);
-//                    }
-                }
-            }
-        }
-        return "text";
-    }
-
-    protected function getFilterFromCustomColumn(string $column)
-    {
-        if (is_array($this->beforePrintCell[$column])) {
-            if (isset($this->beforePrintCell[$column]['filter'])) {
-                if (is_array($this->beforePrintCell[$column]['filter'])) {
-//                    foreach ($this->beforePrintCell[$column]['filter'] as $value) {
-//                    echo "
";
-//                    print_r(current($this->beforePrintCell[$column]['filter']));
-                        return current($this->beforePrintCell[$column]['filter']);
-//                    }
-                }
-            }
-        }
-        return "text";
-        }
+//    protected function getTagFromCustomColumn(string $column): string
+//    {
+//        if (is_array($this->beforePrintCell[$column])) {
+//            if (isset($this->beforePrintCell[$column]['filter'])) {
+//                if (is_array($this->beforePrintCell[$column]['filter'])) {
+////                    foreach ($this->beforePrintCell[$column]['filter'] as $key => $value) {
+//                        return key($this->beforePrintCell[$column]['filter']);
+////                    }
+//                }
+//            }
+//        }
+//        return "text";
+//    }
+//
+//    protected function getFilterFromCustomColumn(string $column)
+//    {
+//        if (is_array($this->beforePrintCell[$column])) {
+//            if (isset($this->beforePrintCell[$column]['filter'])) {
+//                if (is_array($this->beforePrintCell[$column]['filter'])) {
+////                    foreach ($this->beforePrintCell[$column]['filter'] as $value) {
+////                    echo "
";
+////                    print_r(current($this->beforePrintCell[$column]['filter']));
+//                        return current($this->beforePrintCell[$column]['filter']);
+////                    }
+//                }
+//            }
+//        }
+//        return "text";
+//        }
 }
\ No newline at end of file
diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php
index 2209028..98058e0 100644
--- a/src/ListJsonTable.php
+++ b/src/ListJsonTable.php
@@ -147,13 +147,21 @@ class ListJsonTable extends JasonTable
         $this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
     }
 
-    private function getCurrentFilter(string $filter): false|string
+    private function getCurrentFilter(string $column)
     {
-        return match ($filter) {
-            'input' => InputFilter::class,
-            'select' => SelectFilter::class,
-            default => false,
-        };
+//        return match ($filter) {
+//            'input' => InputFilter::class,
+//            'select' => SelectFilter::class,
+//            default => false,
+//        };
+        if (is_array($this->beforePrintCell[$column])) {
+            if (isset($this->beforePrintCell[$column]['filter'])) {
+                if (is_array($this->beforePrintCell[$column]['filter'])) {
+                    return $this->beforePrintCell[$column]['filter'];
+                }
+            }
+        }
+        return false;
     }
 
     public function getCustomColumns($id = null): void
@@ -269,12 +277,18 @@ class ListJsonTable extends JasonTable
         foreach ($columnKeys as $key){
             if ($this->issetFilter($key)){
 //                $this->html .= "";
-                $item = $this->getCurrentFilter($this->getTagFromCustomColumn($key));
-                $objItem = new $item([
-                    'data' => $this->getFilterFromCustomColumn($key),
+//                $item = $this->getCurrentFilter($this->getTagFromCustomColumn($key));
+//                $objItem = new $item([
+//                    'data' => $this->getFilterFromCustomColumn($key),
+//                    'name' => $key
+//                ]);
+//                $this->html .= $objItem->fetch();
+                $arr = $this->getCurrentFilter($key);
+                $class = new $arr['class']([
+                    'param' => $arr['param'],
                     'name' => $key
                 ]);
-                $this->html .= $objItem->fetch();
+                $this->html .= $class->fetch();
             }
             else {
                 $this->html .= "";