簡體   English   中英

Blade Recursive,增加填充的深度

[英]Blade Recursive, increase the depth for padding

每次孩子們在一個項目中時,我都想增加深度int ,這樣我就可以填充a標簽。

下面是我的代碼,我認為這會起作用,但是即使我在$items中的一個項目中有孩子,這個數字仍然保持在 1 。

sidebar.blade.php

<nav class="flex flex-col mt-10">
    <ul class="pl-4">
        @foreach($items as $item)
        <x-layouts.sidebar.items :item="$item"></x-layouts.sidebar.items>
        @endforeach
    </ul>
</nav>

sidebar.items.blade.php

<li>
    @if(count($item) > 0)
        <a href="{{ $item['href'] }}" class="focus:text-blue-500 dark-focus:text-blue-400 focus:outline-none w-full transition duration-500 ease-in-out pl-4 py-2 text-gray-700 dark:text-gray-400 hover:bg-blue-200 dark-hover:bg-blue-500 transition duration-500 ease-in-out block">
            {{ $item['text'] }} {{ $depth }}
        </a>
        @if (count($item['children']) > 0)
            <ul class="pl-4">
                @foreach($item['children'] as $child)
                    <x-layouts.sidebar.items :item="$child" :depth="$loop->parent->index"></x-layouts.sidebar.items>
                @endforeach
            </ul>
        @endif
    @else
        <hr>
    @endif
</li>

Sidebar\Items.php

<?php

namespace App\View\Components\Layouts\Sidebar;

use Illuminate\View\Component;
use Illuminate\View\View;

class Items extends Component
{

    /**
     * @var array
     */
    public $item;

    /**
     * @var int
     */
    public $depth;

    /**
     * Create a new component instance.
     *
     * @param array $item
     * @param int   $depth
     */
    public function __construct($item = [], $depth = 1)
    {
        $this->item = $item;
        $this->depth += $depth;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return View|string
     */
    public function render()
    {
        return view('components.layouts.sidebar.items');
    }
}

數據:

$this->items = [
    [ 'href' => '/home', 'text' => 'Home', 'children' => [], 'active' => 'border-l-2 border-blue-500' ],
    [ 'href' => 'javascript:void(0)', 'text' => 'Test', 'children' => [], 'active' => '' ],
    [ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
    [ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
    [],
    [
        'href' => '/administration',
        'text' => 'Administration',
        'children' => [
            [
                'href' => 'javascript:void(0)',
                'text' => 'Locked Devsites',
                'active' => '',
                'children' => []
            ]
        ],
        'active' => ''
    ],
    [ 'href' => 'javascript:void(0)', 'text' => 'Documentation', 'children' => [], 'active' => '' ],
    [ 'href' => 'javascript:void(0)', 'text' => 'Logout', 'children' => [], 'active' => '' ]
];

結果:

Home 1
Test 1
Websites 1
Websites 1
Administration 1
Locked Devsites 5
Documentation 1
Logout 1

如果我得到了你想要實現的目標,你甚至不需要$depth屬性,因為刀片的@foreach指令有它自己的 depth 屬性,它告訴你你是如何嵌套的。

在您的代碼中,而不是使用:

:depth="$depth"

利用:

:depth="$loop->depth"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM