【问题标题】:Display Comment in Nested Way like Gmail Comment以嵌套方式显示评论,如 Gmail 评论
【发布时间】:2016-11-18 13:50:00
【问题描述】:

我有一张桌子comments,看起来像这样,还添加了一些样机内容:

+------------+---------+----------+-------------------+------------------------------------+---------------------------+
| comment_id | user_id | post_id  | comment_parent_id |          comment_content           | comment_creation_datetime |
+------------+---------+----------+-------------------+------------------------------------+---------------------------+
|         26 |       1 |    16329 |                 0 | Första                             | 2016-01-24 10:42:49       |
|         27 |       1 |    16329 |                26 | Svar till första                   | 2016-01-24 10:42:55       |
|         28 |       1 |    16329 |                26 | Andra svar till förta              | 2016-01-24 10:43:06       |
|         29 |       1 |    16329 |                28 | Svar till "andra svar till första" | 2016-01-24 10:43:23       |
+------------+---------+----------+-------------------+------------------------------------+---------------------------+

我正在尝试显示 cmets Reddit 样式,如下图:

即使我不明白如何开始从哪里开始...... 我尝试用谷歌搜索找到与我的要求相关的文章,但我没有找到。 任何帮助... 我在 cmets 下面发布图片,因为我没有声誉点来发布图片谢谢

【问题讨论】:

  • @fubo : 如何在层次结构中显示 cmets

标签: c# asp.net comments


【解决方案1】:

假设你有一个班级评论

public class Comment
{
    public int comment_id { get; set; }
    public int user_id { get; set; }
    public int post_id { get; set; }
    public int comment_parent_id { get; set; }
    public string comment_content { get; set; }
    public DateTime comment_creation_datetime { get; set; }

    public Comment(int comment_id, int user_id, int post_id, int comment_parent_id, string comment_content, DateTime comment_creation_datetime)
    {
        this.comment_id = comment_id;
        this.user_id = user_id;
        this.post_id = post_id;
        this.comment_parent_id = comment_parent_id;
        this.comment_content = comment_content;
        this.comment_creation_datetime = comment_creation_datetime;
    }

    public override string ToString()
    {
        return string.Format("{0} {1} {2}", this.comment_id, this.comment_content, this.comment_creation_datetime);
    }
}

填充以下示例值

List<Comment> cList = new List<Comment>();
cList.Add(new Comment(26, 1, 16329, 0, "Första  ", new DateTime(2016, 01, 24, 10, 42, 49)));
cList.Add(new Comment(27, 1, 16329, 26, "Svar till första", new DateTime(2016, 01, 24, 10, 42, 55)));
cList.Add(new Comment(28, 1, 16329, 26, "Andra svar till förta", new DateTime(2016, 01, 24, 10, 43, 06)));
cList.Add(new Comment(29, 1, 16329, 28, "Svar till andra svar till första", new DateTime(2016, 01, 24, 10, 43, 23)));

你需要一个方法来对项目进行排序

public static IEnumerable<Comment> Sort(List<Comment> SortItemsList, int parentID = 0)
{
    foreach (var item in SortItemsList.Where(x => x.comment_parent_id == parentID).OrderBy(x => x.comment_id).ToList())
    {
        yield return item;
        foreach (var child in Sort(SortItemsList, item.comment_id))
        {
            yield return child;
        }
    }
}

和一个确定每个项目的层次结构深度

public static int GetDepth(List<Comment> cList, Comment cItem)
{
    if (cItem.comment_parent_id == 0)
    {
        return 0;
    }
    else
    {
        return GetDepth(cList, cList.Where(x => x.comment_id == cItem.comment_parent_id).Single()) + 1;
    }
}

例子:

foreach (Comment cItem in Sort( cList))
{
    Console.WriteLine(GetDepth(cList, cItem) + " | " + cItem.ToString());
}

另一种方法是对 here 描述的 cmets 进行嵌套处理。

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2012-09-17
    • 2022-11-18
    • 2014-08-28
    • 1970-01-01
    • 2013-06-05
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多