Use native json_encode to convert treeView objects to JSON.

This should improve the performance.
This commit is contained in:
Jan Böhmer 2020-01-02 23:21:37 +01:00
parent fbcfc1f2a8
commit 811dca691b
4 changed files with 60 additions and 11 deletions

View file

@ -30,7 +30,7 @@ use App\Helpers\Trees\TreeViewNodeState;
* This class represents a node for the bootstrap treeview node.
* When you serialize an array of these objects to JSON, you can use the serialized data in data for the treeview.
*/
class TreeViewNode
class TreeViewNode implements \JsonSerializable
{
protected $text;
protected $href;
@ -206,4 +206,32 @@ class TreeViewNode
return $this;
}
/**
* @inheritDoc
*/
public function jsonSerialize()
{
$ret = [
'text' => $this->text
];
if($this->href !== null) {
$ret['href'] = $this->href;
}
if($this->tags !== null) {
$ret['tags'] = $this->tags;
}
if($this->nodes !== null) {
$ret['nodes'] = $this->nodes;
}
if($this->state !== null) {
$ret['state'] = $this->state;
}
return $ret;
}
}

View file

@ -21,7 +21,7 @@
namespace App\Helpers\Trees;
class TreeViewNodeState
class TreeViewNodeState implements \JsonSerializable
{
/** @var bool|null */
protected $checked = null;
@ -73,4 +73,25 @@ class TreeViewNodeState
{
$this->selected = $selected;
}
/**
* @inheritDoc
*/
public function jsonSerialize()
{
$ret = [];
if ($this->selected !== null) {
$ret['selected'] = $this->selected;
}
if($this->disabled !== null) {
$ret['disabled'] = $this->disabled;
}
if($this->expanded !== null) {
$ret['expanded'] = $this->expanded;
}
return $ret;
}
}