custom handler
This commit is contained in:
		@@ -6,9 +6,6 @@ use Itguild\Tables\ListJsonTable;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
$json = file_get_contents('simple.json');
 | 
					$json = file_get_contents('simple.json');
 | 
				
			||||||
$table = new ListJsonTable($json);
 | 
					$table = new ListJsonTable($json);
 | 
				
			||||||
//$table->column("status", function ($ceil){
 | 
					 | 
				
			||||||
//    return getStatusLabel()[$ceil];
 | 
					 | 
				
			||||||
//});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
$table->columns([
 | 
					$table->columns([
 | 
				
			||||||
    'status' => function ($ceil) {
 | 
					    'status' => function ($ceil) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace Itguild\Tables;
 | 
					namespace Itguild\Tables;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Closure;
 | 
				
			||||||
use Exception;
 | 
					use Exception;
 | 
				
			||||||
use Itguild\Tables\ActionColumn\ActionColumn;
 | 
					use Itguild\Tables\ActionColumn\ActionColumn;
 | 
				
			||||||
use Itguild\Tables\ActionColumn\DeleteActionColumn;
 | 
					use Itguild\Tables\ActionColumn\DeleteActionColumn;
 | 
				
			||||||
@@ -18,8 +19,7 @@ class ListJsonTable
 | 
				
			|||||||
    private string $json;
 | 
					    private string $json;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private int $count = 0;
 | 
					    private int $count = 0;
 | 
				
			||||||
//    private \Closure|false $beforePrintCell;
 | 
					    private \Closure|array|false $beforePrintCell;
 | 
				
			||||||
    private array $beforePrintCell;
 | 
					 | 
				
			||||||
    private \Closure|false $beforePrintHook;
 | 
					    private \Closure|false $beforePrintHook;
 | 
				
			||||||
    private \Closure|false $afterPrintHook;
 | 
					    private \Closure|false $afterPrintHook;
 | 
				
			||||||
    private string $baseUrl;
 | 
					    private string $baseUrl;
 | 
				
			||||||
@@ -35,8 +35,8 @@ class ListJsonTable
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    #[NoReturn] public function __construct(string $json)
 | 
					    #[NoReturn] public function __construct(string $json)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
//        $this->beforePrintCell = false;
 | 
					        $this->beforePrintCell = false;
 | 
				
			||||||
        $this->beforePrintCell = [];
 | 
					//        $this->beforePrintCellArr = [];
 | 
				
			||||||
        $this->json = $json;
 | 
					        $this->json = $json;
 | 
				
			||||||
        $this->data = json_decode($this->json, true);
 | 
					        $this->data = json_decode($this->json, true);
 | 
				
			||||||
        $this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
 | 
					        $this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
 | 
				
			||||||
@@ -108,21 +108,6 @@ class ListJsonTable
 | 
				
			|||||||
    public function createTbody(): void
 | 
					    public function createTbody(): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if ($this->data['data']) {
 | 
					        if ($this->data['data']) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
//            if($this->filter)
 | 
					 | 
				
			||||||
//            {
 | 
					 | 
				
			||||||
//                foreach ($this->data['meta']['columns'] as $col) {
 | 
					 | 
				
			||||||
//                    $this->html .= "<tr><td>";
 | 
					 | 
				
			||||||
//                    foreach ($this->data['meta']['filters'] as $filter) {
 | 
					 | 
				
			||||||
//                        if ($this->issetFilter($filter))
 | 
					 | 
				
			||||||
//                        {
 | 
					 | 
				
			||||||
//                            $filters = new Filter($filter, )
 | 
					 | 
				
			||||||
//                        }
 | 
					 | 
				
			||||||
//                    }
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//                }
 | 
					 | 
				
			||||||
//            }
 | 
					 | 
				
			||||||
            $this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
 | 
					            $this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
 | 
				
			||||||
            foreach ($this->data['data'] as $row) {
 | 
					            foreach ($this->data['data'] as $row) {
 | 
				
			||||||
                $this->html .= "<tr>";
 | 
					                $this->html .= "<tr>";
 | 
				
			||||||
@@ -133,22 +118,13 @@ class ListJsonTable
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                foreach ($row as $key => $ceil) {
 | 
					                foreach ($row as $key => $ceil) {
 | 
				
			||||||
                    if ($this->issetColumn($key) and $this->is_fillable($key)) {
 | 
					                    if ($this->issetColumn($key) and $this->is_fillable($key)) {
 | 
				
			||||||
                        foreach ($this->beforePrintCell as $column => $closure) {
 | 
					                        if($this->beforePrintCell) {
 | 
				
			||||||
//                            if ($this->beforePrintCell) {
 | 
					                            $ceil = $this->setCustomHandler($this->beforePrintCell, $key, $ceil);
 | 
				
			||||||
//                                $hook = $this->beforePrintCell;
 | 
					 | 
				
			||||||
//                                $ceil = $hook($key, $ceil);
 | 
					 | 
				
			||||||
//                            }
 | 
					 | 
				
			||||||
                                if ($key == $column) {
 | 
					 | 
				
			||||||
                                $hook = $closure;
 | 
					 | 
				
			||||||
                                $ceil = $hook($ceil);
 | 
					 | 
				
			||||||
                            }
 | 
					 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        $this->html .= "<td>" . $ceil . "</td>";
 | 
					                        $this->html .= "<td>" . $ceil . "</td>";
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					 | 
				
			||||||
                $this->getCustomColumns($row["id"] ?? null);
 | 
					                $this->getCustomColumns($row["id"] ?? null);
 | 
				
			||||||
 | 
					 | 
				
			||||||
                if ($this->showActionColumn) {
 | 
					                if ($this->showActionColumn) {
 | 
				
			||||||
                    if (isset($row["id"])) {
 | 
					                    if (isset($row["id"])) {
 | 
				
			||||||
                        $actions = $this->getActions($row["id"]);
 | 
					                        $actions = $this->getActions($row["id"]);
 | 
				
			||||||
@@ -323,15 +299,32 @@ class ListJsonTable
 | 
				
			|||||||
        echo $this->html;
 | 
					        echo $this->html;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function setBeforePrintCell(string $col, \Closure $closure): void
 | 
					    public function setBeforePrintCell(\Closure $closure): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->beforePrintCell[$col] = $closure;
 | 
					        $this->beforePrintCell = $closure;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function columns(array $data): void
 | 
					    public function columns(array $data): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        foreach ($data as $key => $value) {
 | 
					        foreach ($data as $key => $value) {
 | 
				
			||||||
            $this->setBeforePrintCell($key, $value);
 | 
					            $this->beforePrintCell[$key] = $value;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function setCustomHandler(Closure|array $handler, string $key, string $ceil)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (is_array($handler)) {
 | 
				
			||||||
 | 
					            foreach ($this->beforePrintCell as $column => $closure) {
 | 
				
			||||||
 | 
					                if ($key == $column) {
 | 
				
			||||||
 | 
					                    $hook = $closure;
 | 
				
			||||||
 | 
					                    $ceil = $hook($ceil);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $hook = $this->beforePrintCell;
 | 
				
			||||||
 | 
					            $ceil = $hook($key, $ceil);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return $ceil;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user