You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel实现:根据数据库记录为导航表格TD标签设置颜色

解决方案

第一步:在控制器中提取已完成的模块列表

在获取$listings数据后,提取所有已完成的模块名称并整理成统一格式的数组,避免大小写、空格导致的匹配问题:

// 假设这是你获取用户记录的代码
$listings = YourModel::where('user_id', auth()->id())->get();

// 提取已完成模块,统一转为小写并去除首尾空格
$completedModules = $listings->pluck('module')
    ->map(fn($module) => trim(strtolower($module)))
    ->toArray();

// 将数据传递到视图
return view('your-page-view', compact('listings', 'completedModules'));

第二步:修改导航表格的Blade代码

在导航表格的每个<td>标签中,判断当前模块是否存在于已完成数组中,动态添加样式或类:

方式1:直接内联样式

<table class="table table-striped">
    <thead>
    </thead>
    <tbody>
    <tr>
        <td @if(in_array(strtolower(trim('Bloodborne Maws')), $completedModules)) style="color: gray;" @endif>
            <a href="/BP">Bloodborne Maws</a>
        </td>
    </tr>
    <tr>
        <td @if(in_array(strtolower(trim('Bloodborne hyper')), $completedModules)) style="color: gray;" @endif>
            <a href="/CSA">Bloodborne hyper</a>
        </td>
    </tr>
    <tr>
        <td @if(in_array(strtolower(trim('Bloodborne status')), $completedModules)) style="color: gray;" @endif>
            <a href="/CE">Bloodborne status</a>
        </td>
    </tr>
    <tr>
        <td @if(in_array(strtolower(trim('Bloodborne topical')), $completedModules)) style="color: gray;" @endif>
            <a href="/CR">Bloodborne topical</a>
        </td>
    </tr>
    </tbody>
</table>

方式2:使用CSS类(更易维护)

先在页面样式中添加:

.completed-module {
    color: gray;
    /* 可按需添加其他样式,比如背景色、字体加粗等 */
}

再修改导航表格:

<table class="table table-striped">
    <thead>
    </thead>
    <tbody>
    <tr>
        <td class="@if(in_array(strtolower(trim('Bloodborne Maws')), $completedModules)) completed-module @endif">
            <a href="/BP">Bloodborne Maws</a>
        </td>
    </tr>
    <tr>
        <td class="@if(in_array(strtolower(trim('Bloodborne hyper')), $completedModules)) completed-module @endif">
            <a href="/CSA">Bloodborne hyper</a>
        </td>
    </tr>
    <tr>
        <td class="@if(in_array(strtolower(trim('Bloodborne status')), $completedModules)) completed-module @endif">
            <a href="/CE">Bloodborne status</a>
        </td>
    </tr>
    <tr>
        <td class="@if(in_array(strtolower(trim('Bloodborne topical')), $completedModules)) completed-module @endif">
            <a href="/CR">Bloodborne topical</a>
        </td>
    </tr>
    </tbody>
</table>

进阶优化(减少重复代码)

如果模块是固定的,可以提前定义模块列表数组,循环渲染导航表格:

@php
$modules = [
    ['name' => 'Bloodborne Maws', 'url' => '/BP'],
    ['name' => 'Bloodborne hyper', 'url' => '/CSA'],
    ['name' => 'Bloodborne status', 'url' => '/CE'],
    ['name' => 'Bloodborne topical', 'url' => '/CR'],
];
@endphp

<table class="table table-striped">
    <thead></thead>
    <tbody>
        @foreach($modules as $module)
        <tr>
            <td class="@if(in_array(strtolower(trim($module['name'])), $completedModules)) completed-module @endif">
                <a href="{{ $module['url'] }}">{{ $module['name'] }}</a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

内容的提问来源于stack exchange,提问作者Robert Jordan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 15:54:55