我正在使用MPTT构建一个 comments /回复系统,我试图使用缩进来清楚地显示哪些 comments 是回复,以及这些回复处于哪个级别.

我使用comment.level属性来确定填充值,因此注释级别0将是父注释(因此没有填充),对该父注释的回复将具有注释.级别为1(因此填充为1),对回复的回复将具有注释.级别为2(因此填充为2).这将为父 comments 创建缩进pl-0,为第一个回复创建pl-1,为第二个回复创建pl-2.

{% load mptt_tags %}
        {% if not node.is_leaf_node %}
            {% for comment in comments %}
                {% if comment.level > 0 and comment.tree_id == tree_id %}
                    <div class="pl-{{ comment.level }} mt-2">
                        <div class="relative grid grid-cols-1 gap-4 p-4 mb-4 border rounded-lg bg-white shadow-lg">
                            <div class="relative flex gap-4">
                                <div class="flex flex-col w-full">
                                    <div class="flex flex-row justify-between">
                                        {% if not comment.deleted %}
                                            <p class="relative text-xl whitespace-nowrap truncate overflow-hidden">{{ comment.level }}</p>
                                        {% else %}
                                            <p class="relative text-xl whitespace-nowrap truncate overflow-hidden">[deleted]</p>
                                        {% endif %}
                                        <a class="text-gray-500 text-xl" href="#"><i class="fa-solid fa-trash"></i></a>
                                    </div>
                                        <p class="text-gray-400 text-sm">{{ comment.date_added }}</p>
                                </div>
                            </div>
                            {% if not comment.deleted %}
                                <p class="-mt-4 text-gray-500">{{ comment.body }}</p>
                            {% else %}
                                <p class="-mt-4 text-gray-500">[deleted]</p>
                            {% endif %}
                            <div class="relative">
                                {% if user == comment.author and not comment.deleted %}
                                    <button hx-delete="{% url 'posts:delete_comment' comment.id post %}" hx-confirm="Are you sure you want to delete your comment?" class="-mt-4 text-gray-500">delete</button>
                                {% endif %}
                                <button hx-get="{% url 'posts:comment_reply' id=comment.id pk=post %}" hx-swap="afterend" class="mr-auto -mt-4 text-gray-500">reply</button>
                            </div>
                        </div>
                    </div>
                {% endif %}
            {% endfor %}
        {% endif %}

我的问题是填充被忽略了,填充在DOM中正确通过,因为我可以看到我的回复有类似pl-3的填充.我认为这是因为父div不允许元素应用填充,但我无法解决这个问题.

当"请看回复..."按钮在下面的代码中被点击(靠近底部),它触发一个AJAX调用并加载上述代码.

{% if comments %}
        {% load mptt_tags %}
            {% recursetree comments %}
                <div class="relative grid grid-cols-1 gap-4 p-4 mb-4 border rounded-lg bg-white shadow-lg">
                    <div class="relative flex gap-4">
                        <div class="flex flex-col w-full">
                            <div class="flex flex-row justify-between">
                                {% if not node.deleted %}
                                    <p class="relative text-xl whitespace-nowrap truncate overflow-hidden">{{ node.level }}</p>
                                {% else %}
                                    <p class="relative text-xl whitespace-nowrap truncate overflow-hidden">[deleted]</p>
                                {% endif %}
                                <a class="text-gray-500 text-xl" href="#"><i class="fa-solid fa-trash"></i></a>
                            </div>
                                <p class="text-gray-400 text-sm">{{ node.date_added }}</p>
                        </div>
                    </div>
                    {% if not node.deleted %}
                        <p class="-mt-4 text-gray-500">{{ node.body }}</p>
                    {% else %}
                        <p class="-mt-4 text-gray-500">[deleted]</p>
                    {% endif %}
                    <div class="relative">
                        {% if user == node.author and not node.deleted %}
                            <button hx-delete="{% url 'posts:delete_comment' node.id post.pk %}" hx-confirm="Are you sure you want to delete your comment?" class="-mt-4 text-gray-500">delete</button>
                        {% endif %}
                        <button hx-get="{% url 'posts:comment_reply' id=node.id pk=post.pk %}" hx-swap="afterend" class="mr-auto -mt-4 text-gray-500">reply</button>
                        <button onclick="this.disabled = true" hx-get="{% url 'posts:load_replies' id=node.tree_id pk=post.pk %}" hx-swap="afterend" class="absolute right-0 text-gray-500">see replies...</button>
                    </div>
                </div>
            {% endrecursetree %}
    {% else %}
        <p>No comments</p>
{% endif %}

感谢这可能很难描绘,希望这张图片有所帮助,你可以看到对帖子的回复没有向内缩进,尽管他们有递增的填充.

enter image description here

推荐答案

Quick trial:如果你在你有pl-{{ comment.level }}的地方硬编码pl-2,它能起作用吗?

我怀疑您的问题与使用动态类名有关.按照official docs,TailWind只扫描代码中随时可用的类名.所以pl-{{ comment.level }}永远不会有任何生成的css.

您可以try 将所有可能的填充值安全列在您的TailWind配置中,如下所示:

  safelist: [
    'pl-1',
    'pl-2',
    'pl-3',
    // more as you deem fit..
  ]

更多关于safelisting的信息

希望这能帮上忙.

Html相关问答推荐

如何在两个弹性项目之间添加边框?

后续使用收件箱从网页表中提取值

如何防止SVG图标出现断行?

将网格包装在css中

Tumblr博客:如何修复提交页面和询问页面框列计数问题?

根据屏幕大小拆分长行列表

不能以Angular 更改输入的S边框 colored颜色

按钮之间的HTML文本居中不正确

如何将功能附加到嵌入式药剂中的按钮?

默认情况下使用Disbale Scroll,但在单击图标后将其激活

多次使用组件时计数Angular 14中嵌套数组的指令

在屏幕上每行 css 中的特定列居中

为什么我的网页显示网格区域彼此相邻,而不是我设置它们的方式?

理解图像与其内容框之间的关系

并排对齐两个文本区域列

如何使容器的大小适合其 position:absolute; 子元素的大小

需要帮助个性化我的 CSS 导航栏:如何在鼠标悬停时突出显示
  • 元素?
  • 图像比预期宽的网格列

    为什么将 p 元素的垂直边距设置为 1 px 会出现滚动条?

    隐藏在标题后面的下拉菜单