首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >刀片式递归,增加填充深度

刀片式递归,增加填充深度
EN

Stack Overflow用户
提问于 2021-02-09 06:25:49
回答 1查看 102关注 0票数 1

我希望在每次子项中都增加depth int,这样我就可以填充a标记。

下面是我的代码,我假设这会起作用,但是这个数字仍然是1,即使我在$items中的一个项目中有孩子。

sidebar.blade.php

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<?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');
    }
}

数据:

代码语言:javascript
运行
复制
$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' => '' ]
];

结果:

代码语言:javascript
运行
复制
Home 1
Test 1
Websites 1
Websites 1
Administration 1
Locked Devsites 5
Documentation 1
Logout 1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-17 21:17:24

如果我得到了您想要的结果,您甚至不需要$depth属性,因为刀片的@foreach指令有它自己的depth属性,它告诉您嵌套的程度。

在您的代码中,使用:

代码语言:javascript
运行
复制
:depth="$depth"

使用:

代码语言:javascript
运行
复制
:depth="$loop->depth"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66110285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档