繁体   English   中英

Blazor 动态生成表行

[英]Blazor dynamically generated table rows

我创建了一个示例 c# blazor(服务器端)应用程序来练习一下。

我尝试为日历动态生成表行,但在那里遇到了问题。

<table class="table">
    <thead>
        <tr>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            @* currentDateTimeValues is a list with DateTime objects *@
            @foreach (var item in currentDateTimeValues)
            {
                @if (counter % 7 == 0 && counter > 0)
                {
                    @:</tr><tr>
                }
                counter++;
                <td>@item.ToString("dd.MM.yyyy")</td>
            }
        </tr>
    </tbody>
</table>

在 7 个单元格之后,应该创建一个新行,但它没有。 单元格 go 笔直向前,没有换行符。

直线前进的行

也许你们有什么想法。

更新:

与此同时,我创建了一个解决方法,因为我认为 blazor 无法处理</tr><tr>

我的 currentDateTimeValues-List 现在包含周对象

        <tbody>
            @* currentDateTimeValues is a list with Week objects *@
            @foreach (var week in currentDateTimeValues)
            {
            <tr>
                <td>@week.Monday</td>
                <td>@week.Tuesday</td>
                <td>@week.Wednesday</td>
                <td>@week.Thursday</td>
                <td>@week.Friday</td>
                <td>@week.Saturday</td>
                <td>@week.Sunday</td>
            </tr>
            }
        </tbody>

我认为您正在尝试的解决方案目前在 Blazor 中是不可能的。 为什么不使用两个循环来创建日历。 就像是:

@while (counter < currentDateTimeValues.Length)
{
    <tr>
       @{
            var i = 0;
            if (i < 7 && counter + i < currentDateTimeValues.Length)
            {
                <td>@currentDateTimeValues[counter + i].ToString("dd.MM.yyyy")</td>
                i++;
            }
        }
    </tr>
    counter += 7;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM