Skip to content

Instantly share code, notes, and snippets.

@marufsharia
Created January 24, 2023 16:37
Show Gist options
  • Select an option

  • Save marufsharia/7bf1649f069b2505e718de13450b210b to your computer and use it in GitHub Desktop.

Select an option

Save marufsharia/7bf1649f069b2505e718de13450b210b to your computer and use it in GitHub Desktop.

Create a tree view component using Laravel Livewire

Here's an example of how you might create a tree view component using Laravel Livewire: First, create a new Livewire component called TreeView:

php artisan make:livewire TreeView

Next, in the TreeView.php file, define a public property to store the tree data:

class TreeView extends Component
{
    public $treeData = [
        [
            'name' => 'Root Node 1',
            'children' => [
                ['name' => 'Child Node 1'],
                ['name' => 'Child Node 2']
            ]
        ],
        [
            'name' => 'Root Node 2',
            'children' => [
                ['name' => 'Child Node 3'],
                ['name' => 'Child Node 4']
            ]
        ]
    ];
    public function render()
    {
        return view('livewire.tree-view');
    }
}
  1. In the tree-view.blade.php file, iterate over the tree data and display it in a nested format using HTML and CSS. You can also add a toggle button for each node to show/hide its children
<div>
    <ul>
        @foreach($treeData as $node)
            <li>
                {{ $node['name'] }}
                <button wire:click="toggleChildren({{ $loop->index }})" class="btn btn-xs btn-secondary">
                    @if($node['open'])
                        -
                    @else
                        +
                    @endif
                </button>
                @if(count($node['children']) > 0)
                    <ul wire:show.transition.slide.up.500ms="$node['open']">
                        @foreach($node['children'] as $child)
                            <li>{{ $child['name'] }}</li>
                        @endforeach
                    </ul>
                @endif
            </li>
        @endforeach
    </ul>
</div>

4.Next, In the TreeView.php file, add a method toggleChildren to toggle the open property of the selected node

class TreeView extends Component
{
    public $treeData = [
        [
            'name' => 'Root Node 1',
            'open' => false,
            'children' => [
                ['name' => 'Child Node 1'],
                ['name' => 'Child Node 2']
            ]
        ],
        [
            'name' => 'Root Node 2',
            'open' => false,
            'children' => [
                ['name' => 'Child Node 3'],
                ['name' => 'Child Node 4']
            ]
        ]
    ];
    public function render()
    {
        return view('livewire.tree-view');
    }
    public function toggleChildren($index)
    {
        $this->treeData[$index]['open'] = ! $this->treeData[$index]['open'];
    }
}

This is a simple example, in a real-world application, you would likely have more complex logic, such as handling different types of nodes, adding new nodes

@bilogic
Copy link

bilogic commented Apr 7, 2024

make it drag drop to re-order?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment